Debian Codegraph @

src/Util/ZBounded.v

raw
1 From Coq Require Import Lia.
2 From Coq Require Import ZArith.
3 Require Import Crypto.Util.Tuple.
4 Require Import Crypto.Util.Decidable.
5 Require Import Crypto.Util.ZRange.
6 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
7 Require Import Crypto.Util.Bool.IsTrue.
8 Require Import Crypto.Util.Bool.
9 Require Import Crypto.Util.HProp.
10 Require Import Crypto.Util.Decidable.
11 Require Import Crypto.Util.Notations.
12
13 Declare Scope zbounded_scope.
14 Delimit Scope zbounded_scope with zbounded.
15 Local Open Scope Z_scope.
16 Local Set Primitive Projections.
17 Record zbounded {r : zrange}
18 := { value :> Z ;
19 value_bounded : is_true ((upper r <? lower r)
20 || ((lower r <=? value) && (value <=? upper r))) }.
21 Bind Scope zbounded_scope with zbounded.
22 Global Arguments Build_zbounded {r} value {_}, {r} value _, r value _.
23 Global Arguments zbounded r : clear implicits.
24
25 Definition adjust_zbounded {r} (v : zbounded r) : zbounded r
26 := {| value := value v ;
27 value_bounded := adjust_is_true (value_bounded v) |}.
28
29 (** ** Equality for [zbounded] *)
30 Section eq.
31 Definition value_path {r} {u v : zbounded r} (p : u = v)
32 : value u = value v
33 := f_equal (@value _) p.
34
35 Definition value_bounded_path {r} {u v : zbounded r} (p : u = v)
36 : eq_rect
37 _ (fun v => is_true (_ || ((lower r <=? v) && (v <=? upper r))))
38 (value_bounded u) _ (value_path p)
39 = value_bounded v.
40 Proof.
41 destruct p; reflexivity.
42 Defined.
43
44 Definition path_zbounded_full {r} (u v : zbounded r)
45 (p : value u = value v)
46 (q : eq_rect
47 _ (fun v => is_true (_ || ((lower r <=? v) && (v <=? upper r))))
48 (value_bounded u) _ p
49 = value_bounded v)
50 : u = v.
51 Proof.
52 destruct u as [u1 u2], v as [v1 v2]; simpl in *; subst v1 v2; reflexivity.
53 Defined.
54
55 Definition path_zbounded {r} (u v : zbounded r)
56 (p : value u = value v)
57 : u = v.
58 Proof.
59 apply (path_zbounded_full u v p).
60 destruct u as [u1 u2], v as [v1 v2]; simpl in *; subst v1; simpl.
61 let P := lazymatch type of u2 with is_true ?P => P end in
62 generalize dependent P.
63 compute; intros b p q; clear.
64 transitivity (adjust_is_true q); clear; subst; reflexivity.
65 Defined.
66
67 Definition path_zbounded_rect_full {r} {u v : zbounded r} (Q : u = v -> Type)
68 (f : forall p q, Q (path_zbounded_full u v p q))
69 : forall p, Q p.
70 Proof.
71 intro p; specialize (f (value_path p) (value_bounded_path p));
72 destruct u as [u0 u1], p; exact f.
73 Defined.
74 Definition path_zbounded_rec_full {r u v} (Q : u = v :> zbounded r -> Set) := path_zbounded_rect_full Q.
75 Definition path_zbounded_ind_full {r u v} (Q : u = v :> zbounded r -> Prop) := path_zbounded_rec_full Q.
76
77 Definition path_zbounded_rect {r} {u v : zbounded r} (Q : u = v -> Type)
78 (f : forall p, Q (path_zbounded u v p))
79 : forall p, Q p.
80 Proof.
81 intro p; specialize (f (value_path p));
82 destruct u as [u0 u1], p.
83 simpl in *.
84 generalize dependent (@Build_zbounded r u0); intros.
85 let P := lazymatch type of u1 with is_true ?P => P end in
86 generalize dependent P; intros.
87 unfold is_true in *.
88 subst.
89 exact f.
90 Defined.
91 Definition path_zbounded_rec {r u v} (Q : u = v :> zbounded r -> Set) := path_zbounded_rect Q.
92 Definition path_zbounded_ind {r u v} (Q : u = v :> zbounded r -> Prop) := path_zbounded_rec Q.
93
94 (** *** η Principle for [zbounded] *)
95 Definition sigT_eta {r} (p : zbounded r)
96 : p = {| value := value p ; value_bounded := value_bounded p |}
97 := eq_refl.
98 End eq.
99
100 Ltac induction_zbounded_in_as_using H H0 H1 rect :=
101 induction H as [H0 H1] using (rect _ _ _);
102 cbn [value value_bounded] in H0, H1.
103 Ltac induction_zbounded_in_using H rect :=
104 let H0 := fresh H in
105 let H1 := fresh H in
106 induction_zbounded_in_as_using H H0 H1 rect.
107 Ltac inversion_zbounded_step :=
108 match goal with
109 | [ H : _ = @Build_zbounded _ _ _ |- _ ]
110 => induction_zbounded_in_using H @path_zbounded_rect_full
111 | [ H : @Build_zbounded _ _ _ = _ |- _ ]
112 => induction_zbounded_in_using H @path_zbounded_rect_full
113 end.
114 Ltac inversion_zbounded := repeat inversion_zbounded_step.
115
116 Lemma is_bounded_by'_zbounded {r} (v : zbounded r) : lower r <= upper r -> is_bounded_by' None r v.
117 Proof.
118 destruct v as [v H]; cbv [is_bounded_by']; simpl.
119 apply Bool.orb_true_iff in H; destruct H; split_andb; Z.ltb_to_lt; lia.
120 Qed.
121
122 Global Instance dec_eq_zrange {r} : DecidableRel (@eq (zbounded r)) | 10.
123 Proof.
124 intros [x ?] [y ?].
125 destruct (dec (x = y));
126 [ left; apply path_zbounded; assumption
127 | abstract (right; intro H; inversion_zbounded; tauto) ].
128 Defined.
129
130 Definition modulo (z : Z) (r : zrange) : zbounded r.
131 Proof.
132 refine {| value := if (upper r <? lower r)
133 then z
134 else lower r + Z.modulo (z - lower r) (upper r - lower r + 1) |}.
135 abstract (
136 destruct (upper r <? lower r) eqn:H; [ reflexivity | ];
137 pose proof (Z.mod_pos_bound (z - lower r) (upper r - lower r + 1)) as Hmod;
138 destruct r as [l u];
139 simpl in *; apply Bool.andb_true_iff; split;
140 destruct (l =? u) eqn:H';
141 Z.ltb_to_lt; subst;
142 repeat rewrite ?Z.sub_diag, ?Zmod_0_r, ?Z.add_0_r, ?Z.add_0_l, ?Z.mod_1_r;
143 try reflexivity; try lia
144 ).
145 Defined.
146
147 Lemma modulo_value_id {r} (v : zbounded r) : modulo (value v) r = v.
148 Proof.
149 apply path_zbounded; simpl.
150 destruct v as [v H]; simpl.
151 destruct (upper r <? lower r) eqn:H'; [ reflexivity | ].
152 simpl in *; unfold is_true in *; split_andb; Z.ltb_to_lt.
153 rewrite Z.mod_small by lia.
154 lia.
155 Qed.
156
157 Lemma value_modulo_in_range z r (Hr : lower r <= z <= upper r)
158 : value (modulo z r) = z.
159 Proof.
160 simpl.
161 destruct (upper r <? lower r) eqn:H'; [ reflexivity | ].
162 rewrite Z.mod_small by lia.
163 lia.
164 Qed.