| 1 |
From Coq Require Import Morphisms. |
| 2 |
From Coq Require Import ZArith. |
| 3 |
From Coq Require Import Lia. |
| 4 |
Require Import Crypto.Util.Tuple. |
| 5 |
Require Import Crypto.Util.Decidable. |
| 6 |
Require Import Crypto.Util.Bool.Reflect. |
| 7 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 8 |
Require Import Crypto.Util.Notations. |
| 9 |
|
| 10 |
Declare Scope zrange_scope. |
| 11 |
Delimit Scope zrange_scope with zrange. |
| 12 |
Local Set Nonrecursive Elimination Schemes. |
| 13 |
(* COQBUG(https://github.com/coq/coq/issues/7835) *) |
| 14 |
(* |
| 15 |
Local Set Boolean Equality Schemes. |
| 16 |
Local Set Decidable Equality Schemes. |
| 17 |
*) |
| 18 |
Record zrange := { lower : Z ; upper : Z }. |
| 19 |
Bind Scope zrange_scope with zrange. |
| 20 |
Local Open Scope Z_scope. |
| 21 |
Scheme Minimality for zrange Sort Type. |
| 22 |
|
| 23 |
Definition ZToZRange (z : Z) : zrange := {| lower := z ; upper := z |}. |
| 24 |
|
| 25 |
Ltac inversion_zrange := |
| 26 |
let lower := (eval cbv [lower] in (fun x => lower x)) in |
| 27 |
let upper := (eval cbv [upper] in (fun y => upper y)) in |
| 28 |
repeat match goal with |
| 29 |
| [ H : _ = _ :> zrange |- _ ] |
| 30 |
=> pose proof (f_equal lower H); pose proof (f_equal upper H); clear H; |
| 31 |
cbv beta iota in * |
| 32 |
| [ H : Build_zrange _ _ = _ |- _ ] |
| 33 |
=> pose proof (f_equal lower H); pose proof (f_equal upper H); clear H; |
| 34 |
cbv beta iota in * |
| 35 |
| [ H : _ = Build_zrange _ _ |- _ ] |
| 36 |
=> pose proof (f_equal lower H); pose proof (f_equal upper H); clear H; |
| 37 |
cbv beta iota in * |
| 38 |
end. |
| 39 |
|
| 40 |
(** All of the boundedness properties take an optional bitwidth, and |
| 41 |
enforce the condition that the range is within 0 and 2^bitwidth, |
| 42 |
if given. *) |
| 43 |
Section with_bitwidth. |
| 44 |
Context (bitwidth : option Z). |
| 45 |
|
| 46 |
Definition is_bounded_by' : zrange -> Z -> Prop |
| 47 |
:= fun bound val |
| 48 |
=> lower bound <= val <= upper bound |
| 49 |
/\ match bitwidth with |
| 50 |
| Some sz => 0 <= lower bound /\ upper bound < 2^sz |
| 51 |
| None => True |
| 52 |
end. |
| 53 |
|
| 54 |
Definition is_bounded_by {n} : Tuple.tuple zrange n -> Tuple.tuple Z n -> Prop |
| 55 |
:= Tuple.fieldwise is_bounded_by'. |
| 56 |
|
| 57 |
Lemma is_bounded_by_repeat_In_iff {n} vs bound |
| 58 |
: is_bounded_by (Tuple.repeat bound n) vs <-> (forall x, List.In x (Tuple.to_list _ vs) -> is_bounded_by' bound x). |
| 59 |
Proof. apply fieldwise_In_to_list_repeat_l_iff. Qed. |
| 60 |
End with_bitwidth. |
| 61 |
|
| 62 |
Lemma is_bounded_by_None_repeat_In_iff {n} vs l u |
| 63 |
: is_bounded_by None (Tuple.repeat {| lower := l ; upper := u |} n) vs |
| 64 |
<-> (forall x, List.In x (Tuple.to_list _ vs) -> l <= x <= u). |
| 65 |
Proof. |
| 66 |
rewrite is_bounded_by_repeat_In_iff; unfold is_bounded_by'; simpl. |
| 67 |
split; intro H; intros; repeat split; apply H; assumption. |
| 68 |
Qed. |
| 69 |
|
| 70 |
Lemma is_bounded_by_None_repeat_In_iff_lt {n} vs l u |
| 71 |
: is_bounded_by None (Tuple.repeat {| lower := l ; upper := u - 1 |} n) vs |
| 72 |
<-> (forall x, List.In x (Tuple.to_list _ vs) -> l <= x < u). |
| 73 |
Proof. |
| 74 |
rewrite is_bounded_by_None_repeat_In_iff. |
| 75 |
split; intro H; (repeat let x := fresh in intro x; specialize (H x)); lia. |
| 76 |
Qed. |
| 77 |
|
| 78 |
Definition is_bounded_by_bool (v : Z) (x : zrange) : bool |
| 79 |
:= ((lower x <=? v) && (v <=? upper x))%bool%Z. |
| 80 |
|
| 81 |
Definition is_tighter_than_bool (x y : zrange) : bool |
| 82 |
:= ((lower y <=? lower x) && (upper x <=? upper y))%bool%Z. |
| 83 |
|
| 84 |
Global Instance dec_eq_zrange : DecidableRel (@eq zrange) | 10. |
| 85 |
Proof. |
| 86 |
intros [lx ux] [ly uy]. |
| 87 |
destruct (dec (lx = ly)), (dec (ux = uy)); |
| 88 |
[ left; apply f_equal2; assumption |
| 89 |
| abstract (right; intro H; inversion_zrange; tauto).. ]. |
| 90 |
Defined. |
| 91 |
|
| 92 |
Definition zrange_beq (x y : zrange) : bool |
| 93 |
:= (Z.eqb (lower x) (lower y) && Z.eqb (upper x) (upper y)). |
| 94 |
Lemma zrange_bl (x y : zrange) (H : zrange_beq x y = true) : x = y. |
| 95 |
Proof. |
| 96 |
cbv [zrange_beq] in *; rewrite Bool.andb_true_iff, !Z.eqb_eq in *. |
| 97 |
destruct x, y, H; simpl in *; subst; reflexivity. |
| 98 |
Qed. |
| 99 |
Lemma zrange_lb (x y : zrange) (H : x = y) : zrange_beq x y = true. |
| 100 |
Proof. |
| 101 |
cbv [zrange_beq] in *; rewrite Bool.andb_true_iff, !Z.eqb_eq in *. |
| 102 |
subst; split; reflexivity. |
| 103 |
Qed. |
| 104 |
|
| 105 |
Global Instance reflect_zrange_eq : reflect_rel (@eq zrange) zrange_beq | 10. |
| 106 |
Proof. apply reflect_of_beq; auto using zrange_bl, zrange_lb. Qed. |
| 107 |
|
| 108 |
Global Instance zrange_rect_Proper {P} |
| 109 |
: Proper (pointwise_relation _ (pointwise_relation _ eq) ==> eq ==> eq) (@zrange_rect (fun _ => P)) | 10. |
| 110 |
Proof. |
| 111 |
cbv [pointwise_relation zrange_rect]; repeat intro; subst; break_innermost_match; eauto. |
| 112 |
Qed. |
| 113 |
|
| 114 |
Global Instance zrange_rect_Proper_dep {P} |
| 115 |
: Proper (forall_relation (fun _ => forall_relation (fun _ => eq)) ==> forall_relation (fun _ => eq)) |
| 116 |
(@zrange_rect P) | 10. |
| 117 |
Proof. |
| 118 |
cbv [forall_relation zrange_rect]; repeat intro; subst; break_innermost_match; eauto. |
| 119 |
Qed. |
| 120 |
|
| 121 |
Module Export Notations. |
| 122 |
Declare Scope zrange_scope. |
| 123 |
Delimit Scope zrange_scope with zrange. |
| 124 |
Notation "r[ l ~> u ]" := {| lower := l ; upper := u |} : zrange_scope. |
| 125 |
Infix "<=?" := is_tighter_than_bool : zrange_scope. |
| 126 |
Infix "=?" := zrange_beq : zrange_scope. |
| 127 |
End Notations. |