| 1 |
From Coq Require Eqdep_dec. |
| 2 |
From Coq Require Import NArith. |
| 3 |
From Coq Require Import Arith. |
| 4 |
From Coq Require Import Morphisms. |
| 5 |
From Coq Require Import Relation_Definitions. |
| 6 |
From Coq Require Import Lia. |
| 7 |
Import Nat. |
| 8 |
|
| 9 |
Scheme Equality for nat. |
| 10 |
|
| 11 |
Create HintDb natsimplify discriminated. |
| 12 |
|
| 13 |
Global Hint Resolve mod_bound_pos Nat.add_le_mono : arith. |
| 14 |
#[global] |
| 15 |
Hint Rewrite @mod_small @mod_mod @mod_1_l @mod_1_r succ_pred using lia : natsimplify. |
| 16 |
|
| 17 |
#[global] |
| 18 |
Hint Rewrite sub_diag add_0_l add_0_r sub_0_r sub_succ : natsimplify. |
| 19 |
|
| 20 |
Local Open Scope nat_scope. |
| 21 |
|
| 22 |
Lemma mod_bound_nonneg x y : 0 <= x mod y. |
| 23 |
Proof. |
| 24 |
now apply Nat.le_0_l. |
| 25 |
Qed. |
| 26 |
|
| 27 |
Lemma mod_bound_lt x y : 0 < y -> x mod y < y. |
| 28 |
Proof. apply Nat.mod_bound_pos; lia. Qed. |
| 29 |
|
| 30 |
Global Hint Resolve mod_bound_nonneg mod_bound_lt : arith. |
| 31 |
|
| 32 |
Lemma min_def {x y} : min x y = x - (x - y). |
| 33 |
Proof. apply Nat.min_case_strong; lia. Qed. |
| 34 |
Lemma max_def {x y} : max x y = x + (y - x). |
| 35 |
Proof. apply Nat.max_case_strong; lia. Qed. |
| 36 |
Ltac coq_lia := lia. |
| 37 |
Ltac handle_min_max_for_lia_gen min max := |
| 38 |
repeat match goal with |
| 39 |
| [ H : context[min _ _] |- _ ] => rewrite !min_def in H || setoid_rewrite min_def in H |
| 40 |
| [ H : context[max _ _] |- _ ] => rewrite !max_def in H || setoid_rewrite max_def in H |
| 41 |
| [ |- context[min _ _] ] => rewrite !min_def || setoid_rewrite min_def |
| 42 |
| [ |- context[max _ _] ] => rewrite !max_def || setoid_rewrite max_def |
| 43 |
end. |
| 44 |
Ltac handle_min_max_for_lia_case_gen min max := |
| 45 |
repeat match goal with |
| 46 |
| [ H : context[min _ _] |- _ ] => revert H |
| 47 |
| [ H : context[max _ _] |- _ ] => revert H |
| 48 |
| [ |- context[min _ _] ] => apply Nat.min_case_strong |
| 49 |
| [ |- context[max _ _] ] => apply Nat.max_case_strong |
| 50 |
end; |
| 51 |
intros. |
| 52 |
Ltac handle_min_max_for_lia := handle_min_max_for_lia_gen min max. |
| 53 |
Ltac handle_min_max_for_lia_case := handle_min_max_for_lia_case_gen min max. |
| 54 |
(* In 8.4, Nat.min is a definition, so we need to unfold it *) |
| 55 |
Ltac handle_min_max_for_lia_compat_84 := |
| 56 |
let min := (eval cbv [min] in min) in |
| 57 |
let max := (eval cbv [max] in max) in |
| 58 |
handle_min_max_for_lia_gen min max. |
| 59 |
Ltac handle_min_max_for_lia_case_compat_84 := |
| 60 |
let min := (eval cbv [min] in min) in |
| 61 |
let max := (eval cbv [max] in max) in |
| 62 |
handle_min_max_for_lia_case_gen min max. |
| 63 |
Ltac lia_with_min_max := |
| 64 |
handle_min_max_for_lia; |
| 65 |
try handle_min_max_for_lia_compat_84; |
| 66 |
lia. |
| 67 |
Ltac lia_with_min_max_case := |
| 68 |
handle_min_max_for_lia_case; |
| 69 |
try handle_min_max_for_lia_case_compat_84; |
| 70 |
lia. |
| 71 |
Tactic Notation "lia" := coq_lia. |
| 72 |
Tactic Notation "lia" "*" := lia_with_min_max_case. |
| 73 |
Tactic Notation "lia" "**" := lia_with_min_max. |
| 74 |
|
| 75 |
Global Instance nat_rect_Proper {P} : Proper (Logic.eq ==> forall_relation (fun _ => forall_relation (fun _ => Logic.eq)) ==> forall_relation (fun _ => Logic.eq)) (@nat_rect P). |
| 76 |
Proof. |
| 77 |
cbv [forall_relation]; intros O_case O_case' ? S_case S_case' HS n; subst O_case'; revert O_case. |
| 78 |
induction n as [|n IHn]; cbn [nat_rect]; intro; rewrite ?IHn, ?HS; reflexivity. |
| 79 |
Qed. |
| 80 |
Global Instance nat_rect_Proper_nondep {P} : Proper (Logic.eq ==> pointwise_relation _ (pointwise_relation _ Logic.eq) ==> Logic.eq ==> Logic.eq) (@nat_rect (fun _ => P)). |
| 81 |
Proof. repeat intro; subst; apply (@nat_rect_Proper (fun _ => P)); eauto. Qed. |
| 82 |
|
| 83 |
Global Instance nat_rect_Proper_nondep_gen {P} (R : relation P) : Proper (R ==> (Logic.eq ==> R ==> R) ==> Logic.eq ==> R) (@nat_rect (fun _ => P)) | 100. |
| 84 |
Proof. |
| 85 |
cbv [forall_relation respectful]; intros O_case O_case' HO S_case S_case' HS n n' ?; subst n'; revert O_case O_case' HO. |
| 86 |
induction n as [|n IHn]; cbn [nat_rect]; intros; eauto. |
| 87 |
Qed. |
| 88 |
|
| 89 |
Lemma nat_eq_dec_S x y |
| 90 |
: match nat_eq_dec (S x) (S y), nat_eq_dec x y with |
| 91 |
| left pfS, left pf => pfS = f_equal S pf |
| 92 |
| right _, right _ => True |
| 93 |
| _, _ => False |
| 94 |
end. |
| 95 |
Proof. |
| 96 |
unfold nat_eq_dec; simpl. |
| 97 |
match goal with |
| 98 |
| [ |- match match ?e with _ => _ end with _ => _ end ] |
| 99 |
=> destruct e |
| 100 |
end; simpl; try exact I. |
| 101 |
reflexivity. |
| 102 |
Defined. |
| 103 |
|
| 104 |
Lemma UIP_nat_transparent x y (p1 p2 : x = y :> nat) : p1 = p2. |
| 105 |
Proof. |
| 106 |
transitivity (match nat_eq_dec x y, nat_eq_dec y y with |
| 107 |
| left pf1, left pf2 => eq_trans pf1 (eq_sym pf2) |
| 108 |
| _, _ => p1 |
| 109 |
end); |
| 110 |
[ revert p2 | revert p1 ]; |
| 111 |
subst y; intro p; |
| 112 |
destruct (nat_eq_dec x x) as [q|q]; case q; reflexivity. |
| 113 |
Defined. |
| 114 |
|
| 115 |
Lemma nat_beq_false_iff x y : nat_beq x y = false <-> x <> y. |
| 116 |
Proof. |
| 117 |
split; intro H; repeat intro; subst. |
| 118 |
{ erewrite internal_nat_dec_lb in H by reflexivity; congruence. } |
| 119 |
{ destruct (nat_beq x y) eqn:H'; [ | reflexivity ]. |
| 120 |
apply internal_nat_dec_bl in H'; subst; congruence. } |
| 121 |
Qed. |
| 122 |
|
| 123 |
Ltac nat_beq_to_eq := |
| 124 |
repeat match goal with |
| 125 |
| [ H : nat_beq _ _ = true |- _ ] => apply internal_nat_dec_bl in H |
| 126 |
| [ H : is_true (nat_beq _ _) = true |- _ ] => apply internal_nat_dec_bl in H |
| 127 |
| [ |- nat_beq _ _ = true ] => apply internal_nat_dec_lb |
| 128 |
| [ |- is_true (nat_beq _ _) ] => apply internal_nat_dec_lb |
| 129 |
| [ H : nat_beq _ _ = false |- _ ] => apply nat_beq_false_iff in H |
| 130 |
| [ |- nat_beq _ _ = false ] => apply nat_beq_false_iff |
| 131 |
end. |
| 132 |
|
| 133 |
Lemma div_minus : forall a b, b <> 0 -> (a + b) / b = a / b + 1. |
| 134 |
Proof. |
| 135 |
intros a b H. |
| 136 |
assert (H0 : b = 1 * b) by lia. |
| 137 |
rewrite H0 at 1. |
| 138 |
rewrite <- Nat.div_add by auto. |
| 139 |
reflexivity. |
| 140 |
Qed. |
| 141 |
|
| 142 |
Lemma pred_mod : forall m, (0 < m)%nat -> ((pred m) mod m)%nat = pred m. |
| 143 |
Proof. |
| 144 |
intros m H; apply Nat.mod_small. |
| 145 |
destruct m; try lia; rewrite Nat.pred_succ; auto. |
| 146 |
Qed. |
| 147 |
|
| 148 |
Lemma div_add_l' : forall a b c, a <> 0 -> (a * b + c) / a = b + c / a. |
| 149 |
Proof. |
| 150 |
intros; rewrite Nat.mul_comm; auto using div_add_l. |
| 151 |
Qed. |
| 152 |
|
| 153 |
Lemma mod_add_l : forall a b c, b <> 0 -> (a * b + c) mod b = c mod b. |
| 154 |
Proof. |
| 155 |
intros; rewrite Nat.add_comm; auto using mod_add. |
| 156 |
Qed. |
| 157 |
|
| 158 |
Lemma mod_add_l' : forall a b c, b <> 0 -> (b * a + c) mod b = c mod b. |
| 159 |
Proof. |
| 160 |
intros; rewrite Nat.mul_comm; auto using mod_add_l. |
| 161 |
Qed. |
| 162 |
|
| 163 |
Lemma mod_div_eq0 : forall a b, b <> 0 -> a mod b / b = 0. |
| 164 |
Proof. |
| 165 |
intros; apply Nat.div_small, mod_bound_pos; lia. |
| 166 |
Qed. |
| 167 |
|
| 168 |
Lemma divide2_1mod4_nat : forall c x, c = x / 4 -> x mod 4 = 1 -> exists y, 2 * y = (x / 2). |
| 169 |
Proof. |
| 170 |
assert (4 <> 0) as ne40 by lia. |
| 171 |
induction c; intros x H H0; pose proof (div_mod x 4 ne40) as H1; rewrite <- H in H1. { |
| 172 |
rewrite H0 in H1. |
| 173 |
simpl in H1. |
| 174 |
rewrite H1. |
| 175 |
exists 0; auto. |
| 176 |
} { |
| 177 |
rewrite Nat.mul_succ_r in H1. |
| 178 |
assert (4 <= x) as le4x by (apply Nat.div_str_pos_iff; lia). |
| 179 |
rewrite <- Nat.add_1_r in H. |
| 180 |
replace x with ((x - 4) + 4) in H by lia. |
| 181 |
rewrite div_minus in H by auto. |
| 182 |
apply Nat.add_cancel_r in H. |
| 183 |
replace x with ((x - 4) + (1 * 4)) in H0 by lia. |
| 184 |
rewrite Nat.mod_add in H0 by auto. |
| 185 |
pose proof (IHc _ H H0) as H2. |
| 186 |
destruct H2 as [x0 H2]. |
| 187 |
exists (x0 + 1). |
| 188 |
rewrite <- (Nat.sub_add 4 x) in H1 at 1 by auto. |
| 189 |
replace (4 * c + 4 + x mod 4) with (4 * c + x mod 4 + 4) in H1 by lia. |
| 190 |
apply Nat.add_cancel_r in H1. |
| 191 |
replace (2 * (x0 + 1)) with (2 * x0 + 2) |
| 192 |
by (rewrite Nat.mul_add_distr_l; auto). |
| 193 |
rewrite H2. |
| 194 |
rewrite <- Nat.div_add by lia. |
| 195 |
f_equal. |
| 196 |
simpl. |
| 197 |
apply Nat.sub_add; auto. |
| 198 |
} |
| 199 |
Qed. |
| 200 |
|
| 201 |
Lemma Nat2N_inj_lt : forall n m, (N.of_nat n < N.of_nat m)%N <-> n < m. |
| 202 |
Proof. |
| 203 |
split; intros. { |
| 204 |
rewrite nat_compare_lt. |
| 205 |
rewrite Nnat.Nat2N.inj_compare. |
| 206 |
rewrite N.compare_lt_iff; auto. |
| 207 |
} { |
| 208 |
rewrite <- N.compare_lt_iff. |
| 209 |
rewrite <- Nnat.Nat2N.inj_compare. |
| 210 |
rewrite <- nat_compare_lt; auto. |
| 211 |
} |
| 212 |
Qed. |
| 213 |
|
| 214 |
Lemma lt_min_l : forall x a b, (x < min a b)%nat -> (x < a)%nat. |
| 215 |
Proof. |
| 216 |
intros ? ? ? lt_min. |
| 217 |
apply Nat.min_glb_lt_iff in lt_min. |
| 218 |
destruct lt_min; assumption. |
| 219 |
Qed. |
| 220 |
|
| 221 |
(* useful for hints *) |
| 222 |
Lemma eq_le_incl_rev : forall a b, a = b -> b <= a. |
| 223 |
Proof. |
| 224 |
intros; lia. |
| 225 |
Qed. |
| 226 |
|
| 227 |
Lemma beq_nat_eq_nat_dec {R} (x y : nat) (a b : R) |
| 228 |
: (if Nat.eqb x y then a else b) = (if eq_nat_dec x y then a else b). |
| 229 |
Proof. |
| 230 |
destruct (eq_nat_dec x y) as [H|H]; |
| 231 |
[ rewrite (proj2 (@Nat.eqb_eq _ _) H); reflexivity |
| 232 |
| rewrite (proj2 (@Nat.eqb_neq _ _) H); reflexivity ]. |
| 233 |
Qed. |
| 234 |
|
| 235 |
Lemma pow_nonzero a k : a <> 0 -> a ^ k <> 0. |
| 236 |
Proof. |
| 237 |
intro; induction k; simpl; nia. |
| 238 |
Qed. |
| 239 |
|
| 240 |
Global Hint Resolve pow_nonzero : arith. |
| 241 |
|
| 242 |
Lemma S_pred_nonzero : forall a, (a > 0 -> S (pred a) = a)%nat. |
| 243 |
Proof. |
| 244 |
destruct a; simpl; lia. |
| 245 |
Qed. |
| 246 |
|
| 247 |
#[global] |
| 248 |
Hint Rewrite S_pred_nonzero using lia : natsimplify. |
| 249 |
|
| 250 |
Lemma mod_same_eq a b : a <> 0 -> a = b -> b mod a = 0. |
| 251 |
Proof. intros; subst; apply mod_same; assumption. Qed. |
| 252 |
|
| 253 |
#[global] |
| 254 |
Hint Rewrite @mod_same_eq using lia : natsimplify. |
| 255 |
Global Hint Resolve mod_same_eq : arith. |
| 256 |
|
| 257 |
Lemma mod_mod_eq a b c : a <> 0 -> b = c mod a -> b mod a = b. |
| 258 |
Proof. intros; subst; autorewrite with natsimplify; reflexivity. Qed. |
| 259 |
|
| 260 |
#[global] |
| 261 |
Hint Rewrite @mod_mod_eq using (reflexivity || lia) : natsimplify. |
| 262 |
|
| 263 |
Local Arguments minus !_ !_. |
| 264 |
|
| 265 |
Lemma S_mod_full a b : a <> 0 -> (S b) mod a = if eq_nat_dec (S (b mod a)) a |
| 266 |
then 0 |
| 267 |
else S (b mod a). |
| 268 |
Proof. |
| 269 |
change (S b) with (1+b); intros. |
| 270 |
pose proof (mod_bound_pos b a). |
| 271 |
rewrite add_mod by assumption. |
| 272 |
destruct (eq_nat_dec (S (b mod a)) a) as [H'|H']; |
| 273 |
destruct a as [|[|a]]; autorewrite with natsimplify in *; |
| 274 |
try congruence; try reflexivity. |
| 275 |
Qed. |
| 276 |
|
| 277 |
#[global] |
| 278 |
Hint Rewrite S_mod_full using lia : natsimplify. |
| 279 |
|
| 280 |
Lemma S_mod a b : a <> 0 -> S (b mod a) <> a -> (S b) mod a = S (b mod a). |
| 281 |
Proof. |
| 282 |
intros; rewrite S_mod_full by assumption. |
| 283 |
edestruct eq_nat_dec; lia. |
| 284 |
Qed. |
| 285 |
|
| 286 |
#[global] |
| 287 |
Hint Rewrite S_mod using (lia || autorewrite with natsimplify; lia) : natsimplify. |
| 288 |
|
| 289 |
Lemma eq_nat_dec_refl x : eq_nat_dec x x = left (Logic.eq_refl x). |
| 290 |
Proof. |
| 291 |
edestruct eq_nat_dec; try congruence. |
| 292 |
apply f_equal, Eqdep_dec.UIP_dec, eq_nat_dec. |
| 293 |
Qed. |
| 294 |
|
| 295 |
#[global] |
| 296 |
Hint Rewrite eq_nat_dec_refl : natsimplify. |
| 297 |
|
| 298 |
(** Helper to get around the lack of function extensionality *) |
| 299 |
Definition eq_nat_dec_right_val n m (pf0 : n <> m) : { pf | eq_nat_dec n m = right pf }. |
| 300 |
Proof. |
| 301 |
revert dependent n; induction m as [|m IHm]; destruct n as [|n]; simpl; |
| 302 |
intro pf0; |
| 303 |
[ (exists pf0; exfalso; abstract congruence) |
| 304 |
| eexists; reflexivity |
| 305 |
| eexists; reflexivity |
| 306 |
| ]. |
| 307 |
{ specialize (IHm n). |
| 308 |
destruct IHm as [? IHm]; [ lia | ]. |
| 309 |
eexists; rewrite IHm; reflexivity. } |
| 310 |
Qed. |
| 311 |
|
| 312 |
Lemma eq_nat_dec_S_n n : eq_nat_dec (S n) n = right (proj1_sig (@eq_nat_dec_right_val _ _ (@neq_succ_diag_l n))). |
| 313 |
Proof. |
| 314 |
edestruct eq_nat_dec_right_val; assumption. |
| 315 |
Qed. |
| 316 |
|
| 317 |
#[global] |
| 318 |
Hint Rewrite eq_nat_dec_S_n : natsimplify. |
| 319 |
|
| 320 |
Lemma eq_nat_dec_n_S n : eq_nat_dec n (S n) = right (proj1_sig (@eq_nat_dec_right_val _ _ (n_Sn n))). |
| 321 |
Proof. |
| 322 |
edestruct eq_nat_dec_right_val; assumption. |
| 323 |
Qed. |
| 324 |
|
| 325 |
#[global] |
| 326 |
Hint Rewrite eq_nat_dec_n_S : natsimplify. |
| 327 |
|
| 328 |
#[global] |
| 329 |
Hint Rewrite Nat.max_0_l Nat.max_0_r Nat.max_idempotent Nat.min_0_l Nat.min_0_r Nat.min_idempotent : natsimplify. |
| 330 |
|
| 331 |
(** Helper to get around the lack of function extensionality *) |
| 332 |
Definition le_dec_right_val n m (pf0 : ~n <= m) : { pf | le_dec n m = right pf }. |
| 333 |
Proof. |
| 334 |
destruct (le_dec n m) eqn:Heq; [ | eexists; reflexivity ]. |
| 335 |
exfalso; clear Heq; apply pf0; assumption. |
| 336 |
Qed. |
| 337 |
|
| 338 |
(** Helper to get around the lack of function extensionality *) |
| 339 |
Definition lt_dec_right_val n m (pf0 : ~n < m) : { pf | lt_dec n m = right pf } |
| 340 |
:= @le_dec_right_val _ _ pf0. |
| 341 |
|
| 342 |
Lemma lt_dec_irrefl n : lt_dec n n = right (proj1_sig (@lt_dec_right_val _ _ (lt_irrefl n))). |
| 343 |
Proof. edestruct lt_dec_right_val; assumption. Qed. |
| 344 |
#[global] |
| 345 |
Hint Rewrite lt_dec_irrefl : natsimplify. |
| 346 |
|
| 347 |
Lemma not_lt_n_pred_n n : ~n < pred n. |
| 348 |
Proof. destruct n; simpl; lia. Qed. |
| 349 |
|
| 350 |
Lemma lt_dec_n_pred_n n : lt_dec n (pred n) = right (proj1_sig (@lt_dec_right_val _ _ (not_lt_n_pred_n n))). |
| 351 |
Proof. edestruct lt_dec_right_val; assumption. Qed. |
| 352 |
#[global] |
| 353 |
Hint Rewrite lt_dec_n_pred_n : natsimplify. |
| 354 |
|
| 355 |
Lemma le_dec_refl n : le_dec n n = left (le_refl n). |
| 356 |
Proof. |
| 357 |
edestruct le_dec; try ((idtac + exfalso); lia). |
| 358 |
apply f_equal, le_unique. |
| 359 |
Qed. |
| 360 |
#[global] |
| 361 |
Hint Rewrite le_dec_refl : natsimplify. |
| 362 |
|
| 363 |
Lemma le_dec_pred_l n : le_dec (pred n) n = left (le_pred_l n). |
| 364 |
Proof. |
| 365 |
edestruct le_dec; [ | destruct n; simpl in *; (idtac + exfalso); lia ]. |
| 366 |
apply f_equal, le_unique. |
| 367 |
Qed. |
| 368 |
#[global] |
| 369 |
Hint Rewrite le_dec_pred_l : natsimplify. |
| 370 |
|
| 371 |
Lemma le_pred_plus_same n : n <= pred (n + n). |
| 372 |
Proof. destruct n; simpl; lia. Qed. |
| 373 |
|
| 374 |
Lemma le_dec_pred_plus_same n : le_dec n (pred (n + n)) = left (le_pred_plus_same n). |
| 375 |
Proof. |
| 376 |
edestruct le_dec; [ | destruct n; simpl in *; (idtac + exfalso); lia ]. |
| 377 |
apply f_equal, le_unique. |
| 378 |
Qed. |
| 379 |
#[global] |
| 380 |
Hint Rewrite le_dec_pred_plus_same : natsimplify. |
| 381 |
|
| 382 |
Lemma minus_S_diag x : (S x - x = 1)%nat. |
| 383 |
Proof. lia. Qed. |
| 384 |
#[global] |
| 385 |
Hint Rewrite minus_S_diag : natsimplify. |
| 386 |
|
| 387 |
Lemma min_idempotent_S_l x : min (S x) x = x. |
| 388 |
Proof. lia *. Qed. |
| 389 |
#[global] |
| 390 |
Hint Rewrite min_idempotent_S_l : natsimplify. |
| 391 |
|
| 392 |
Lemma min_idempotent_S_r x : min x (S x) = x. |
| 393 |
Proof. lia *. Qed. |
| 394 |
#[global] |
| 395 |
Hint Rewrite min_idempotent_S_r : natsimplify. |
| 396 |
|
| 397 |
Lemma mod_pow_same b e : b <> 0 -> e <> 0 -> b^e mod b = 0. |
| 398 |
Proof. |
| 399 |
intros; destruct e as [|e]; [ lia | simpl ]. |
| 400 |
rewrite mul_comm, mod_mul by lia; lia. |
| 401 |
Qed. |
| 402 |
|
| 403 |
Lemma setbit_high : forall x i, (x < 2^i -> setbit x i = x + 2^i)%nat. |
| 404 |
Proof. |
| 405 |
intros x i H; apply bits_inj; intro n. |
| 406 |
rewrite setbit_eqb. |
| 407 |
destruct (Nat.eqb i n) eqn:H'; simpl. |
| 408 |
{ apply Nat.eqb_eq in H'; subst. |
| 409 |
symmetry; apply testbit_true. |
| 410 |
rewrite div_minus, div_small by lia. |
| 411 |
reflexivity. } |
| 412 |
{ assert (H'' : (((x + 2 ^ i) / 2 ^ n) mod 2) = ((x / 2 ^ n) mod 2)). |
| 413 |
{ assert (2^(i-n) <> 0) by auto with arith. |
| 414 |
assert (2^(i-n) <> 0) by lia. |
| 415 |
destruct (lt_eq_lt_dec i n) as [ [?|?] | ? ]; [ | subst; rewrite Nat.eqb_refl in H'; congruence | ]. |
| 416 |
{ assert (i <= n - 1) by lia. |
| 417 |
assert (2^i <= 2^n) by auto using pow_le_mono_r with arith. |
| 418 |
assert (2^i <= 2^(n - 1)) by auto using pow_le_mono_r with arith. |
| 419 |
assert (2^(n-1) <> 0) by auto with arith. |
| 420 |
assert (2^(n-1) + 2^(n-1) = 2^n) |
| 421 |
by (transitivity (2^(S (n - 1))); [ simpl; lia | apply f_equal; lia ]). |
| 422 |
assert ((2^(n - 1) - 1) + (2^(n - 1)) < 2^n) by lia. |
| 423 |
rewrite !div_small; try lia. } |
| 424 |
{ replace (2^i) with (2^(i - n) * 2^n) |
| 425 |
by (rewrite <- pow_add_r, ?le_plus_minus_r, ?sub_add by lia; lia). |
| 426 |
rewrite div_add by auto with arith. |
| 427 |
rewrite <- add_mod_idemp_r, mod_pow_same, add_0_r by lia. |
| 428 |
reflexivity. } } |
| 429 |
{ match goal with |
| 430 |
| [ |- ?x = ?y ] |
| 431 |
=> destruct x eqn:H0; destruct y eqn:H1; try reflexivity; |
| 432 |
try apply testbit_true in H0; |
| 433 |
try apply testbit_true in H1; |
| 434 |
first [ rewrite <- H0; clear H0 | rewrite <- H1; clear H1 ]; |
| 435 |
first [ symmetry; apply testbit_true | apply testbit_true ] |
| 436 |
end; |
| 437 |
rewrite H'' in *; assumption. } } |
| 438 |
Qed. |
| 439 |
|
| 440 |
Lemma max_0_iff a b : Nat.max a b = 0%nat <-> (a = 0%nat /\ b = 0%nat). |
| 441 |
Proof. lia **. Qed. |
| 442 |
|
| 443 |
Lemma push_f_nat_rect {P P'} (f : P -> P') PO PS PS' n |
| 444 |
(HS : forall x rec, f (PS x rec) |
| 445 |
= PS' x (f rec)) |
| 446 |
: f (nat_rect (fun _ => P) PO PS n) |
| 447 |
= nat_rect |
| 448 |
(fun _ => _) |
| 449 |
(f PO) |
| 450 |
PS' |
| 451 |
n. |
| 452 |
Proof. |
| 453 |
induction n as [|n IHn]; cbn [nat_rect]; [ reflexivity | ]. |
| 454 |
rewrite HS, IHn; reflexivity. |
| 455 |
Qed. |
| 456 |
|
| 457 |
Lemma push_f_nat_rect_arrow {P P'} (f : P -> P') {A} PO PS PS' n v |
| 458 |
(HS : forall x rec v, f (PS x rec v) |
| 459 |
= PS' x (fun v => f (rec v)) v) |
| 460 |
(PS'_Proper : Proper (Logic.eq ==> pointwise_relation _ Logic.eq ==> Logic.eq ==> Logic.eq) PS') |
| 461 |
: f (nat_rect (fun _ => A -> P) PO PS n v) |
| 462 |
= nat_rect |
| 463 |
(fun _ => _) |
| 464 |
(fun v => f (PO v)) |
| 465 |
PS' |
| 466 |
n |
| 467 |
v. |
| 468 |
Proof. |
| 469 |
revert v; induction n as [|n IHn]; cbn [nat_rect]; [ reflexivity | ]; intro. |
| 470 |
rewrite HS; apply PS'_Proper; eauto. |
| 471 |
Qed. |
| 472 |
|
| 473 |
Lemma min_x_xy x y : Nat.min x (Nat.min x y) = Nat.min x y. |
| 474 |
Proof. now rewrite Nat.min_assoc; autorewrite with natsimplify. Qed. |
| 475 |
#[global] |
| 476 |
Hint Rewrite min_x_xy : natsimplify. |
| 477 |
|
| 478 |
Lemma min_x_yx x y : Nat.min x (Nat.min y x) = Nat.min x y. |
| 479 |
Proof. now rewrite Nat.min_comm, <- Nat.min_assoc, Nat.min_comm; autorewrite with natsimplify. Qed. |
| 480 |
#[global] |
| 481 |
Hint Rewrite min_x_yx : natsimplify. |
| 482 |
|
| 483 |
Lemma min_xy_x x y : Nat.min (Nat.min x y) x = Nat.min x y. |
| 484 |
Proof. now rewrite Nat.min_comm, Nat.min_assoc; autorewrite with natsimplify. Qed. |
| 485 |
#[global] |
| 486 |
Hint Rewrite min_xy_x : natsimplify. |
| 487 |
|
| 488 |
Lemma min_yx_x x y : Nat.min (Nat.min y x) x = Nat.min y x. |
| 489 |
Proof. now rewrite <- Nat.min_assoc; autorewrite with natsimplify. Qed. |
| 490 |
#[global] |
| 491 |
Hint Rewrite min_yx_x : natsimplify. |
| 492 |
|
| 493 |
Lemma max_x_xy x y : Nat.max x (Nat.max x y) = Nat.max x y. |
| 494 |
Proof. now rewrite Nat.max_assoc; autorewrite with natsimplify. Qed. |
| 495 |
#[global] |
| 496 |
Hint Rewrite max_x_xy : natsimplify. |
| 497 |
|
| 498 |
Lemma max_x_yx x y : Nat.max x (Nat.max y x) = Nat.max x y. |
| 499 |
Proof. now rewrite Nat.max_comm, <- Nat.max_assoc, Nat.max_comm; autorewrite with natsimplify. Qed. |
| 500 |
#[global] |
| 501 |
Hint Rewrite max_x_yx : natsimplify. |
| 502 |
|
| 503 |
Lemma max_xy_x x y : Nat.max (Nat.max x y) x = Nat.max x y. |
| 504 |
Proof. now rewrite Nat.max_comm, Nat.max_assoc; autorewrite with natsimplify. Qed. |
| 505 |
#[global] |
| 506 |
Hint Rewrite max_xy_x : natsimplify. |
| 507 |
|
| 508 |
Lemma max_yx_x x y : Nat.max (Nat.max y x) x = Nat.max y x. |
| 509 |
Proof. now rewrite <- Nat.max_assoc; autorewrite with natsimplify. Qed. |
| 510 |
#[global] |
| 511 |
Hint Rewrite max_yx_x : natsimplify. |
| 512 |
|
| 513 |
Ltac inversion_nat_eq_step := |
| 514 |
match goal with |
| 515 |
| [ H : O = S _ |- _ ] => solve [ inversion H ] |
| 516 |
| [ H : S _ = O |- _ ] => solve [ inversion H ] |
| 517 |
| [ H : O = O |- _ ] => clear H |
| 518 |
| [ H : O = O |- _ ] => pose proof (@UIP_nat _ _ eq_refl H); subst H |
| 519 |
| [ H : S _ = S _ |- _ ] |
| 520 |
=> apply (f_equal pred) in H; cbn [pred] in H |
| 521 |
end. |
| 522 |
Ltac inversion_nat_eq := repeat inversion_nat_eq_step. |
| 523 |
|
| 524 |
Ltac inversion_nat_rel_step := |
| 525 |
first [ inversion_nat_eq_step |
| 526 |
| match goal with |
| 527 |
| [ H : S _ <= O |- _ ] => exfalso; clear -H; lia |
| 528 |
| [ H : _ < O |- _ ] => exfalso; clear -H; lia |
| 529 |
| [ H : O >= S _ |- _ ] => exfalso; clear -H; lia |
| 530 |
| [ H : O > _ |- _ ] => exfalso; clear -H; lia |
| 531 |
| [ H : O <= _ |- _ ] => clear H |
| 532 |
| [ H : O < S _ |- _ ] => clear H |
| 533 |
| [ H : _ >= O |- _ ] => clear H |
| 534 |
| [ H : S _ > O |- _ ] => clear H |
| 535 |
| [ H : S _ <= S _ |- _ ] => apply le_S_n in H |
| 536 |
| [ H : S _ < S _ |- _ ] => rewrite <- succ_lt_mono in H |
| 537 |
| [ H : S _ >= S _ |- _ ] => progress cbv [ge] in H |
| 538 |
| [ H : S _ > S _ |- _ ] => progress cbv [gt] in H |
| 539 |
end ]. |
| 540 |
Ltac inversion_nat_rel := repeat inversion_nat_rel_step. |