| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import Lia. |
| 4 |
Require Import Crypto.Util.ZRange. |
| 5 |
Require Import Crypto.Util.ZRange.BasicLemmas. |
| 6 |
Require Import Crypto.Util.ZRange.Operations. |
| 7 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 8 |
Require Import Crypto.Util.ZUtil.Tactics.PeelLe. |
| 9 |
Require Import Crypto.Util.ZUtil.Tactics.ReplaceNegWithPos. |
| 10 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 11 |
Require Import Crypto.Util.ZUtil.ZSimplify. |
| 12 |
Require Import Crypto.Util.ZUtil.ZSimplify.Core. |
| 13 |
Require Import Crypto.Util.Prod. |
| 14 |
Require Import Crypto.Util.Option. |
| 15 |
Require Import Crypto.Util.ListUtil. |
| 16 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 17 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 18 |
Import ListNotations. |
| 19 |
Local Open Scope list_scope. |
| 20 |
Local Open Scope Z_scope. |
| 21 |
|
| 22 |
Module ZRange. |
| 23 |
Import Operations.ZRange. |
| 24 |
Lemma split_range_at_0_covers r |
| 25 |
(Hr : goodb r = true) |
| 26 |
: covers |
| 27 |
match split_range_at_0 r with |
| 28 |
| (Some n, Some z, Some p) => [n; p; z] |
| 29 |
| (x, y, z) |
| 30 |
=> let f o := match o with Some v => [v] | None => [] end in |
| 31 |
List.flat_map f [x; y; z] |
| 32 |
end r. |
| 33 |
Proof. |
| 34 |
intros z Hz. |
| 35 |
exists (let '(a, b, c) := split_range_at_0 r in |
| 36 |
let extract o := match o with Some v => v | None => r[0~>0]%zrange end in |
| 37 |
if z <? 0 then extract a else if z =? 0 then extract b else extract c). |
| 38 |
destruct r as [l u]; |
| 39 |
repeat first [ progress cbv [andb orb is_bounded_by_bool goodb] in * |
| 40 |
| progress cbn in * |
| 41 |
| break_innermost_match_step |
| 42 |
| congruence |
| 43 |
| break_innermost_match_hyps_step |
| 44 |
| progress Z.ltb_to_lt |
| 45 |
| lia ]. |
| 46 |
all: split; [ | let rec tac := first [ left; reflexivity | right; tac ] in tac ]. |
| 47 |
all: Z.ltb_to_lt. |
| 48 |
all: lia. |
| 49 |
Qed. |
| 50 |
|
| 51 |
Lemma is_bounded_by_fold_left_union (f : Z -> Z) (fr : zrange -> zrange) z r r' rs |
| 52 |
(Hf : is_bounded_by_bool (f z) (fr r') = true) |
| 53 |
(Hr' : is_bounded_by_bool (f z) r = true \/ In r' rs) |
| 54 |
: is_bounded_by_bool (f z) (fold_left (fun x y : zrange => union x (fr y)) rs r) = true. |
| 55 |
Proof. |
| 56 |
revert dependent r; induction rs as [|r0 rs IH]; cbn -[union] in *; intros; |
| 57 |
[ now destruct Hr' | ]. |
| 58 |
apply IH; clear IH. |
| 59 |
destruct_head'_or; subst; first [ right; assumption | left ]. |
| 60 |
all: now (apply ZRange.is_bounded_by_bool_union_l + apply ZRange.is_bounded_by_bool_union_r). |
| 61 |
Qed. |
| 62 |
|
| 63 |
Lemma nary_is_bounded_by_under_args2_union {n f fr z rs r} |
| 64 |
(Hr : covers rs r) |
| 65 |
(Hf : @nary_is_bounded_by (S n) f fr) |
| 66 |
(Hzr : is_bounded_by_bool z r = true) |
| 67 |
: match List.map fr rs with |
| 68 |
| nil => False |
| 69 |
| r0 :: rs |
| 70 |
=> nary_is_bounded_by (f z) (List.fold_left (under_args2 union) rs r0) |
| 71 |
end. |
| 72 |
Proof. |
| 73 |
specialize (Hr _ Hzr). |
| 74 |
destruct Hr as [r' [Hr Hr']]. |
| 75 |
destruct rs as [|r0 rs]; [ exfalso; assumption | ]. |
| 76 |
cbn [List.map List.In] in *. |
| 77 |
cbn [nary_is_bounded_by] in *. |
| 78 |
specialize (Hf _ _ Hr). |
| 79 |
rewrite fold_left_map. |
| 80 |
induction n as [|n IHn]. |
| 81 |
{ cbn [nary_T nary_is_bounded_by under_args2] in *. |
| 82 |
eapply is_bounded_by_fold_left_union; |
| 83 |
[ | destruct Hr'; [ left | right; eassumption ]; subst ]; assumption. } |
| 84 |
{ cbn [under_args2 nary_is_bounded_by nary_T] in *; intros. |
| 85 |
rewrite_fold_left_fun_apply. |
| 86 |
apply (fun z0 r2 => IHn (fun z => f z z0) (fun r => fr r r2)); clear IHn. |
| 87 |
auto. } |
| 88 |
Qed. |
| 89 |
|
| 90 |
Lemma apply_to_n_split_range_is_bounded_by {n f fr} |
| 91 |
(Hf : @nary_is_bounded_by n f fr) |
| 92 |
: @nary_is_bounded_by n f (apply_to_n_split_range fr). |
| 93 |
Proof. |
| 94 |
induction n as [|n IHn]; [ assumption | ]. |
| 95 |
cbn [nary_is_bounded_by apply_to_n_split_range]. |
| 96 |
intros z r Hzr. |
| 97 |
apply IHn; clear IHn. |
| 98 |
pose proof (split_range_at_0_covers r ltac:(eapply ZRange.goodb_of_is_bounded_by_bool; eassumption)) as Hcover. |
| 99 |
cbv [apply_to_split_range_under_args]. |
| 100 |
break_innermost_match; |
| 101 |
pose proof (nary_is_bounded_by_under_args2_union Hcover Hf Hzr) as H. |
| 102 |
all: first [ exact H | exfalso; assumption ]. |
| 103 |
Qed. |
| 104 |
End ZRange. |