Debian Codegraph @

src/AbstractInterpretation/ZRangeProofs.v

raw
1 From Coq Require Import Lia.
2 From Coq Require Import ZArith.
3 From Coq Require Import Morphisms.
4 From Coq Require Import RelationPairs.
5 From Coq Require Import Relations.
6 From Coq Require Import List.
7 Require Import Crypto.Util.ZRange.
8 Require Import Crypto.Util.ZRange.Operations.
9 Require Import Crypto.Util.ZRange.BasicLemmas.
10 Require Import Crypto.Util.ZRange.SplitBounds.
11 Require Import Crypto.Util.ZRange.CornersMonotoneBounds.
12 Require Import Crypto.Util.ZRange.LandLorBounds.
13 Require Import Crypto.Util.Sum.
14 Require Import Crypto.Util.LetIn.
15 Require Import Crypto.Util.Prod.
16 Require Import Crypto.Util.Sigma.
17 Require Import Crypto.Util.Option.
18 Require Import Crypto.Util.ListUtil.
19 Require Import Crypto.Util.ListUtil.FoldBool.
20 Require Import Crypto.Util.NatUtil.
21 Require Import Crypto.Util.ZUtil.AddGetCarry.
22 Require Import Crypto.Util.ZUtil.AddModulo.
23 Require Import Crypto.Util.ZUtil.CC.
24 Require Import Crypto.Util.ZUtil.MulSplit.
25 Require Import Crypto.Util.ZUtil.Rshi.
26 Require Import Crypto.Util.ZUtil.TruncatingShiftl.
27 Require Import Crypto.Util.ZUtil.Zselect.
28 Require Import Crypto.Util.ZUtil.Combine.
29 Require Import Crypto.Util.ZUtil.Le.
30 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
31 Require Import Crypto.Util.ZUtil.Tactics.SplitMinMax.
32 Require Import Crypto.Util.ZUtil.Tactics.ReplaceNegWithPos.
33 Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem.
34 Require Import Crypto.Util.ZUtil.Morphisms.
35 Require Import Crypto.Util.HProp.
36 Require Import Crypto.Util.Bool.Reflect.
37 Require Import Crypto.Util.Tactics.BreakMatch.
38 Require Import Crypto.Util.Tactics.DestructHead.
39 Require Import Crypto.Util.Tactics.SplitInContext.
40 Require Import Crypto.Util.Tactics.UniquePose.
41 Require Import Crypto.Util.Tactics.SpecializeBy.
42 Require Import Crypto.Util.Tactics.SpecializeAllWays.
43 Require Import Crypto.Util.Tactics.Head.
44 Require Import Crypto.Util.Tactics.PrintGoal.
45 Require Import Crypto.Util.Tactics.DoWithHyp.
46 Require Import Crypto.Language.PreExtra.
47 Require Import Crypto.CastLemmas.
48 Require Import Crypto.AbstractInterpretation.ZRange.
49 Require Import Crypto.AbstractInterpretation.ZRangeCommonProofs.
50 Import ListNotations. Import EqNotations.
51
52 Module Compilers.
53 Import AbstractInterpretation.ZRange.Compilers.
54 Import AbstractInterpretation.ZRangeCommonProofs.Compilers.
55 Import Rewriter.Language.Wf.Compilers. (* for properties about type.related_hetero *)
56 Export ZRange.Settings.
57
58 Module ZRange.
59 Module type.
60 Module base.
61 Module option.
62 Lemma is_bounded_by_None t v : ZRange.type.base.option.is_bounded_by (@ZRange.type.base.option.None t) v = true.
63 Proof. induction t; cbn; cbv [andb]; break_innermost_match; eauto. Qed.
64
65 Lemma tighter_than_union t (rx ry : ZRange.type.base.option.interp t) :
66 ZRange.type.base.option.is_tighter_than rx (ZRange.type.base.option.union rx ry) = true /\
67 ZRange.type.base.option.is_tighter_than ry (ZRange.type.base.option.union rx ry) = true.
68 Proof.
69 induction t; [destruct t|..]; destruct rx, ry;
70 cbn [
71 ZRange.type.base.interp
72 ZRange.type.base.option.interp
73 ZRange.type.base.option.is_tighter_than
74 ZRange.type.base.is_tighter_than
75 ZRange.type.base.option.union
76 Option.map2
77 Option.bind2
78 ] in *; auto 2.
79 all: fold (@ZRange.type.base.option.interp) in *.
80 all: break_innermost_match; reflect_hyps; inversion_option; subst; auto 2.
81 all: split_and.
82 all: cbn [option_beq].
83 all: rewrite ?Bool.andb_true_iff; auto.
84 all: try now split; first [ apply ZRange.is_tighter_than_bool_union_l | apply ZRange.is_tighter_than_bool_union_r ].
85 all: do 2 try (match goal with
86 | [ |- context[?x = true] ]
87 => lazymatch x with true => fail | false => fail | _ => idtac end;
88 destruct x eqn:?; progress reflect_hyps
89 end;
90 cbn [ZRange.type.base.option.None] in *;
91 auto 2; subst; try congruence).
92 { rewrite 2 fold_andb_map_map.
93 rewrite 2 fold_andb_map_iff.
94 rewrite List.combine_length.
95 do_with_hyp' ltac:(fun H => rewrite H).
96 rewrite Nat.min_id.
97 repeat split; intros *; destruct_head'_prod; cbn [uncurry fst snd].
98 all: rewrite In_nth_error_iff.
99 all: intros [n H']; revert H'.
100 all: rewrite !nth_error_combine.
101 all: break_innermost_match; intros; inversion_option; inversion_pair; subst; auto. }
102 Qed.
103
104 Lemma is_bounded_by_union_l t (rx ry : ZRange.type.base.option.interp t) x :
105 ZRange.type.base.option.is_bounded_by rx x = true ->
106 ZRange.type.base.option.is_bounded_by (ZRange.type.base.option.union rx ry) x = true.
107 Proof.
108 eapply ZRange.type.base.option.is_tighter_than_is_bounded_by; try eassumption;
109 eapply tighter_than_union.
110 Qed.
111
112 Lemma is_bounded_by_union_r t (rx ry : ZRange.type.base.option.interp t) y :
113 ZRange.type.base.option.is_bounded_by ry y = true ->
114 ZRange.type.base.option.is_bounded_by (ZRange.type.base.option.union rx ry) y = true.
115 Proof.
116 eapply ZRange.type.base.option.is_tighter_than_is_bounded_by; try eassumption;
117 eapply tighter_than_union.
118 Qed.
119 End option.
120 End base.
121
122 Module option.
123 Lemma app_curried_None {t args} : type.app_curried (@ZRange.type.option.None t) args = ZRange.type.base.option.None.
124 Proof. induction t; cbn; auto. Qed.
125
126 Lemma is_bounded_by_impl_related_hetero t
127 (x : ZRange.type.option.interp t) (v : type.interp base.interp t)
128 : ZRange.type.option.is_bounded_by x v = true
129 -> type.related_hetero (fun t x v => ZRange.type.base.option.is_bounded_by x v = true) x v.
130 Proof. induction t; cbn in *; intuition congruence. Qed.
131
132 Lemma is_bounded_by_impl_related_hetero_and_Proper {skip_base} t
133 (x : ZRange.type.option.interp t) (v : type.interp base.interp t)
134 : ZRange.type.option.is_bounded_by x v = true
135 -> type.related_hetero_and_Proper (skip_base:=skip_base) (fun _ => eq) (fun _ => eq) (fun t x v => ZRange.type.base.option.is_bounded_by x v = true) x v.
136 Proof. induction t; cbn in *; break_innermost_match; intuition congruence. Qed.
137
138 Lemma is_bounded_by_impl_eqv_refl t
139 (x : ZRange.type.option.interp t) (v : type.interp base.interp t)
140 : ZRange.type.option.is_bounded_by x v = true
141 -> x == x /\ v == v.
142 Proof. induction t; cbn; split; try reflexivity; try congruence. Qed.
143
144 Lemma is_bounded_by_impl_eqv_refl1 t
145 (x : ZRange.type.option.interp t) (v : type.interp base.interp t)
146 : ZRange.type.option.is_bounded_by x v = true
147 -> x == x.
148 Proof. intros; eapply is_bounded_by_impl_eqv_refl; eassumption. Qed.
149
150 Lemma is_bounded_by_impl_eqv_refl2 t
151 (x : ZRange.type.option.interp t) (v : type.interp base.interp t)
152 : ZRange.type.option.is_bounded_by x v = true
153 -> v == v.
154 Proof. intros; eapply is_bounded_by_impl_eqv_refl; eassumption. Qed.
155
156 Lemma andb_bool_for_each_lhs_of_arrow_is_bounded_by_impl_and_for_each_lhs_of_arrow_eqv_refl t
157 (x : type.for_each_lhs_of_arrow ZRange.type.option.interp t) (v : type.for_each_lhs_of_arrow (type.interp base.interp) t)
158 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) x v = true
159 -> type.and_for_each_lhs_of_arrow (@type.eqv) v v.
160 Proof.
161 induction t; cbn; [ reflexivity | ].
162 rewrite Bool.andb_true_iff.
163 intros [H0 H1].
164 split; eauto using is_bounded_by_impl_eqv_refl2.
165 Qed.
166 End option.
167 End type.
168
169 Module ident.
170 Module option.
171 (** First we prove relatedness for some particularly complicated identifiers separately *)
172 Section interp_related.
173 Context {shiftr_avoid_uint1 : shiftr_avoid_uint1_opt}
174 (assume_cast_truncates : bool).
175 Local Notation interp_is_related idc
176 := (type.related_hetero
177 (fun t st v => ZRange.type.base.option.is_bounded_by st v = true)
178 (ZRange.ident.option.interp assume_cast_truncates idc)
179 (ident.interp idc)).
180 Local Notation interp_is_related_and_Proper idc
181 := (type.related_hetero_and_Proper
182 (skip_base:=true)
183 (fun t => eq)
184 (fun t => eq)
185 (fun t st v => ZRange.type.base.option.is_bounded_by st v = true)
186 (ZRange.ident.option.interp assume_cast_truncates idc)
187 (ident.interp idc)).
188
189 Local Ltac z_cast_t :=
190 cbn [type.related_hetero ZRange.ident.option.interp ident.interp respectful_hetero type.interp ZRange.type.base.option.interp ZRange.type.base.interp base.interp base.base_interp ZRange.type.base.option.Some];
191 cbv [ZRange.ident.option.interp_Z_cast ZRange.ident.option.interp_Z_cast_truncate ZRange.type.base.option.is_bounded_by ZRange.type.base.is_bounded_by respectful_hetero];
192 cbn [base.interp_beq base.base_interp_beq option_map] in *;
193 cbv [ident.cast2] in *; cbn [fst snd] in *;
194 intros; break_innermost_match; break_innermost_match_hyps; trivial;
195 cbn [option_map] in *; inversion_option;
196 rewrite ?Bool.andb_true_iff, ?Bool.andb_false_iff in *; destruct_head'_and; destruct_head'_or; repeat apply conj; Z.ltb_to_lt;
197 reflect_beq_to_eq zrange_beq; subst;
198 rewrite ?ident.cast_in_bounds by (eapply ZRange.is_bounded_by_iff_is_tighter_than; eauto);
199 try reflexivity; try lia; try assumption;
200 auto using ident.cast_always_bounded.
201
202 Lemma interp_related_Z_cast : interp_is_related ident.Z_cast.
203 Proof using Type. z_cast_t. Qed.
204
205 Lemma interp_related_Z_cast2 : interp_is_related ident.Z_cast2.
206 Proof using Type. z_cast_t. Qed.
207
208 Lemma interp_related_List_flat_map A B : interp_is_related (@ident.List_flat_map A B).
209 Proof using Type.
210 cbn; cbv [respectful_hetero]; intros.
211 destruct_head' option; cbn in *; [ | reflexivity ].
212 break_match; cbn in *; [ | reflexivity ].
213 rewrite fold_andb_map_iff in *.
214 rewrite OptionList.fold_right_option_seq in *.
215 destruct_head'_ex.
216 destruct_head'_and.
217 rewrite List.flat_map_concat_map, concat_fold_right_app.
218 subst.
219 lazymatch goal with
220 | [ H : List.map _ ?ls1 = List.map _ ?ls' |- length (List.fold_right _ _ ?ls1) = length (List.fold_right _ _ (List.map _ ?ls2)) /\ _ ]
221 => is_var ls1; is_var ls2; is_var ls';
222 revert dependent ls'; intro ls'; revert dependent ls2; revert ls'; induction ls1 as [|? xs IHxs];
223 intros [|? ls'] [|? ls2]; cbn [length] in *; intros; cbn in *;
224 [ tauto | exfalso; discriminate.. | ]
225 end.
226 repeat first [ progress cbn in *
227 | progress subst
228 | discriminate
229 | congruence
230 | progress autorewrite with distr_length
231 | progress destruct_head'_and
232 | rewrite combine_app_samelength
233 | rewrite fold_andb_map_iff in *
234 | specialize (IHxs _ _ ltac:(eassumption) ltac:(eassumption) ltac:(eassumption))
235 | match goal with
236 | [ H : cons _ _ = cons _ _ |- _ ] => inversion H; clear H
237 | [ H : S _ = S _ |- _ ] => inversion H; clear H
238 | [ H : forall v, _ = v \/ _ -> _ |- _ ] => pose proof (H _ (or_introl eq_refl)); specialize (fun v pf => H v (or_intror pf))
239 | [ H : forall x y, ?R x y = true -> match ?f x with Some _ => _ | None => _ end = true, H' : Some _ = ?f ?x' |- _ ]
240 => specialize (H _ _ ltac:(eassumption)); rewrite <- H' in H
241 | [ |- context[List.In _ (_ ++ _)] ] => setoid_rewrite List.in_app_iff
242 end
243 | solve [ intuition ] ].
244 Qed.
245
246 Lemma interp_related_List_partition A : interp_is_related (@ident.List_partition A).
247 Proof using Type.
248 cbn; cbv [respectful_hetero]; intros.
249 destruct_head' option; cbn in *; [ | break_innermost_match; reflexivity ].
250 rewrite fold_andb_map_iff in *.
251 destruct_head'_ex.
252 destruct_head'_and.
253 repeat break_match_step ltac:(fun v => let t := type of v in match (eval hnf in t) with prod _ _ => idtac end).
254 repeat match goal with H : option (list _) |- _ => revert dependent H end.
255 lazymatch goal with
256 | [ Heq : List.partition _ _ = (?l0, ?l1), H : length ?ls1 = length ?ls2 |- _ ]
257 => is_var ls1; is_var ls2; is_var l0; is_var l1;
258 revert dependent l0; intro l0; revert dependent l1; intro l1;
259 revert dependent ls2; intro ls2;
260 revert ls2 l0 l1;
261 induction ls1 as [|? xs IHxs]; intros [|? ls2]; cbn [length] in *; cbn in *;
262 [ intros [|? ?] [|? ?]; cbn in *; intros; break_innermost_match; cbn in *;
263 inversion_prod; inversion_option; subst..
264 | ];
265 [ tauto | exfalso; discriminate.. | intros ]
266 end.
267 repeat first [ progress cbn in *
268 | progress subst
269 | match goal with
270 | [ H : S _ = S _ |- _ ] => inversion H; clear H
271 end
272 | discriminate
273 | congruence
274 | progress destruct_head'_and
275 | progress inversion_prod
276 | progress eta_expand
277 | rewrite Bool.if_const
278 | specialize (fun l0 l1 => IHxs _ l0 l1 ltac:(eassumption))
279 | specialize (fun l0 l1 => IHxs l0 l1 ltac:(eassumption))
280 | specialize (IHxs _ _ (@surjective_pairing _ _ _))
281 | progress Bool.split_andb
282 | match goal with
283 | [ |- context[(_ <- ?x; _)%option] ] => destruct x eqn:?
284 | [ H : bool_eq _ _ = true |- _ ] => apply bool_eq_ok in H
285 | [ H : forall v, _ = v \/ _ -> _ |- _ ] => pose proof (H _ (or_introl eq_refl)); specialize (fun v pf => H v (or_intror pf))
286 | [ |- context[match ?b with true => ?f ?x | false => ?f ?y end] ]
287 => replace (match b with true => f x | false => f y end)
288 with (f match b with true => x | false => y end)
289 by now case b
290 | [ H : context[match ?b with true => ?f ?x | false => ?f ?y end] |- _ ]
291 => replace (match b with true => f x | false => f y end)
292 with (f match b with true => x | false => y end)
293 in H
294 by now case b
295 | [ H : _ = (_, _) -> _ |- _ ] => specialize (fun pf1 pf2 => H (@path_prod _ _ _ _ pf1 pf2))
296 | [ H : ?x = _, H' : context[?x] |- _ ] => rewrite H in H'
297 | [ H : ?x = _ :> bool |- context[?x] ] => rewrite H
298 | [ H : forall x y, ?R x y = true -> match ?f x with Some _ => _ | None => _ end _ = true, H' : ?f ?x' = Some _ |- _ ]
299 => specialize (H _ _ ltac:(eassumption)); rewrite H' in H
300 end
301 | progress break_innermost_match_step ].
302 Qed.
303
304 Lemma interp_related_List_filter A : interp_is_related (@ident.List_filter A).
305 Proof using Type.
306 cbn; cbv [respectful_hetero]. intros f g H0 l1 l2 H1.
307 destruct l1 as [l1|]; try reflexivity.
308 destruct (list_rect _ _) as [l3 |] eqn:E; try reflexivity.
309 rewrite fold_andb_map_iff in *. destruct H1 as [H1 H2].
310 cbv [list_rect] in E. generalize dependent l2. generalize dependent l3.
311 induction l1 as [|a l1' IHl1'].
312 - intros l3 E l2 H1 H2. injection E as E. subst. simpl. simpl in H1.
313 destruct l2 as [|l2_0 l2']; try discriminate H1. constructor.
314 + reflexivity.
315 + intros v H. destruct H.
316 - intros l3 E l2 H1 H2. cbv [bind] in E.
317 destruct (f a) as [ [|]|] eqn:E'; try discriminate E.
318 + destruct (_ l1') as [v|] eqn:E''; try discriminate E.
319 destruct l3 as [| a' l3']; try discriminate E.
320 injection E as E1 E2. subst. destruct l2 as [| a l2']; try discriminate H1.
321 injection H1 as H1. assert (Htemp: Some l3' = Some l3') by reflexivity.
322 simpl in H2. remember (IHl1' l3' Htemp l2' H1) as IHl1'' eqn:clearMe.
323 clear IHl1' clearMe. assert (IHhyp: forall v, List.In v (List.combine l1' l2') ->
324 ZRange.type.base.option.is_bounded_by (fst v) (snd v) = true).
325 { intros v H. apply H2. right. apply H. }
326 apply IHl1'' in IHhyp. clear IHl1''. destruct IHhyp as [IH1 IH2].
327 assert (H2': ZRange.type.base.option.is_bounded_by a' a = true).
328 { apply (H2 (pair a' a)). simpl. left. reflexivity. }
329 clear H2. remember (H0 _ _ H2') as H0' eqn:clearMe. clear H0 clearMe.
330 destruct (f a') as [r|]; try discriminate E'. injection E' as E'. subst.
331 simpl in H0'. destruct (g a) as [|] eqn:E'''; try discriminate H0'.
332 clear H0'. simpl. rewrite E'''. simpl. constructor.
333 -- f_equal. assumption.
334 -- intros v [H|H].
335 ++ subst. simpl. assumption.
336 ++ apply IH2. assumption.
337 + destruct (_ l1') as [v|] eqn:E''; try discriminate E.
338 injection E as E. subst. destruct l2 as [| a' l2']; try discriminate H1.
339 injection H1 as H1. assert (Htemp: Some l3 = Some l3) by reflexivity.
340 simpl in H2. remember (IHl1' l3 Htemp l2' H1) as IHl1'' eqn:clearMe.
341 clear IHl1' clearMe. assert (IHhyp: forall v, List.In v (List.combine l1' l2') ->
342 ZRange.type.base.option.is_bounded_by (fst v) (snd v) = true).
343 { intros v H. apply H2. right. apply H. }
344 apply IHl1'' in IHhyp. clear IHl1''. destruct IHhyp as [IH1 IH2].
345 assert (H2': ZRange.type.base.option.is_bounded_by a a' = true).
346 { apply (H2 (pair a a')). simpl. left. reflexivity. }
347 clear H2. remember (H0 _ _ H2') as H0' eqn:clearMe. clear H0 clearMe.
348 destruct (f a) as [r|]; try discriminate E'. injection E' as E'. subst.
349 simpl in H0'. destruct (g a') as [|] eqn:E'''; try discriminate H0'.
350 clear H0'. simpl. rewrite E'''. constructor.
351 -- f_equal. assumption.
352 -- intros v H. apply IH2. assumption.
353 Qed.
354
355 Local Ltac handle_lt_le_t_step_fast :=
356 first [ match goal with
357 | [ H : (?a <= ?b)%Z, H' : (?b <= ?a)%Z |- _ ]
358 => pose proof (@Z.le_antisymm _ _ H H'); clear H H'
359 | [ H : (?a <= ?b <= ?a)%Z |- _ ]
360 => pose proof (@Z.le_antisymm _ _ (proj1 H) (proj2 H)); clear H
361 end ].
362
363 Local Ltac handle_lt_le_t_step :=
364 first [ handle_lt_le_t_step_fast
365 | match goal with
366 | [ |- context[Z.leb ?a ?b = true] ] => rewrite !Z.leb_le
367 | [ |- context[Z.ltb ?a ?b = true] ] => rewrite !Z.ltb_lt
368 | [ |- context[Nat.eqb ?a ?b = true] ] => rewrite !Nat.eqb_eq
369 | [ |- context[Z.eqb ?a ?b = true] ] => rewrite !Z.eqb_eq
370 | [ H : context[andb ?a ?b = true] |- _ ] => rewrite !Bool.andb_true_iff in H
371 | [ H : context[Z.leb ?a ?b = true] |- _ ] => rewrite !Z.leb_le in H
372 | [ H : context[Z.ltb ?a ?b = true] |- _ ] => rewrite !Z.ltb_lt in H
373 | [ H : context[Nat.eqb ?a ?b = true] |- _ ] => rewrite !Nat.eqb_eq in H
374 | [ H : context[Z.eqb ?a ?b = true] |- _ ] => rewrite !Z.eqb_eq in H
375 end ].
376
377 Local Ltac handle_mod_bounds_t_step_fast :=
378 first [ match goal with
379 | [ |- (0 <= _ mod _)%Z ] => apply Z.mod_pos_bound
380 | [ |- (_ mod ?m < ?m)%Z ] => apply Z.mod_pos_bound
381 | [ |- (?x mod ?m <= ?m - 1)%Z ] => cut (x mod m < m)%Z; [ clear; lia | ]
382 end ].
383
384 Local Ltac simplify_ranges_t_step_fast :=
385 first [ match goal with
386 | [ H : ZRange.lower ?r = ZRange.upper ?r |- _ ]
387 => is_var r; let x := fresh in destruct r as [r x]; cbn in H; subst x
388 | [ H : context[ZRange.type.base.is_bounded_by r[_ ~> _]%zrange _] |- _ ]
389 => progress cbn [ZRange.type.base.is_bounded_by] in H
390 | [ H : context[is_bounded_by_bool _ r[_ ~> _]%zrange] |- _ ]
391 => progress cbv [is_bounded_by_bool] in H
392 end ].
393 Local Ltac simplify_ranges_t_step :=
394 first [ simplify_ranges_t_step_fast ].
395
396 Local Ltac non_arith_t :=
397 repeat first [ assumption
398 | match goal with
399 | [ |- ?R ?x ?x ] => reflexivity
400 | [ H : forall x : unit, _ |- _ ] => specialize (H tt)
401 | [ H : ?x = ?x -> _ |- _ ] => specialize (H eq_refl)
402 | [ H : ?x = ?x |- _ ] => clear H
403 | [ H : false = true |- _ ] => exfalso; clear -H; discriminate
404 | [ H : ~?R ?x ?x |- _ ] => exfalso; apply H; reflexivity
405 | [ |- ZRange.type.base.option.is_bounded_by ZRange.type.base.option.None _ = true ]
406 => apply type.base.option.is_bounded_by_None
407 | [ H : fold_andb_map _ ?l1 ?l2 = true |- length ?l1 = length ?l2 ]
408 => eapply fold_andb_map_length, H
409 | [ H : forall x y, Nat.eqb x y = true -> _ |- _ ] => specialize (fun x => H x x (@Nat.eqb_refl x))
410 | [ H : forall x, true = true -> _ |- _ ] => specialize (fun x => H x eq_refl)
411 | [ H : forall v, List.In v (List.combine ?ls1 ?ls2) -> ?R (fst v) (snd v) = true,
412 H1 : List.nth_error ?ls1 ?n = Some ?v1,
413 H2 : List.nth_error ?ls2 ?n = Some ?v2
414 |- ?R ?v1 ?v2 = true ]
415 => apply (H (v1, v2)), (@nth_error_In _ _ n); clear -H1 H2
416 | [ H : ?T, H' : ~?T |- _ ] => exfalso; solve [ apply H', H ]
417 | [ H : List.nth_error ?ls ?n = Some _, H' : List.nth_error ?ls' ?n = None |- _ ]
418 => let Hlen := fresh in
419 assert (Hlen : length ls = length ls') by congruence;
420 exfalso; clear -H H' Hlen;
421 apply nth_error_value_length in H;
422 apply nth_error_error_length in H';
423 lia
424 | [ |- (0 <= 1)%Z ] => clear; lia
425 | [ H : ?beq ?x ?y = true |- ?x = ?y ]
426 => progress reflect_beq_to_eq beq
427 | [ |- ?beq ?x ?x = true ]
428 => progress reflect_beq_to_eq beq
429 end
430 | handle_lt_le_t_step_fast
431 | simplify_ranges_t_step_fast
432 | handle_mod_bounds_t_step_fast
433 | progress intros
434 | progress subst
435 | progress inversion_option
436 | progress inversion_prod
437 | progress destruct_head'_and
438 | progress destruct_head'_unit
439 | progress destruct_head'_prod
440 | progress destruct_head'_ex
441 | break_innermost_match_step
442 | break_innermost_match_hyps_step
443 | progress cbn [id
444 ZRange.type.base.option.is_bounded_by is_bounded_by_bool ZRange.type.base.is_bounded_by lower upper fst snd projT1 projT2 bool_eq base.interp base.base_interp Crypto.Util.Option.bind fold_andb_map negb ZRange.ident.option.to_literal ZRange.type.base.option.None fst snd ZRange.type.base.option.interp ZRange.type.base.interp List.combine List.In base.interp_beq base.base_interp_beq base.base_interp ZRange.type.option.None] in *
445 | progress ident.fancy.cbv_fancy_in_all
446 | progress destruct_head'_bool
447 | solve [ auto with nocore ]
448 | progress fold (@base.interp) in *
449 | progress fold (@ZRange.type.base.option.interp) in *
450 | let v := (eval cbv [base.interp ZRange.type.base.option.interp ZRange.type.base.interp] in (@ZRange.type.base.option.interp)) in
451 progress change v with (@ZRange.type.base.option.interp) in *
452 | handle_lt_le_t_step
453 | simplify_ranges_t_step
454 | match goal with
455 | [ |- context[andb ?a ?b = true] ] => rewrite !Bool.andb_true_iff
456 | [ H : context[andb ?a ?b = true] |- _ ] => rewrite !Bool.andb_true_iff
457 | [ H : fold_andb_map _ _ _ = true |- _ ] => rewrite fold_andb_map_iff in H
458 | [ |- fold_andb_map _ _ _ = true ] => rewrite fold_andb_map_iff
459 | [ H : forall (x : option _), _ |- _ ] => pose proof (H None); specialize (fun x => H (Some x))
460 | [ H : forall x y z (w : option _), _ |- _ ] => pose proof (fun x y z => H x y z None); specialize (fun x y z w => H x y z (Some w))
461 | [ H : forall v, _ = v \/ _ -> _ |- _ ] => pose proof (H _ (or_introl eq_refl)); specialize (fun v pf => H v (or_intror pf))
462 | [ |- context[length ?x] ] => tryif is_var x then fail else (progress autorewrite with distr_length)
463 | [ H : List.In _ (List.combine _ _) |- _ ] => apply List.In_nth_error in H
464 | [ H : context[List.nth_error (List.combine _ _) _] |- _ ] => rewrite nth_error_combine in H
465 | [ |- context[List.nth_error (List.combine _ _) _] ] => rewrite nth_error_combine
466 | [ H : List.nth_error (List.map _ _) _ = Some _ |- _ ] => apply nth_error_map_ex in H
467 | [ H : context[List.nth_error (List.seq _ _) _] |- _ ] => rewrite nth_error_seq in H
468 | [ |- context[List.nth_error (List.seq _ _) _] ] => rewrite nth_error_seq
469 | [ H : context[List.nth_error (List.firstn _ _) _] |- _ ] => rewrite nth_error_firstn in H
470 | [ |- context[List.nth_error (List.firstn _ _) _] ] => rewrite nth_error_firstn
471 | [ H : context[List.nth_error (List.skipn _ _) _] |- _ ] => rewrite nth_error_skipn in H
472 | [ |- context[List.nth_error (List.skipn _ _) _] ] => rewrite nth_error_skipn
473 | [ H : context[List.nth_error (update_nth _ _ _) _] |- _ ] => rewrite nth_update_nth in H
474 | [ |- context[List.nth_error (update_nth _ _ _) _] ] => rewrite nth_update_nth
475 | [ H : context[List.nth_error (List.app _ _) _] |- _ ] => rewrite nth_error_app in H
476 | [ |- context[List.nth_error (List.app _ _) _] ] => rewrite nth_error_app
477 | [ H : context[List.nth_error (List.rev _) _] |- _ ] => rewrite nth_error_rev in H
478 | [ |- context[List.nth_error (List.rev _) _] ] => rewrite nth_error_rev
479 | [ H : List.nth_error (Lists.List.repeat _ _) _ = Some _ |- _ ] => apply nth_error_repeat in H
480 | [ H : List.nth_error (List.repeat _ _) _ = Some _ |- _ ] => apply nth_error_repeat in H
481 | [ H : length ?l1 = length ?l2 |- context[length ?l1] ] => rewrite H
482 | [ H : length ?l1 = length ?l2, H' : context[length ?l1] |- _ ] => rewrite H in H'
483 | [ |- context[List.flat_map _ _] ] => rewrite List.flat_map_concat_map, concat_fold_right_app
484 | [ H : S _ = S _ |- _ ] => inversion H; clear H
485 | [ H : List.fold_right (fun x y => x' <- x; y' <- y; Some _)%option (Some ?init) ?ls = Some ?v |- _]
486 => rewrite OptionList.fold_right_option_seq in H
487 | [ |- and _ _ ] => apply conj
488 end
489 | progress cbv [bool_eq Bool.eqb option_map List.nth_default Definitions.Z.bneg is_bounded_by_bool zrange_beq] in *
490 | let rec do_rect_head lhs rhs k :=
491 lazymatch lhs with
492 | nat_rect ?P ?O ?S ?n
493 => lazymatch rhs with
494 | nat_rect ?P' ?O' ?S' n
495 => is_var n;
496 k ();
497 induction n; cbn [nat_rect];
498 generalize dependent (nat_rect P O S); generalize dependent (nat_rect P' O' S');
499 intros
500 end
501 | list_rect ?P ?N ?C ?ls
502 => lazymatch rhs with
503 | list_rect ?P' ?N' ?C' ?ls'
504 => lazymatch goal with
505 | [ H : length ls = length ls' |- _ ]
506 => is_var ls; is_var ls'; k ();
507 let IH := fresh "IH" in
508 (revert dependent ls'; induction ls as [|? ls IH]; intros [|? ls']; intros; cbn [list_rect length] in * );
509 [
510 | exfalso; discriminate | exfalso; discriminate
511 | specialize (IH ls');
512 generalize dependent (list_rect P N C); generalize dependent (list_rect P' N' C') ];
513 intros
514 end
515 end
516 | ?f ?x
517 => lazymatch rhs with
518 | ?g ?y
519 => is_var x; is_var y;
520 do_rect_head f g ltac:(fun _ => k (); revert dependent x; try revert dependent y)
521 end
522 end in
523 lazymatch goal with
524 | [ |- ?R ?lhs ?rhs = true ] => do_rect_head lhs rhs ltac:(fun _ => idtac)
525 end
526 | match goal with
527 | [ H : length ?ls = length ?ls' |- ?R (List.fold_right ?f ?x ?ls) (List.fold_right ?f' ?x' ?ls') = true ]
528 => is_var ls; is_var ls';
529 let IH := fresh "IH" in
530 revert dependent ls'; induction ls as [|? ls IH]; intros [|? ls']; cbn [List.fold_right length] in *;
531 [
532 | exfalso; clear -H; discriminate | exfalso; clear -H; discriminate
533 | specialize (IH ls');
534 generalize dependent (List.fold_right f x); generalize dependent (List.fold_right f' x') ];
535 intros
536 | [ H : length ?ls = length ?ls' |- ?R (List.fold_right ?f ?x ?ls) (List.fold_right ?f' ?x' ?ls') = true ]
537 => is_var ls; is_var ls';
538 let IH := fresh "IH" in
539 revert dependent ls'; induction ls as [|? ls IH]; intros [|? ls']; intros; cbn [List.fold_right length] in *;
540 [
541 | exfalso; discriminate | exfalso; discriminate
542 | specialize (IH ls');
543 generalize dependent (List.fold_right f x); generalize dependent (List.fold_right f' x') ];
544 intros
545 | [ H : forall a b, ?R a b = true -> ?R' (?f a) (?g b) = true |- ?R' (?f _) (?g _) = true ] => apply H; clear H
546 | [ H : forall a b, ?R a b = true -> forall c d, ?R' c d = true -> ?R'' (?f a c) (?g b d) = true |- ?R'' (?f _ _) (?g _ _) = true ]
547 => apply H; clear H
548 | [ H : forall a b, ?R a b = true -> forall c d, ?R' c d = true -> forall e f, ?R'' e f = true -> ?R''' (?F _ _ _) (?G _ _ _) = true |- ?R''' (?F _ _ _) (?G _ _ _) = true ]
549 => apply H; clear H
550 | [ H : context[ZRange.type.base.option.is_bounded_by (?f (Some _) (Some _)) (?g _ _) = true]
551 |- ZRange.type.base.option.is_bounded_by (?f (Some _) (Some _)) (?g _ _) = true ]
552 => apply H
553 | [ H : (forall a b, ?R0 a b = true -> forall c d, ?R1 c d = true -> forall e f, (forall g h, ?R3 g h = true -> ?R4 (e g) (f h) = true) -> forall i j, ?R5 i j = true -> ?R6 (?F _ _ _ _) (?G _ _ _ _) = true)
554 |- ?R6 (?F _ _ _ _) (?G _ _ _ _) = true ]
555 => apply H; clear H
556 end
557 | match goal with
558 | [ |- ZRange.type.base.option.is_bounded_by (ZRange.type.base.option.union _ _) (Bool.Thunked.bool_rect _ _ _ true) = true ]
559 => apply ZRange.type.base.option.is_bounded_by_union_l
560 | [ |- ZRange.type.base.option.is_bounded_by (ZRange.type.base.option.union _ _) (Bool.Thunked.bool_rect _ _ _ false) = true ]
561 => apply ZRange.type.base.option.is_bounded_by_union_r
562 | [ |- ZRange.type.base.option.is_bounded_by (ZRange.type.base.option.union _ _) (Bool.bool_rect_nodep _ _ true) = true ]
563 => apply ZRange.type.base.option.is_bounded_by_union_l
564 | [ |- ZRange.type.base.option.is_bounded_by (ZRange.type.base.option.union _ _) (Bool.bool_rect_nodep _ _ false) = true ]
565 => apply ZRange.type.base.option.is_bounded_by_union_r
566 end
567 | do_with_hyp' ltac:(fun H => apply H; clear H; now non_arith_t) ].
568
569 Local Lemma mul_by_halves_bounds x y n :
570 (0 <= x < 2^ (n / 2))%Z ->
571 (0 <= y < 2^ (n / 2))%Z ->
572 (0 <= x * y <= 2 ^ n - 1)%Z.
573 Proof using Type.
574 intros. rewrite Z.le_sub_1_iff.
575 assert (2 ^ (n / 2) * 2 ^ (n / 2) <= 2 ^ n)%Z.
576 { rewrite <-Z.pow_twice_r.
577 apply Z.pow_le_mono_r; auto with zarith; [ ].
578 Z.div_mod_to_quot_rem. nia. }
579 Z.div_mod_to_quot_rem; nia.
580 Qed.
581
582 Local Ltac mul_by_halves_t :=
583 apply mul_by_halves_bounds;
584 match goal with
585 | |- context [Z.land _ (2 ^ ?n - 1)%Z] =>
586 rewrite (Z.sub_1_r (2 ^ n)), <-Z.ones_equiv, !Z.land_ones by auto with zarith
587 | |- context [Z.shiftr ?a ?n] =>
588 apply LandLorShiftBounds.Z.shiftr_range; auto with zarith; rewrite Z.add_diag, <-Z_div_exact_full_2 by auto with zarith
589 end; auto using Z.mod_pos_bound with zarith.
590
591 Local Lemma invert_to_literal {t} e v
592 : ZRange.ident.option.to_literal (t:=t) e = Some v
593 <-> e = ZRange.ident.option.of_literal v.
594 Proof using Type.
595 induction t; split_iff; split; cbn; cbv [Crypto.Util.Option.bind option_map Option.lift]; break_innermost_match;
596 repeat first [ progress Z.ltb_to_lt
597 | reflexivity
598 | congruence
599 | apply (f_equal2 (@pair _ _))
600 | progress cbn [ZRange.lower ZRange.upper] in *
601 | progress cbv [Crypto.Util.Option.bind] in *; break_match_hyps
602 | progress subst
603 | progress intros
604 | progress inversion_option
605 | progress inversion_prod
606 | progress inversion_zrange
607 | progress destruct_head_hnf' zrange
608 | progress destruct_head_hnf' unit
609 | solve [ eauto ]
610 | match goal with
611 | [ H : Crypto.Util.OptionList.Option.List.lift (List.map ZRange.ident.option.to_literal ?l) = Some ?v |- _ ]
612 => cbv [Crypto.Util.OptionList.Option.List.lift] in H; revert v H; induction l; intro v; destruct v; cbn [List.fold_right List.map]; intro
613 | [ |- Crypto.Util.OptionList.Option.List.lift (List.map ZRange.ident.option.to_literal (List.map ZRange.ident.option.of_literal ?v)) = Some ?v ]
614 => cbv [Crypto.Util.OptionList.Option.List.lift]; induction v; cbn [List.fold_right List.map]
615 | [ H : cons _ _ = cons _ _ |- _ ] => inversion H; clear H
616 | [ H : forall x, Some x = Some _ -> _ |- _ ] => specialize (H _ eq_refl)
617 | [ H : forall x, Some _ = Some x -> _ |- _ ] => specialize (H _ eq_refl)
618 | [ H : forall e v, ?f e = Some v -> e = ?g v, H' : ?f ?e' = Some ?v' |- _ ]
619 => is_var v'; specialize (H e' v' H')
620 | [ H : forall e v, e = ?f v -> ?g e = Some v, H' : ?g ?e' = _ |- _ ]
621 => erewrite H in H' by reflexivity
622 end
623 | progress break_match
624 | apply (f_equal2 (@cons _))
625 | apply (f_equal (@Some _)) ].
626 Qed.
627
628 Local Lemma is_bounded_by_of_literal {t} x y
629 : ZRange.type.base.option.is_bounded_by (t:=t) (ZRange.ident.option.of_literal x) y = true <-> x = y.
630 Proof using Type.
631 induction t; split_iff; split; cbn [ZRange.ident.option.of_literal ZRange.type.base.option.is_bounded_by ZRange.type.base.is_bounded_by]; break_innermost_match.
632 all: repeat first [ progress intros
633 | progress subst
634 | progress destruct_head'_and
635 | progress cbv [option_beq_hetero option_map] in *
636 | match goal with
637 | [ H : forall x y, x = y -> _ |- _ ] => specialize (fun x => H x x eq_refl)
638 | [ H : andb _ _ = true |- _ ] => rewrite Bool.andb_true_iff in H
639 | [ |- andb _ _ = true ] => rewrite Bool.andb_true_iff
640 | [ |- and _ _ ] => split
641 | [ H : is_bounded_by_bool _ r[?x ~> ?x] = true |- _ ]
642 => apply ZRange.is_bounded_by_bool_constant_iff in H
643 | [ H : ?beq ?x ?y = true |- ?x = ?y ]
644 => progress reflect_beq_to_eq beq
645 | [ |- ?beq ?x ?x = true ]
646 => progress reflect_beq_to_eq beq
647 end
648 | solve [ eauto with nocore ]
649 | reflexivity
650 | apply ZRange.is_bounded_by_bool_constant
651 | progress inversion_prod
652 | progress destruct_head_hnf' unit
653 | progress break_innermost_match_hyps
654 | progress break_innermost_match
655 | congruence
656 | apply (f_equal2 (@pair _ _))
657 | apply (f_equal2 (@cons _))
658 | apply (f_equal (@Some _))
659 | match goal with
660 | [ H : fold_andb_map ZRange.type.base.option.is_bounded_by (List.map ZRange.ident.option.of_literal ?x) ?y = true |- ?x = ?y ]
661 => is_var x; is_var y; revert dependent y; induction x; intro y; destruct y; cbn [fold_andb_map List.map] in *; generalize dependent (@fold_andb_map); intros
662 | [ |- fold_andb_map ZRange.type.base.option.is_bounded_by (List.map ZRange.ident.option.of_literal ?x) ?x = true ]
663 => is_var x; induction x; cbn [fold_andb_map List.map] in *; generalize dependent (@fold_andb_map); intros
664 end ].
665 Qed.
666
667 Local Ltac require_pairs_of_literals term :=
668 lazymatch term with
669 | ZRange.ident.option.of_literal _ => idtac
670 | (?x, ?y) => require_pairs_of_literals x; require_pairs_of_literals y
671 end.
672 Local Ltac handle_to_literal :=
673 repeat match goal with
674 | [ |- context[ZRange.ident.option.to_literal (t:=?T) ?x] ]
675 => is_var x;
676 let H := fresh in
677 destruct (ZRange.ident.option.to_literal (t:=T) x) eqn:H;
678 [ rewrite invert_to_literal in H; subst x | ]
679 | [ H : ZRange.type.base.option.is_bounded_by (t:=?T1*?T2) (?r1, ?r2) (?x1, ?x2) = true |- _ ]
680 => require_pairs_of_literals r1;
681 change (ZRange.type.base.option.is_bounded_by (t:=T1) r1 x1 && ZRange.type.base.option.is_bounded_by (t:=T2) r2 x2 = true)%bool in H;
682 rewrite Bool.andb_true_iff in H; destruct H
683 | [ H : ZRange.type.base.option.is_bounded_by (ZRange.ident.option.of_literal ?x) ?y = true |- _ ]
684 => rewrite is_bounded_by_of_literal in H; (subst x || subst y)
685 end.
686
687 Local Lemma interp_related_fancy_rshi :
688 interp_is_related ident.fancy_rshi.
689 Proof using Type.
690 cbn [type.related_hetero ZRange.ident.option.interp ident.interp respectful_hetero type.interp ZRange.type.base.option.interp ZRange.type.base.interp base.interp base.base_interp ZRange.type.base.option.Some ZRange.ident.option.of_literal].
691 cbv [respectful_hetero option_map list_case].
692 intros.
693 destruct_head_hnf' prod.
694 handle_to_literal;
695 break_innermost_match;
696 auto using type.base.option.is_bounded_by_None;
697 ident.fancy.cbv_fancy_in_all; cbn [ZRange.type.base.option.is_bounded_by ZRange.type.base.is_bounded_by Crypto.Util.Option.bind ZRange.ident.option.to_literal fst snd] in *;
698 repeat match goal with
699 | H : _ |- _ =>
700 rewrite Bool.andb_true_iff in H;
701 destruct H
702 | _ => progress destruct_head_hnf' option; try congruence; [ ]
703 | _ => progress cbv [is_bounded_by_bool is_tighter_than_bool] in *; cbn in *
704 | _ => progress cbn in *
705 end.
706
707 { Z.ltb_to_lt. rewrite !Z.rshi_small by lia.
708 apply Bool.andb_true_iff; split; apply Z.leb_le;
709 Z.div_mod_to_quot_rem; nia. }
710 { Z.ltb_to_lt.
711 rewrite Z.rshi_correct_full by lia.
712 break_innermost_match; apply Bool.andb_true_iff; split; apply Z.leb_le; try apply Z.le_sub_1_iff; auto with zarith. }
713 Qed.
714
715 (** In abstract interpretation, we only need this version of the lemma for less-than-third-order types, but in Assembly/Symbolic, we use it for all identifiers *)
716 Lemma interp_related {t} (idc : ident t) (*H : type.is_not_higher_order_than 3 t = true*) : interp_is_related idc.
717 Proof using Type.
718 destruct idc.
719 (*
720 (** clear out higher-than-third-order types *)
721 all: cbn in H; try congruence.*)
722 all: lazymatch goal with
723 | [ |- context[ident.Z_cast] ] => apply interp_related_Z_cast
724 | [ |- context[ident.Z_cast2] ] => apply interp_related_Z_cast2
725 | [ |- context[ident.List_flat_map] ] => apply interp_related_List_flat_map
726 | [ |- context[ident.List_partition] ] => apply interp_related_List_partition
727 | [ |- context[ident.List_filter] ] => apply interp_related_List_filter
728 | [ |- context[ident.fancy_rshi] ] => apply interp_related_fancy_rshi
729 | _ => idtac
730 end.
731 all: cbn [type.related_hetero ZRange.ident.option.interp ident.interp respectful_hetero type.interp ZRange.type.base.option.interp ZRange.type.base.interp base.interp base.base_interp ZRange.type.base.option.Some ZRange.ident.option.of_literal].
732 all: cbv [ident.cast2 ident.literal prod_rect_nodep prod_rect ident.eagerly] in *.
733 all: change (@zrange_rect_nodep) with (fun T => @zrange_rect (fun _ => T)) in *.
734 all: change (@Thunked.nat_rect) with (fun P P0 => @nat_rect (fun _ => P) (P0 Datatypes.tt)) in *.
735 all: change (@nat_rect_arrow_nodep) with (fun P Q => @nat_rect (fun _ => P -> Q)).
736 all: change (@Thunked.list_rect) with (fun A P PNil => @list_rect A (fun _ => P) (PNil Datatypes.tt)) in *.
737 all: change (@list_rect_arrow_nodep) with (fun A P Q => @list_rect A (fun _ => P -> Q)).
738 all: change (@Thunked.list_case) with (fun A P PNil => @list_case A (fun _ => P) (PNil Datatypes.tt)) in *.
739 all: change (@Thunked.option_rect) with (fun A P PS PN => @option_rect A (fun _ => P) PS (PN Datatypes.tt)) in *.
740 all: cbv [
741 nat_rect_fbb_b
742 nat_rect_fbb_b_b
743 list_rect_fbb_b
744 list_rect_fbb_b_b
745 list_rect_fbb_b_b_b
746 list_rect_fbb_b_b_b_b
747 list_rect_fbb_b_b_b_b_b
748 ] in *.
749 all: cbv [respectful_hetero option_map option_rect zrange_rect list_case].
750 all: intros.
751 all: destruct_head_hnf' prod.
752 all: destruct_head_hnf' option.
753 Time all: try solve [ non_arith_t ].
754 all: ident.fancy.cbv_fancy_in_all; cbn [ZRange.type.base.option.is_bounded_by ZRange.type.base.is_bounded_by Crypto.Util.Option.bind ZRange.ident.option.to_literal fst snd] in *.
755 all: break_innermost_match; try reflexivity.
756 Time all: try solve [ non_arith_t ].
757 all: repeat first [ progress subst
758 | progress inversion_prod
759 | progress inversion_option
760 | progress destruct_head'_and
761 | progress cbn [ZRange.type.base.option.is_tighter_than lower upper fst snd Crypto.Util.Option.bind ZRange.type.base.is_tighter_than] in *
762 | progress cbv [Definitions.Z.lnot_modulo Definitions.Z.add_with_carry] in *
763 | handle_lt_le_t_step
764 | simplify_ranges_t_step
765 | discriminate
766 | solve [ auto using ZRange.is_bounded_by_bool_Proper_if_sumbool_union, Z.mod_pos_bound, Z.mod_neg_bound with zarith ]
767 | rewrite ZRange.is_bounded_by_bool_opp
768 | progress autorewrite with zsimplify_const
769 | rewrite Z.le_sub_1_iff
770 | rewrite Z.le_add_1_iff
771 | match goal with
772 | [ H : ?x = ?x |- _ ] => clear H
773 | [ |- andb (is_bounded_by_bool (_ mod ?m) (fst (Operations.ZRange.split_bounds _ ?m)))
774 (is_bounded_by_bool (_ / ?m) (snd (Operations.ZRange.split_bounds _ ?m))) = true ]
775 => apply ZRange.is_bounded_by_bool_split_bounds
776 | [ |- is_bounded_by_bool (_ / ?m) (snd (Operations.ZRange.split_bounds _ ?m)) = true ]
777 => apply ZRange.is_bounded_by_bool_split_bounds_and
778 | [ |- is_bounded_by_bool (_ mod _) r[_ ~> _] = true ] => cbv [is_bounded_by_bool]; rewrite Bool.andb_true_iff
779 | [ H : is_bounded_by_bool ?v ?r1 = true, H' : is_tighter_than_bool ?r1 ?r2 = true |- _ ]
780 => pose proof (@ZRange.is_bounded_by_of_is_tighter_than r1 r2 H' v H);
781 clear H H' r1
782 | [ |- context [match (if _ then _ else _) with _ => _ end ] ]
783 => break_innermost_match
784 | [ |- context[ZRange.constant ?x] ] => unique pose proof (ZRange.is_bounded_by_bool_constant x)
785 | [ |- context[r[?x~>?x]%zrange] ] => unique pose proof (ZRange.is_bounded_by_bool_constant x)
786 | [ |- is_bounded_by_bool (Definitions.Z.ltz _ _) r[0 ~> 1] = true ]
787 => cbv [Definitions.Z.ltz]; break_innermost_match; cbv
788 end
789 | progress Z.ltb_to_lt
790 | progress rewrite ?Z.mul_split_div, ?Z.mul_split_mod, ?Z.mul_high_div, ?Z.add_get_carry_full_div, ?Z.add_get_carry_full_mod, ?Z.add_with_get_carry_full_div, ?Z.add_with_get_carry_full_mod, ?Z.sub_get_borrow_full_div, ?Z.sub_get_borrow_full_mod, ?Z.sub_with_get_borrow_full_div, ?Z.sub_with_get_borrow_full_mod, ?Z.zselect_correct, ?Z.add_modulo_correct, ?Z.rshi_correct_full, ?Z.truncating_shiftl_correct_land_ones ].
791 all: repeat lazymatch goal with
792 | [ |- is_bounded_by_bool (Z.land _ _) (ZRange.land_bounds _ _) = true ]
793 => apply ZRange.is_bounded_by_bool_land_bounds; auto
794 | [ |- is_bounded_by_bool (Z.lor _ _) (ZRange.lor_bounds _ _) = true ]
795 => apply ZRange.is_bounded_by_bool_lor_bounds; auto
796 | [ |- is_bounded_by_bool (if _ then (_ + _ - _)%Z else (_ + _)%Z) (ZRange.union (ZRange.four_corners Z.add _ _) (ZRange.eight_corners _ _ _ _)) = true ]
797 => rewrite ZRange.union_comm; apply ZRange.is_bounded_by_bool_Proper_if_bool_union_dep; intros; Z.ltb_to_lt
798 | [ |- is_bounded_by_bool (?a + ?b - ?c) (ZRange.eight_corners (fun x y z => Z.max 0 (x + y - z)) _ _ _) = true ]
799 => replace (a + b - c)%Z with (Z.max 0 (a + b - c)) by lia
800 | [ Hx : is_bounded_by_bool _ ?x = true, Hy : is_bounded_by_bool _ ?y = true, Hz : is_bounded_by_bool _ ?z = true
801 |- is_bounded_by_bool _ (ZRange.eight_corners ?f ?x ?y ?z) = true ]
802 => apply (fun pf1 pf2 pf3 => @ZRange.monotoneb_eight_corners_gen f pf1 pf2 pf3 _ _ _ _ _ _ Hx Hy Hz); intros; auto with zarith
803 | [ Hx : is_bounded_by_bool _ ?x = true, Hy : is_bounded_by_bool _ ?y = true
804 |- is_bounded_by_bool _ (ZRange.four_corners_and_zero ?f ?x ?y) = true ]
805 => apply (fun pf1 pf2 pf3 pf4 => @ZRange.monotoneb_four_corners_and_zero_gen f pf1 pf2 pf3 pf4 _ _ _ _ Hx Hy); intros; auto with zarith
806 | [ Hx : is_bounded_by_bool _ ?x = true, Hy : is_bounded_by_bool _ ?y = true
807 |- is_bounded_by_bool _ (ZRange.four_corners ?f ?x ?y) = true ]
808 => apply (fun pf1 pf2 => @ZRange.monotoneb_four_corners_gen f pf1 pf2 _ _ _ _ Hx Hy); intros; auto with zarith
809 | [ Hx : is_bounded_by_bool _ ?x = true
810 |- is_bounded_by_bool _ (ZRange.two_corners ?f ?x) = true ]
811 => apply (fun pf => @ZRange.monotoneb_two_corners_gen f pf x _ Hx); intros; auto with zarith
812 | [ Hx : is_bounded_by_bool _ ?x = true
813 |- is_bounded_by_bool _ (ZRange.two_corners_and_zero ?f ?x) = true ]
814 => apply (fun pf1 pf2 => @ZRange.monotoneb_two_corners_and_zero_gen f pf1 pf2 x _ Hx); intros; auto with zarith
815 | [ |- is_bounded_by_bool (if _ then _ else _) (ZRange.four_corners _ _ _) = true ]
816 => apply ZRange.is_bounded_by_bool_Proper_if_bool_union_dep; intros; Z.ltb_to_lt
817 | [ _ : is_bounded_by_bool ?x1 ?r1 = true,
818 _ : is_bounded_by_bool ?x2 ?r2 = true,
819 H : (?fr ?r1 ?r2 =? r[0 ~> 1])%zrange = true |- is_bounded_by_bool (?f ?x1 ?x2) r[0~>2] = true ]
820 => cut (is_bounded_by_bool (f x1 x2) (fr r1 r2) = true);
821 [ apply ZRange.is_bounded_by_of_is_tighter_than; apply zrange_bl in H; rewrite H; clear
822 | clear H ]
823 | [ H : ?x <> ?x |- _ ] => destruct (H eq_refl)
824 | [ |- context[Proper _ (Z.mul ?v)] ] => is_var v; destruct v; auto with zarith
825 | [ |- context[Proper _ (Z.div ?v)] ] => is_var v; destruct v; auto with zarith
826 | [ |- context[Proper _ (fun y => Z.mul _ ?v)] ] => is_var v; destruct v; auto with zarith
827 | [ |- context[Proper _ (fun y => Z.mul ?v _)] ] => is_var v; destruct v; auto with zarith
828 | [ |- context[Proper _ (fun y => Z.div _ ?v)] ] => is_var v; destruct v; auto with zarith
829 | [ |- context[Proper _ (fun y => Z.div ?v _)] ] => is_var v; destruct v; auto with zarith
830 | [ |- context[Proper _ (fun y => Z.shiftr _ ?v)] ] => is_var v; destruct v; auto with zarith
831 | [ |- context[Proper _ (fun y => Z.shiftr ?v _)] ] => is_var v; destruct v; auto with zarith
832 | [ |- context[Proper _ (fun y => Z.shiftl _ ?v)] ] => is_var v; destruct v; auto with zarith
833 | [ |- context[Proper _ (fun y => Z.shiftl ?v _)] ] => is_var v; destruct v; auto with zarith
834 | _ => idtac
835 end.
836 all: try solve [ cbv [Proper respectful Basics.flip]; constructor; intros; lia ].
837 all: try solve [ cbv [Proper respectful Basics.flip]; constructor; intros; autorewrite with zsimplify_const; lia ].
838 all: cbv [is_bounded_by_bool ZRange.upper ZRange.lower]; rewrite ?Bool.andb_true_iff, ?Z.leb_le.
839 all: try solve [ lia
840 | match goal with
841 | [ |- ((0 <= _ mod _ <= _) /\ (0 <= _ <= _))%Z ]
842 => Z.div_mod_to_quot_rem; nia
843 end
844 | intros; mul_by_halves_t ].
845 all: try solve [ non_arith_t; Z.ltb_to_lt; reflexivity ].
846 all: try solve [ non_arith_t; try match goal with |- ?x = true => destruct x eqn:? end; reflect_hyps; subst; nia ].
847 all: try solve [ cbv [ZRange.ToConstant.four_corners ZRange.ToConstant.Option.four_corners ZRange.ToConstant.Option.apply_to_range ZRange.ToConstant.Option.two_corners ZRange.ToConstant.Option.union option_beq Bool.eqb] in *; non_arith_t; Z.ltb_to_lt; lia ].
848 (** For command-line debugging, we display goals that should not remain *)
849 all: [ > idtac "WARNING: Remaining goal:"; print_context_and_goal () .. ].
850 Qed.
851
852 Variant rect_arg_kind := fin (_ : API.type) | recr.
853 Definition ctor_descr := list rect_arg_kind.
854 Definition rect_spec := list ctor_descr.
855 Definition interp_rect_arg_kind (recrT : API.type) (r : rect_arg_kind) : API.type
856 := match r with
857 | fin t => t
858 | recr => recrT
859 end.
860 Fixpoint interp_ctor_descr (containerT : API.type) (motive : API.type) (r : ctor_descr) : API.type
861 := match r with
862 | nil => motive
863 | cons (fin t) rs => t -> interp_ctor_descr containerT motive rs
864 | cons recr rs => containerT -> motive -> interp_ctor_descr containerT motive rs
865 end%etype.
866 Fixpoint interp_rect (containerT : API.type) (motive : API.type) (r : rect_spec) : API.type
867 := match r with
868 | nil => containerT -> motive
869 | cons r rs => interp_ctor_descr containerT motive r -> interp_rect containerT motive rs
870 end%etype.
871 Variant rect_kind := rect_nat | rect_list (t : base.type).
872
873 Definition rect_spec_of_kind (r : rect_kind) : rect_spec
874 := match r with
875 | rect_nat => [ [] ; [recr] ]
876 | rect_list t => [ [] ; [fin (type.base t); recr] ]
877 end.
878 Definition rect_container_of_kind (r : rect_kind) : base.type
879 := match r with
880 | rect_nat => base.type.type_base base.type.nat
881 | rect_list t => base.type.list t
882 end.
883
884 (* TODO: move? *)
885 (** N.B. We don't say yes for thunked recursors, which are a bit different in shape *)
886 Definition is_rect {t} (idc : ident t) : option { k : rect_kind & { m : _ | t = interp_rect (type.base (rect_container_of_kind k)) m (rect_spec_of_kind k) } }.
887 Proof.
888 refine match idc with
889 | ident.nat_rect_arrow _ _
890 | ident.eager_nat_rect_arrow _ _
891 | ident.nat_rect_fbb_b _ _ _
892 | ident.nat_rect_fbb_b_b _ _ _ _
893 => Some (existT _ rect_nat _)
894 | ident.list_rect_arrow _ _ _
895 | ident.eager_list_rect_arrow _ _ _
896 | ident.list_rect_fbb_b _ _ _ _
897 | ident.list_rect_fbb_b_b _ _ _ _ _
898 | ident.list_rect_fbb_b_b_b _ _ _ _ _ _
899 | ident.list_rect_fbb_b_b_b_b _ _ _ _ _ _ _
900 | ident.list_rect_fbb_b_b_b_b_b _ _ _ _ _ _ _ _
901 => Some (existT _ (rect_list _) _)
902 | _
903 => None
904 end.
905 all: eexists; cbv; reflexivity.
906 Defined.
907
908 Lemma rect_interp_rect {t} {idc : ident t} {k} (H : is_rect idc = Some k)
909 : match k with
910 | existT rect_nat (exist p H)
911 => forall z s, let f := (rew [API.interp_type] H in ident.interp idc) in f z s 0 = z /\ forall n, f z s (S n) = s n (f z s n)
912 | existT (rect_list _) (exist p H)
913 => forall n c, let f := (rew [API.interp_type] H in ident.interp idc) in f n c nil = n /\ forall x xs, f n c (cons x xs) = c x xs (f n c xs)
914 end.
915 Proof using Type.
916 fold (@type.interp) in *.
917 destruct idc; cbn in H; inversion_option; subst.
918 all: cbn; repeat split.
919 Qed.
920
921 Lemma rect_ZRange_interp_rect {t} {idc : ident t} {k} (H : is_rect idc = Some k)
922 : match k with
923 | existT rect_nat (exist p H)
924 => forall z s, let f := (rew [ZRange.type.option.interp] H in ZRange.ident.option.interp assume_cast_truncates idc) in f z s None = ZRange.type.option.None /\ f z s (Some 0) = z /\ forall n, f z s (Some (S n)) = s (Some n) (f z s (Some n))
925 | existT (rect_list _) (exist p H)
926 => forall n c, let f := (rew [ZRange.type.option.interp] H in ZRange.ident.option.interp assume_cast_truncates idc) in f n c None = ZRange.type.option.None /\ f n c (Some nil) = n /\ forall x xs, f n c (Some (cons x xs)) = c x (Some xs) (f n c (Some xs))
927 end.
928 Proof using Type.
929 fold (@type.interp) in *.
930 fold (@ZRange.type.base.option.interp) in *.
931 destruct idc; cbn in H; inversion_option; subst.
932 all: cbn; repeat split.
933 Qed.
934
935 Lemma interp_related_and_Proper {t} (idc : ident t) : interp_is_related_and_Proper idc.
936 Proof using Type.
937 destruct (type.is_not_higher_order_than 3 t) eqn:Hho;
938 [ | destruct (is_rect idc) eqn:Hrect ].
939 { apply type.related_hetero_impl_related_hetero_and_Proper_eqv_not_higher_order_than_3.
940 all: try apply interp_related.
941 all: try apply ident.interp_Proper.
942 all: try apply ZRange.ident.option.interp_Proper.
943 all: try assumption.
944 all: try reflexivity. }
945 { apply type.related_hetero_and_Proper_iff_app_curried.
946 all: try now intros; hnf.
947 repeat split.
948 all: try now apply ident.interp_Proper.
949 all: try now apply ZRange.ident.option.interp_Proper.
950 all: [ > ].
951 intros x y Hxy.
952
953 (*pose proof (interp_related idc) as Hir.*)
954 pose proof (ident.interp_Proper idc idc eq_refl) as Hip.
955 pose proof (ZRange.ident.option.interp_Proper assume_cast_truncates idc idc eq_refl) as Hzip.
956 pose proof Hrect as Hrect'.
957 apply rect_interp_rect in Hrect.
958 apply rect_ZRange_interp_rect in Hrect'.
959 fold base.interp in *.
960 fold (@type.interp) in *.
961 fold (@ZRange.type.base.option.interp) in *.
962 fold (@ZRange.type.option.interp) in *.
963 clear Hho.
964 let k := match goal with H : sigT _ |- _ => H end in
965 destruct k as [k [p Ht] ].
966 subst t.
967
968 generalize dependent (ident.interp idc).
969 generalize dependent (ZRange.ident.option.interp assume_cast_truncates idc).
970 clear idc.
971 destruct_head' rect_kind.
972 all: cbn [eq_rect].
973 all: cbn [interp_rect rect_spec_of_kind interp_ctor_descr rect_container_of_kind type.app_curried type.final_codomain ZRange.type.option.interp type.interp type.and_for_each_lhs_of_arrow type.for_each_lhs_of_arrow] in *.
974 all: destruct_head'_prod; cbn [fst snd] in *.
975 all: intros; split_and.
976 all: repeat match goal with
977 | [ H : type.related_hetero_and_Proper _ _ _ _ _ |- _ ]
978 => apply type.related_hetero_and_Proper_iff_app_curried in H;
979 [ | clear H; repeat intro; hnf; reflexivity .. ]
980 end.
981 all: destruct_head'_and.
982 all: cbn [type.related] in *.
983 all: cbv [Proper] in *.
984 all: cbn in *.
985 all: specialize_by first [ exact tt | exact I ].
986 all: destruct_head' option; reflect_hyps; subst.
987 all: eliminate_hprop_eq.
988 all: match goal with
989 | [ |- ?R (type.app_curried (?f (Some ?n)) ?x) (type.app_curried (?g ?n) ?y) = true ]
990 => revert dependent y; revert dependent x;
991 induction n; intros
992 | [ |- ?R (type.app_curried (?f None) ?x) (type.app_curried (?g ?n) ?y) = true ]
993 => revert dependent y; revert dependent x;
994 induction n; intros
995 | [ |- ?R (type.app_curried (?f (Some ?n')) ?x) (type.app_curried (?g ?n) ?y) = true ]
996 => revert dependent y; revert dependent x;
997 revert dependent n'; induction n; intro n'; destruct n'; intros
998 end.
999 all: repeat do_with_hyp' ltac:(fun H => rewrite !H; []).
1000 all: auto.
1001 all: repeat match goal with
1002 | [ H : forall x y : _ * (_ * (_ * _)), _ |- _ ]
1003 => specialize (fun x1 y1 x2 y2 x3 y3 x4 y4 => H (x1, (x2, (x3, x4))) (y1, (y2, (y3, y4))))
1004 | [ H : forall x y : _ * _ * _, _ |- _ ]
1005 => specialize (fun x1 y1 x2 y2 x3 y3 => H (x1, x2, x3) (y1, y2, y3))
1006 | [ H : forall x y : _ * (_ * _), _ |- _ ]
1007 => specialize (fun x1 y1 x2 y2 x3 y3 => H (x1, (x2, x3)) (y1, (y2, y3)))
1008 end.
1009 all: cbn [fst snd] in *.
1010 all: try do_with_exactly_one_hyp ltac:(fun H => apply H).
1011 all: repeat split; intros; auto.
1012 all: rewrite ?ZRange.type.option.app_curried_None.
1013 all: try apply type.base.option.is_bounded_by_None.
1014 all: rewrite ?type.related_hetero_and_Proper_iff_app_curried by now intros; hnf.
1015 all: repeat split; intros; cbv [Proper] in *.
1016 all: auto.
1017 all: repeat match goal with
1018 | [ |- ?x = true ] => destruct x eqn:?; reflect_hyps; congruence
1019 end.
1020 all: repeat do_with_hyp' ltac:(fun H => rewrite !H; []).
1021 all: reflect_hyps; destruct_head'_False.
1022 all: cbn [fold_andb_map] in *; rewrite ?Bool.andb_true_iff in *; destruct_head'_and.
1023 all: auto.
1024 all: try now repeat do_with_hyp' ltac:(fun H => apply H); repeat intro; subst; auto. }
1025 { destruct idc.
1026 all: try (apply Bool.diff_true_false in Hho; exfalso; exact Hho).
1027 all: try solve [ cbn in Hrect; inversion_option ].
1028 (** For command-line debugging, we display goals that should not remain *)
1029 all: [ > idtac "WARNING: Remaining non-rect goal of order > 3:"; print_context_and_goal () .. ]. }
1030 Time Qed.
1031 End interp_related.
1032 End option.
1033 End ident.
1034 End ZRange.
1035 End Compilers.