Debian Codegraph @

src/AbstractInterpretation/ZRange.v

raw
1 From Coq Require Import Lia.
2 From Coq Require Import ZArith.
3 Require Import Crypto.Util.ListUtil Coq.Lists.List Crypto.Util.ListUtil.FoldBool.
4 Require Import Crypto.Util.ZRange.
5 Require Import Crypto.Util.ZRange.Operations.
6 Require Import Crypto.Util.Option.
7 Require Import Crypto.Util.OptionList.
8 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
9 Require Import Crypto.Util.Bool.Reflect.
10 Require Import Rewriter.Language.Language.
11 Require Import Crypto.Language.API.
12 Import ListNotations. Local Open Scope bool_scope. Local Open Scope Z_scope.
13
14 Module Compilers.
15 Export Language.Compilers.
16 Export Language.API.Compilers.
17
18 Module ZRange.
19 Module Export Settings.
20 Class shiftr_avoid_uint1_opt := shiftr_avoid_uint1 : bool.
21 #[global]
22 Typeclasses Opaque shiftr_avoid_uint1_opt.
23 Module AbstractInterpretation.
24 Local Set Primitive Projections.
25 Class Options
26 := { shiftr_avoid_uint1 : shiftr_avoid_uint1_opt
27 }.
28 Definition default_Options : Options
29 := {| shiftr_avoid_uint1 := false |}.
30 Module Export Exports.
31 Global Existing Instance Build_Options.
32 Global Existing Instance shiftr_avoid_uint1.
33 Global Hint Cut [ ( _ * ) shiftr_avoid_uint1 ( _ * ) Build_Options ] : typeclass_instances.
34 Global Coercion shiftr_avoid_uint1 : Options >-> shiftr_avoid_uint1_opt.
35 End Exports.
36 End AbstractInterpretation.
37 Export AbstractInterpretation.Exports.
38 End Settings.
39 Module type.
40 Local Notation binterp := base.interp.
41 Local Notation tinterp_gen := type.interp.
42 Local Notation einterp := (type.interp base.interp).
43 Module base.
44 (** turn a [base.type] into a [Set] describing the type of
45 bounds on that primitive; Z is a range, nat and bool are exact values *)
46 Fixpoint interp (t : base.type) : Type
47 := match t with
48 | base.type.type_base base.type.Z => zrange
49 | base.type.type_base _ as t
50 | base.type.unit as t
51 => base.interp t
52 | base.type.prod A B => interp A * interp B
53 | base.type.list A => list (interp A)
54 | base.type.option A => option (interp A)
55 end%type.
56 Fixpoint bitwidth {t} : interp t -> binterp t
57 := match t with
58 | base.type.type_base base.type.Z => fun r => ZRange.bitwidth r
59 | base.type.type_base _ as t
60 | base.type.unit as t
61 => fun x => x
62 | base.type.prod A B => fun '(a, b) => (@bitwidth A a, @bitwidth B b)
63 | base.type.list A => List.map (@bitwidth A)
64 | base.type.option A => option_map (@bitwidth A)
65 end.
66 Fixpoint of_bitwidth (signed : bool) {t} : binterp t -> interp t
67 := match t with
68 | base.type.type_base base.type.Z => fun r => ZRange.of_bitwidth signed r
69 | base.type.type_base _ as t
70 | base.type.unit as t
71 => fun x => x
72 | base.type.prod A B => fun '(a, b) => (@of_bitwidth signed A a, @of_bitwidth signed B b)
73 | base.type.list A => List.map (@of_bitwidth signed A)
74 | base.type.option A => option_map (@of_bitwidth signed A)
75 end%type.
76 Notation of_bitwidth_unsigned := (@of_bitwidth false).
77 Notation of_bitwidth_signed := (@of_bitwidth true).
78 Fixpoint map_ranges (f : zrange -> zrange) {t} : interp t -> interp t
79 := match t with
80 | base.type.type_base base.type.Z => f
81 | base.type.type_base _ as t
82 | base.type.unit as t
83 => fun x => x
84 | base.type.prod A B => fun '(a, b) => (@map_ranges f A a, @map_ranges f B b)
85 | base.type.list A => List.map (@map_ranges f A)
86 | base.type.option A => option_map (@map_ranges f A)
87 end%type.
88 Definition is_neg {t} : interp t -> bool
89 := match t with
90 | base.type.type_base base.type.Z => fun r => (lower r <? 0) && (upper r <=? 0)
91 | _ => fun _ => false
92 end.
93 Fixpoint is_tighter_than {t} : interp t -> interp t -> bool
94 := match t with
95 | base.type.type_base base.type.Z => is_tighter_than_bool
96 | base.type.type_base _ as t
97 | base.type.unit as t
98 => base.interp_beq (fun t => @base.base_interp_beq t t)
99 | base.type.prod A B
100 => fun '(a, b) '(a', b')
101 => @is_tighter_than A a a' && @is_tighter_than B b b'
102 | base.type.list A
103 => fold_andb_map (@is_tighter_than A)
104 | base.type.option A
105 => option_beq (@is_tighter_than A)
106 end%bool.
107 Fixpoint is_bounded_by {t} : interp t -> binterp t -> bool
108 := match t with
109 | base.type.type_base base.type.Z => fun r z => ZRange.is_bounded_by_bool z r
110 | base.type.type_base _ as t
111 | base.type.unit as t
112 => base.interp_beq (fun t => @base.base_interp_beq t t)
113 | base.type.prod A B
114 => fun '(a, b) '(a', b')
115 => @is_bounded_by A a a' && @is_bounded_by B b b'
116 | base.type.list A
117 => fold_andb_map (@is_bounded_by A)
118 | base.type.option A
119 => option_beq_hetero (@is_bounded_by A)
120 end.
121 Fixpoint interp_beq {t1 t2} : interp t1 -> interp t2 -> bool
122 := match t1, t2 return interp t1 -> interp t2 -> bool with
123 | base.type.type_base base.type.Z, base.type.type_base base.type.Z => zrange_beq
124 | base.type.type_base base.type.Z, _ | _, base.type.type_base base.type.Z => fun _ _ => false
125 | base.type.type_base _ as t1, base.type.type_base _ as t2
126 | base.type.unit as t1, base.type.unit as t2
127 => base.interp_beq_hetero (@base.base_interp_beq)
128 | base.type.prod A1 B1, base.type.prod A2 B2
129 => fun '(a, b) '(a', b')
130 => @interp_beq A1 A2 a a' && @interp_beq B1 B2 b b'
131 | base.type.list A1, base.type.list A2
132 => list_beq_hetero (@interp_beq A1 A2)
133 | base.type.option A1, base.type.option A2
134 => option_beq_hetero (@interp_beq A1 A2)
135 | base.type.type_base _, _
136 | base.type.unit, _
137 | base.type.prod _ _, _
138 | base.type.list _, _
139 | base.type.option _, _
140 => fun _ _ => false
141 end%bool.
142
143 Global Instance reflect_interp_eq {t} : reflect_rel (@eq _) (@interp_beq t t).
144 Proof.
145 induction t; cbn [interp_beq]; intros; break_innermost_match; try exact _.
146 Defined.
147
148 Lemma interp_beq_bl {t x y} : @interp_beq t t x y = true -> x = y.
149 Proof. apply reflect_to_beq; exact _. Qed.
150 Lemma interp_beq_lb {t x y} : x = y -> @interp_beq t t x y = true.
151 Proof. apply reflect_to_beq; exact _. Qed.
152
153 Module option.
154 (** turn a [base.type] into a [Set] describing the type
155 of optional bounds on that primitive; bounds on a [Z]
156 may be either a range, or [None], generally indicating
157 that the [Z] is unbounded. *)
158 Fixpoint interp (t : base.type) : Type
159 := match t with
160 | base.type.type_base _ as t
161 => option (base.interp t)
162 | base.type.prod A B => interp A * interp B
163 | base.type.list A => option (list (interp A))
164 | base.type.option A => option (option (interp A))
165 | base.type.unit => unit
166 end%type.
167 Fixpoint map_ranges (f : zrange -> zrange) {t} : interp t -> interp t
168 := match t with
169 | base.type.type_base _ as t
170 => option_map (@base.map_ranges f t)
171 | base.type.prod A B => fun '(a, b) => (@map_ranges f A a, @map_ranges f B b)
172 | base.type.list A => option_map (List.map (@map_ranges f A))
173 | base.type.option A => option_map (option_map (@map_ranges f A))
174 | base.type.unit => fun 'tt => tt
175 end%type.
176 Fixpoint None {t} : interp t
177 := match t with
178 | base.type.unit => tt
179 | base.type.list _
180 | base.type.option _
181 | base.type.type_base _
182 => Datatypes.None
183 | base.type.prod A B
184 => (@None A, @None B)
185 end.
186 Fixpoint Some {t} : base.interp t -> interp t
187 := match t with
188 | base.type.unit
189 => fun _ => tt
190 | base.type.type_base _
191 => Datatypes.Some
192 | base.type.list A
193 => fun ls => Datatypes.Some (List.map (@Some A) ls)
194 | base.type.option A
195 => fun ls => Datatypes.Some (option_map (@Some A) ls)
196 | base.type.prod A B
197 => fun '(a, b)
198 => (@Some A a, @Some B b)
199 end.
200 Fixpoint lift_Some {t} : interp t -> option (base.interp t)
201 := match t with
202 | base.type.type_base _
203 => fun x => x
204 | base.type.unit
205 => fun x => Datatypes.Some tt
206 | base.type.list A
207 => fun ls => ls <- ls; Option.List.lift (List.map (@lift_Some A) ls)
208 | base.type.option A
209 => fun v => v <- v; Option.lift (option_map (@lift_Some A) v)
210 | base.type.prod A B
211 => fun '(a, b) => a <- @lift_Some A a; b <- @lift_Some B b; Datatypes.Some (a, b)
212 end%option.
213 Fixpoint is_informative {t} : interp t -> bool (* true iff there's some bounds info *)
214 := match t with
215 | base.type.type_base _
216 | base.type.list _
217 | base.type.option _
218 => Option.is_Some
219 | base.type.unit
220 => fun _ => false
221 | base.type.prod A B
222 => fun '(a, b) => orb (@is_informative A a) (@is_informative B b)
223 end.
224 (** Keep data about list length and nat value, but not zrange *)
225 Fixpoint strip_ranges {t} : interp t -> interp t
226 := match t with
227 | base.type.type_base base.type.Z => fun _ => Datatypes.None
228 | base.type.type_base _
229 | base.type.unit
230 => fun x => x
231 | base.type.list A
232 => fun ls => ls <- ls; Datatypes.Some (List.map (@strip_ranges A) ls)
233 | base.type.option A
234 => fun v => v <- v; Datatypes.Some (option_map (@strip_ranges A) v)
235 | base.type.prod A B
236 => fun '(a, b)
237 => (@strip_ranges A a, @strip_ranges B b)
238 end%option.
239 Definition is_neg {t} : interp t -> bool
240 := match t with
241 | base.type.type_base base.type.Z
242 => fun v => match v with
243 | Datatypes.Some v => @is_neg (base.type.type_base base.type.Z) v
244 | Datatypes.None => false
245 end
246 | t => fun _ => false
247 end.
248 Fixpoint is_tighter_than {t} : interp t -> interp t -> bool
249 := match t with
250 | base.type.type_base _ as t
251 => fun r1 r2
252 => match r1, r2 with
253 | _, Datatypes.None => true
254 | Datatypes.None, Datatypes.Some _ => false
255 | Datatypes.Some r1, Datatypes.Some r2 => base.is_tighter_than (t:=t) r1 r2
256 end
257 | base.type.prod A B
258 => fun '(a, b) '(a', b')
259 => @is_tighter_than A a a' && @is_tighter_than B b b'
260 | base.type.list A
261 => fun ls1 ls2
262 => match ls1, ls2 with
263 | Datatypes.None, Datatypes.None => true
264 | Datatypes.Some _, Datatypes.None => true
265 | Datatypes.None, Datatypes.Some _ => false
266 | Datatypes.Some ls1, Datatypes.Some ls2 => fold_andb_map (@is_tighter_than A) ls1 ls2
267 end
268 | base.type.option A
269 => fun v1 v2
270 => match v1, v2 with
271 | Datatypes.None, Datatypes.None => true
272 | Datatypes.Some _, Datatypes.None => true
273 | Datatypes.None, Datatypes.Some _ => false
274 | Datatypes.Some v1, Datatypes.Some v2 => option_beq (@is_tighter_than A) v1 v2
275 end
276 | base.type.unit => fun 'tt 'tt => true
277 end.
278 Fixpoint interp_beq {t1 t2} : interp t1 -> interp t2 -> bool
279 := match t1, t2 return interp t1 -> interp t2 -> bool with
280 | base.type.type_base _ as t1, base.type.type_base _ as t2 => option_beq_hetero base.interp_beq
281 | base.type.prod A1 B1, base.type.prod A2 B2
282 => fun '(a, b) '(a', b')
283 => @interp_beq A1 A2 a a' && @interp_beq B1 B2 b b'
284 | base.type.list A1, base.type.list A2
285 => option_beq_hetero (list_beq_hetero (@interp_beq A1 A2))
286 | base.type.option A1, base.type.option A2
287 => option_beq_hetero (option_beq_hetero (@interp_beq A1 A2))
288 | base.type.unit, base.type.unit => fun 'tt 'tt => true
289 | base.type.type_base _, _
290 | base.type.unit, _
291 | base.type.prod _ _, _
292 | base.type.list _, _
293 | base.type.option _, _
294 => fun _ _ => false
295 end.
296
297 Global Instance reflect_interp_eq {t} : reflect_rel (@eq _) (@interp_beq t t).
298 Proof.
299 induction t; cbn [interp_beq]; intros; break_innermost_match; try exact _.
300 Defined.
301
302 Lemma interp_beq_bl {t x y} : @interp_beq t t x y = true -> x = y.
303 Proof. apply reflect_to_beq; exact _. Qed.
304 Lemma interp_beq_lb {t x y} : x = y -> @interp_beq t t x y = true.
305 Proof. apply reflect_to_beq; exact _. Qed.
306
307 Fixpoint union {t} : interp t -> interp t -> interp t
308 := match t return interp t -> interp t -> interp t with
309 | base.type.type_base base.type.Z => Option.map2 ZRange.union
310 | base.type.type_base _ as t
311 | base.type.unit as t
312 => fun x y
313 => if @interp_beq t t x y
314 then x
315 else None
316 | base.type.prod A B => fun '(a, b) '(a', b') =>
317 (@union A a a', @union B b b')
318 | base.type.list A => fun la lb => Option.bind2 la lb (fun la lb =>
319 if Nat.eqb (length la) (length lb)
320 then Datatypes.Some (List.map (uncurry (@union A)) (List.combine la lb))
321 else Datatypes.None)
322 | base.type.option A => fun oa ob => Option.bind2 oa ob (fun l r =>
323 match l, r return option (option (interp A)) with
324 | Datatypes.Some l, Datatypes.Some r => Datatypes.Some (Datatypes.Some (union l r))
325 | Datatypes.None, Datatypes.None => Datatypes.Some Datatypes.None
326 | _, _ => Datatypes.None
327 end)
328 end.
329
330 Fixpoint is_bounded_by {t} : interp t -> binterp t -> bool
331 := match t with
332 | base.type.type_base base.type.Z as t
333 | base.type.type_base _ as t
334 => fun r
335 => match r with
336 | Datatypes.Some r => @base.is_bounded_by t r
337 | Datatypes.None => fun _ => true
338 end
339 | base.type.prod A B
340 => fun '(a, b) '(a', b')
341 => @is_bounded_by A a a' && @is_bounded_by B b b'
342 | base.type.list A
343 => fun ls1 ls2
344 => match ls1 with
345 | Datatypes.None => true
346 | Datatypes.Some ls1 => fold_andb_map (@is_bounded_by A) ls1 ls2
347 end
348 | base.type.option A
349 => fun v1 v2
350 => match v1 with
351 | Datatypes.None => true
352 | Datatypes.Some v1 => option_beq_hetero (@is_bounded_by A) v1 v2
353 end
354 | base.type.unit => fun 'tt _ => true
355 end.
356
357 Lemma is_bounded_by_Some {t} r val
358 : is_bounded_by (@Some t r) val = base.is_bounded_by r val.
359 Proof.
360 induction t;
361 repeat first [ reflexivity
362 | progress cbn in *
363 | progress destruct_head'_prod
364 | progress destruct_head' base.type.base
365 | progress destruct_head' option
366 | rewrite fold_andb_map_map1
367 | match goal with H : _ |- _ => rewrite H end
368 | match goal with H : _ |- _ => setoid_rewrite H end ].
369 Qed.
370
371 Lemma is_tighter_than_is_bounded_by {t} r1 r2 val
372 (Htight : @is_tighter_than t r1 r2 = true)
373 (Hbounds : is_bounded_by r1 val = true)
374 : is_bounded_by r2 val = true.
375 Proof.
376 induction t;
377 repeat first [ progress destruct_head'_prod
378 | progress destruct_head'_and
379 | progress destruct_head'_unit
380 | progress destruct_head' option
381 | progress cbn [interp binterp base.interp is_bounded_by is_tighter_than base.is_tighter_than base.is_bounded_by option_beq_hetero] in *
382 | progress subst
383 | reflexivity
384 | progress break_innermost_match_hyps
385 | solve [ eauto with nocore ]
386 | progress cbv [ZRange.is_bounded_by_bool is_tighter_than_bool] in *
387 | progress rewrite ?Bool.andb_true_iff in *
388 | discriminate
389 | apply conj
390 | Z.ltb_to_lt; lia
391 | match goal with
392 | [ H : context[@Compilers.base.interp_beq _ _ ?base_interp_beq ?t] |- _ ]
393 => progress Reflect.reflect_beq_to_eq (@Compilers.base.interp_beq _ _ base_interp_beq t)
394 | [ |- context[@Compilers.base.interp_beq _ _ ?base_interp_beq ?t] ]
395 => progress Reflect.reflect_beq_to_eq (@Compilers.base.interp_beq _ _ base_interp_beq t)
396 end ].
397 { lazymatch goal with
398 | [ r1 : list (interp t), r2 : list (interp t), val : list (binterp t) |- _ ]
399 => revert r1 r2 val Htight Hbounds IHt
400 end; intros r1 r2 val; revert r1 r2 val.
401 induction r1, r2, val; cbn; auto with nocore; try congruence; [].
402 rewrite !Bool.andb_true_iff; intros; destruct_head'_and; split; eauto with nocore. }
403 Qed.
404
405 Lemma is_tighter_than_Some_is_bounded_by {t} r1 r2 val
406 (Htight : @is_tighter_than t r1 (Some r2) = true)
407 (Hbounds : is_bounded_by r1 val = true)
408 : base.is_bounded_by r2 val = true.
409 Proof.
410 rewrite <- is_bounded_by_Some.
411 eapply is_tighter_than_is_bounded_by; eassumption.
412 Qed.
413 End option.
414 End base.
415
416 (** turn a [type] into a [Set] describing the type of bounds on
417 that type; this lifts [base.interp] from
418 [type.base] to [type] *)
419 Definition interp (t : type base.type)
420 := type.interp base.interp t.
421 Definition is_tighter_than {t} : interp t -> interp t -> bool
422 := match t with
423 | type.base x => @base.is_tighter_than x
424 | type.arrow s d => fun _ _ => false
425 end.
426 Definition is_bounded_by {t} : interp t -> einterp t -> bool
427 := match t return interp t -> einterp t -> bool with
428 | type.base x => @base.is_bounded_by x
429 | type.arrow s d => fun _ _ => false
430 end.
431 Fixpoint bitwidth (signed : bool) {t} : interp t -> einterp t
432 := match t with
433 | type.base x => @base.bitwidth x
434 | type.arrow s d => fun fr sz => @bitwidth signed d (fr (@of_bitwidth signed s sz))
435 end
436 with of_bitwidth (signed : bool) {t} : einterp t -> interp t
437 := match t with
438 | type.base x => @base.of_bitwidth signed x
439 | type.arrow s d => fun fr sz => @of_bitwidth signed d (fr (@bitwidth signed s sz))
440 end.
441 Notation of_bitwidth_unsigned := (@of_bitwidth false).
442 Notation of_bitwidth_signed := (@of_bitwidth true).
443 Module option.
444 (** turn a [type] into a [Set] describing the type of optional
445 bounds on that base type; bounds on a [Z] may be either a
446 range, or [None], generally indicating that the [Z] is
447 unbounded. This lifts [base.option.interp] from
448 [base.type] to [type] *)
449 Definition interp (t : type base.type)
450 := tinterp_gen base.option.interp t.
451 Fixpoint None {t : type base.type} : interp t
452 := match t with
453 | type.base x => @base.option.None x
454 | type.arrow s d => fun _ => @None d
455 end.
456 Definition Some {t : type base.type} : type.interp t -> interp t
457 := match t with
458 | type.base x => @base.option.Some x
459 | type.arrow s d => fun _ _ => @None d
460 end.
461 Fixpoint strip_ranges {t : type base.type} : interp t -> interp t
462 := match t with
463 | type.base x => @base.option.strip_ranges x
464 | type.arrow s d => fun f x => @strip_ranges d (f x)
465 end.
466 Definition is_tighter_than {t} : interp t -> interp t -> bool
467 := match t with
468 | type.base x => @base.option.is_tighter_than x
469 | type.arrow s d => fun _ _ => false
470 end.
471 Definition is_bounded_by {t} : interp t -> einterp t -> bool
472 := match t with
473 | type.base x => @base.option.is_bounded_by x
474 | type.arrow s d => fun _ _ => false
475 end.
476
477 Lemma is_bounded_by_Some {t} r val
478 : is_bounded_by (@Some t r) val = type.is_bounded_by r val.
479 Proof.
480 induction t; [ apply base.option.is_bounded_by_Some | reflexivity ].
481 Qed.
482
483 Lemma is_tighter_than_is_bounded_by {t} r1 r2 val
484 (Htight : @is_tighter_than t r1 r2 = true)
485 (Hbounds : is_bounded_by r1 val = true)
486 : is_bounded_by r2 val = true.
487 Proof.
488 induction t; cbn in *;
489 eauto using base.option.is_tighter_than_is_bounded_by.
490 Qed.
491
492 Lemma is_tighter_than_Some_is_bounded_by {t} r1 r2 val
493 (Htight : @is_tighter_than t r1 (Some r2) = true)
494 (Hbounds : is_bounded_by r1 val = true)
495 : type.is_bounded_by r2 val = true.
496 Proof.
497 rewrite <- is_bounded_by_Some.
498 eapply is_tighter_than_is_bounded_by; eassumption.
499 Qed.
500 End option.
501 End type.
502
503 Module ident.
504 Module option.
505 Local Open Scope zrange_scope.
506
507 Fixpoint of_literal {t} : base.interp t -> type.base.option.interp t
508 := match t with
509 | base.type.type_base base.type.Z => fun z => Some r[z~>z]%zrange
510 | base.type.type_base _
511 => fun n => Some n
512 | base.type.unit
513 => fun _ => tt
514 | base.type.prod A B
515 => fun '(a, b) => (@of_literal A a, @of_literal B b)
516 | base.type.list A
517 => fun ls => Some (List.map (@of_literal A) ls)
518 | base.type.option A
519 => fun v => Some (option_map (@of_literal A) v)
520 end.
521 Fixpoint to_literal {t} : type.base.option.interp t -> option (base.interp t)
522 := match t with
523 | base.type.type_base base.type.Z => fun r => r <- r; if r.(lower) =? r.(upper) then Some r.(lower) else None
524 | base.type.type_base _
525 => fun v => v
526 | base.type.unit
527 => fun _ => Some tt
528 | base.type.prod A B
529 => fun '(a, b) => a <- @to_literal A a; b <- @to_literal B b; Some (a, b)
530 | base.type.list A
531 => fun ls => ls <- ls; Option.List.lift (List.map (@to_literal A) ls)
532 | base.type.option A
533 => fun v => v <- v; Option.lift (option_map (@to_literal A) v)
534 end%option%Z.
535 Local Notation rSome v
536 := (ZRange.type.base.option.Some (t:=base.reify_norm_type_of v) v)
537 (only parsing).
538 (** do bounds analysis on identifiers; take in optional bounds
539 on arguments, return optional bounds on outputs. *)
540 (** Casts are like assertions; we only guarantee anything when they're true *)
541 (** If [r] is [None], that means we had a cast with a
542 non-literal range. If [v] is [None], that means we don't
543 know anything about bounds on [v]. *)
544 Definition interp_Z_cast (r : option zrange) (v : option zrange) : option zrange
545 := match r, v with
546 | Some r, Some v
547 => if is_tighter_than_bool v r (* the value is definitely inside the range *)
548 then Some v
549 else None
550 | _, _ => None
551 end.
552 (** Here we guarantee truncating/modular behavior of casting *)
553 Definition interp_Z_cast_truncate (r : option zrange) (v : option zrange) : option zrange
554 := match interp_Z_cast r v, r with
555 | Some x, _ => Some x
556 | None, Some r => Some (ZRange.normalize r)
557 | None, None => None
558 end.
559 Local Notation tZ := (base.type.type_base base.type.Z).
560 Definition interp {shiftr_avoid_uint1 : shiftr_avoid_uint1_opt} (assume_cast_truncates : bool) {t} (idc : ident t) : type.option.interp t
561 := let interp_Z_cast := if assume_cast_truncates then interp_Z_cast_truncate else interp_Z_cast in
562 match idc in ident.ident t return type.option.interp t with
563 | ident.Literal t v => @of_literal (base.type.type_base t) v
564 | ident.comment _
565 | ident.comment_no_keep _
566 => fun _ => tt
567 | ident.tt as idc
568 => ident.interp idc
569 | ident.Nat_succ as idc
570 | ident.Nat_pred as idc
571 => option_map (ident.interp idc)
572 | ident.Z_of_nat as idc
573 | ident.Z_pos as idc
574 => option_map (fun n => r[ident.interp idc n~>ident.interp idc n]%zrange)
575 | ident.Z_to_nat as idc
576 | ident.Z_to_pos as idc
577 => fun v => v <- to_literal v; Some (ident.interp idc v)
578 | ident.List_length _
579 => option_map (@List.length _)
580 | ident.value_barrier
581 => fun x => x
582 | ident.Pos_mul as idc
583 | ident.Pos_add as idc
584 | ident.Nat_max as idc
585 | ident.Nat_mul as idc
586 | ident.Nat_add as idc
587 | ident.Nat_sub as idc
588 | ident.Nat_eqb as idc
589 | ident.List_seq as idc
590 => fun x y => x <- x; y <- y; rSome (ident.interp idc x y)
591 | ident.List_repeat _
592 => fun x y => y <- y; Some (repeat x y)
593 | ident.List_firstn _
594 => fun n ls => n <- n; ls <- ls; Some (firstn n ls)
595 | ident.List_skipn _
596 => fun n ls => n <- n; ls <- ls; Some (skipn n ls)
597 | ident.List_combine _ _
598 => fun x y => x <- x; y <- y; Some (List.combine x y)
599 | ident.List_flat_map _ _
600 => fun f ls
601 => (ls <- ls;
602 let fls := List.map f ls in
603 List.fold_right
604 (fun ls1 ls2 => ls1 <- ls1; ls2 <- ls2; Some (ls1 ++ ls2))
605 (Some nil)
606 fls)
607 | ident.List_partition _
608 => fun f ls
609 => match ls with
610 | Some ls
611 => list_rect
612 _
613 (Some nil, Some nil)
614 (fun x tl partition_tl
615 => let '(g, d) := partition_tl in
616 ((fx <- f x;
617 if fx then (g <- g; Some (x::g)) else g),
618 (fx <- f x;
619 if fx then d else (d <- d; Some (x::d)))))
620 ls
621 | None => (None, None)
622 end
623 | ident.List_filter _
624 => fun f ls
625 => match ls with
626 | Some xs
627 => list_rect
628 _
629 (Some nil)
630 (fun x xs' filter_xs'
631 => (fx <- f x;
632 if fx then (filter_xs' <- filter_xs'; Some (x::filter_xs')) else filter_xs'))
633 xs
634 | None => None
635 end
636 | ident.Z_eqb as idc
637 | ident.Z_pow as idc
638 | ident.Z_lxor as idc
639 | ident.Z_modulo as idc
640 | ident.Build_zrange as idc
641 => fun x y => match to_literal x, to_literal y with
642 | Some x, Some y => of_literal (ident.interp idc x y)
643 | _, _ => ZRange.type.base.option.None
644 end
645 | ident.Z_ltz as idc
646 => fun x y => match to_literal x, to_literal y with
647 | Some x, Some y => of_literal (ident.interp idc x y)
648 | _, _ => Some r[0~>1]
649 end
650 | ident.Z_bneg as idc
651 => fun x => match to_literal x with
652 | Some x => of_literal (ident.interp idc x)
653 | None => Datatypes.Some r[0~>1]
654 end
655 | ident.Z_lnot_modulo as idc
656 => fun v m
657 => match to_literal m, to_literal v with
658 | Some m, Some v => of_literal (ident.interp idc v m)
659 | Some m, None => (if (0 <? m)%Z
660 then Some r[0 ~> m-1]
661 else if (m =? 0)%Z
662 then ltac:(match eval hnf in (1 mod 0) with | 0 => exact r[0 ~> 0] | _ => exact None (* if `_ mod 0` is not constant and neither is `v`, we do not bother computing the bounds of `Z.lnot v` *) end)
663 else Some r[m+1 ~> 0])
664 | _, _ => None
665 end
666 | ident.Z_truncating_shiftl as idc
667 => fun bw x y => match to_literal bw, to_literal x, to_literal y with
668 | Some bw, Some x, Some y => of_literal (ident.interp idc bw x y)
669 | Some bw, _, _
670 => x <- x;
671 y <- y;
672 Some (ZRange.land_bounds
673 (ZRange.four_corners_and_zero Z.shiftl x y)
674 (ZRange.constant (Z.ones (ltac:(match eval hnf in (1 mod 0) with | 0 => exact (Z.max 0 bw) | _ => exact bw end)))))
675 | None, _, _ => None
676 end
677 | ident.bool_rect _
678 => fun t f b
679 => match b with
680 | Some b => if b then t tt else f tt
681 | None => type.base.option.union (t tt) (f tt)
682 end
683 | ident.bool_rect_nodep _
684 => fun t f b
685 => match b with
686 | Some b => if b then t else f
687 | None => type.base.option.union t f
688 end
689 | ident.option_rect _ _
690 => fun s n o
691 => match o with
692 | Some o => option_rect _ s (n tt) o
693 | None => ZRange.type.base.option.None
694 end
695 | ident.zrange_rect _
696 => fun f v
697 => match v with
698 | Some v => ZRange.zrange_rect
699 _
700 (fun l u => f (Some (ZRange.constant l)) (Some (ZRange.constant u)))
701 v
702 | None => ZRange.type.base.option.None
703 end
704 | ident.nat_rect _
705 | ident.eager_nat_rect _
706 => fun O_case S_case n
707 => match n with
708 | Some n
709 => nat_rect
710 _
711 (O_case tt)
712 (fun n' rec => S_case (Some n') rec)
713 n
714 | None => ZRange.type.base.option.None
715 end
716 | ident.nat_rect_arrow _ _ as idc
717 | ident.eager_nat_rect_arrow _ _ as idc
718 | ident.nat_rect_fbb_b _ _ _ as idc
719 | ident.nat_rect_fbb_b_b _ _ _ _ as idc
720 => fun O_case S_case n
721 => let t := ((fun t (idc : ident (_ -> _ -> _ -> t)) => t) _ idc) in
722 match n return type.option.interp t with
723 | Some n
724 => nat_rect
725 _
726 O_case
727 (fun n' rec => S_case (Some n') rec)
728 n
729 | None => ZRange.type.option.None
730 end
731 | ident.list_rect _ _
732 | ident.eager_list_rect _ _
733 => fun N C ls
734 => match ls with
735 | Some ls
736 => list_rect
737 _
738 (N tt)
739 (fun x xs rec => C x (Some xs) rec)
740 ls
741 | None => ZRange.type.base.option.None
742 end
743 | ident.list_rect_arrow _ _ _ as idc
744 | ident.eager_list_rect_arrow _ _ _ as idc
745 | ident.list_rect_fbb_b _ _ _ _ as idc
746 | ident.list_rect_fbb_b_b _ _ _ _ _ as idc
747 | ident.list_rect_fbb_b_b_b _ _ _ _ _ _ as idc
748 | ident.list_rect_fbb_b_b_b_b _ _ _ _ _ _ _ as idc
749 | ident.list_rect_fbb_b_b_b_b_b _ _ _ _ _ _ _ _ as idc
750 => fun N C ls
751 => let t := ((fun t (idc : ident (_ -> _ -> _ -> t)) => t) _ idc) in
752 match ls return type.option.interp t with
753 | Some ls
754 => list_rect
755 _
756 N
757 (fun x xs rec => C x (Some xs) rec)
758 ls
759 | None => ZRange.type.option.None
760 end
761 | ident.list_case _ _
762 => fun N C ls
763 => match ls with
764 | Some ls
765 => list_case
766 _
767 (N tt)
768 (fun x xs => C x (Some xs))
769 ls
770 | None => ZRange.type.base.option.None
771 end
772 | ident.List_fold_right _ _
773 => fun f v ls
774 => match ls with
775 | Some ls
776 => fold_right f v ls
777 | None => ZRange.type.base.option.None
778 end
779 | ident.eager_List_nth_default _
780 | ident.List_nth_default _
781 => fun d ls n
782 => match ls, n with
783 | Some ls, Some n
784 => nth_default d ls n
785 | _, _ => ZRange.type.base.option.None
786 end
787 | ident.List_update_nth _
788 => fun n f ls => ls <- ls; n <- n; Some (update_nth n f ls)
789 | ident.nil t => Some nil
790 | ident.cons t => fun x => option_map (cons x)
791 | ident.None t => Some None
792 | ident.Some t => fun x => Some (Some x)
793 | ident.pair A B => pair
794 | ident.fst A B => fst
795 | ident.snd A B => snd
796 | ident.prod_rect A B P => fun f '(a, b) => f a b
797 | ident.List_map _ _
798 => fun f ls => ls <- ls; Some (List.map f ls)
799 | ident.List_app _
800 => fun ls1 ls2 => ls1 <- ls1; ls2 <- ls2; Some (List.app ls1 ls2)
801 | ident.List_rev _ => option_map (@List.rev _)
802 | ident.Z_leb as idc
803 | ident.Z_ltb as idc
804 | ident.Z_geb as idc
805 | ident.Z_gtb as idc
806 => fun x y => x <- x; y <- y; ZRange.ToConstant.four_corners Bool.eqb (ident.interp idc) x y
807 | ident.Z_opp as idc
808 | ident.Z_log2 as idc
809 | ident.Z_log2_up as idc
810 => fun x => x <- x; Some (ZRange.two_corners (ident.interp idc) x)
811 | ident.Z_abs as idc
812 => fun x => x <- x; Some (ZRange.two_corners_and_zero (ident.interp idc) x)
813 | ident.Z_max as idc
814 | ident.Z_min as idc
815 | ident.Z_add as idc
816 | ident.Z_mul as idc
817 | ident.Z_sub as idc
818 => fun x y => x <- x; y <- y; Some (ZRange.four_corners (ident.interp idc) x y)
819 | ident.Z_div as idc
820 | ident.Z_shiftl as idc
821 => fun x y => x <- x; y <- y; Some (ZRange.four_corners_and_zero (ident.interp idc) x y)
822 | ident.Z_shiftr as idc
823 => fun x y => x <- x; y <- y; Some (let r := ZRange.four_corners_and_zero (ident.interp idc) x y in
824 (* kludge to avoid uint1 after >> *)
825 if shiftr_avoid_uint1 && (r =? r[0~>1]) then r[0~>2] else r)
826 | ident.Z_add_with_carry as idc
827 => fun x y z => x <- x; y <- y; z <- z; Some (ZRange.eight_corners (ident.interp idc) x y z)
828 | ident.Z_cc_m as idc
829 => fun s x => s <- to_literal s; x <- x; Some (ZRange.two_corners (ident.interp idc s) x)
830 | ident.Z_rshi as idc
831 => fun s x y offset
832 => s <- to_literal s; x <- x; y <- y; offset <- to_literal offset;
833 if (0 <? s) then Some r[0~>s-1] else None
834 | ident.Z_land
835 => fun x y => x <- x; y <- y; Some (ZRange.land_bounds x y)
836 | ident.Z_lor
837 => fun x y => x <- x; y <- y; Some (ZRange.lor_bounds x y)
838 | ident.Z_mul_split
839 => fun split_at x y
840 => match to_literal split_at, x, y with
841 | Some split_at, Some x, Some y
842 => ZRange.type.base.option.Some
843 (t:=tZ*tZ)
844 (ZRange.split_bounds (ZRange.four_corners Z.mul x y) split_at)
845 | _, _, _ => ZRange.type.base.option.None
846 end
847 | ident.Z_mul_high
848 => fun split_at x y
849 => match to_literal split_at, x, y with
850 | Some split_at, Some x, Some y
851 => ZRange.type.base.option.Some
852 (t:=tZ)
853 (snd (ZRange.split_bounds (ZRange.four_corners Z.mul x y) split_at))
854 | _, _, _ => ZRange.type.base.option.None
855 end
856 | ident.Z_add_get_carry
857 => fun split_at x y
858 => match to_literal split_at, x, y with
859 | Some split_at, Some x, Some y
860 => ZRange.type.base.option.Some
861 (t:=tZ*tZ)
862 (ZRange.split_bounds (ZRange.four_corners Z.add x y) split_at)
863 | _, _, _ => ZRange.type.base.option.None
864 end
865 | ident.Z_add_with_get_carry
866 => fun split_at x y z
867 => match to_literal split_at, x, y, z with
868 | Some split_at, Some x, Some y, Some z
869 => ZRange.type.base.option.Some
870 (t:=tZ*tZ)
871 (ZRange.split_bounds
872 (ZRange.eight_corners (fun x y z => (x + y + z)%Z) x y z)
873 split_at)
874 | _, _, _, _ => ZRange.type.base.option.None
875 end
876 | ident.Z_sub_get_borrow
877 => fun split_at x y
878 => match to_literal split_at, x, y with
879 | Some split_at, Some x, Some y
880 => ZRange.type.base.option.Some
881 (t:=tZ*tZ)
882 (let b := ZRange.split_bounds (ZRange.four_corners BinInt.Z.sub x y) split_at in
883 (* N.B. sub_get_borrow returns - ((x - y) / split_at) as the borrow, so we need to negate *)
884 (fst b, ZRange.opp (snd b)))
885 | _, _, _ => ZRange.type.base.option.None
886 end
887 | ident.Z_sub_with_get_borrow
888 => fun split_at x y z
889 => match to_literal split_at, x, y, z with
890 | Some split_at, Some x, Some y, Some z
891 => ZRange.type.base.option.Some
892 (t:=tZ*tZ)
893 (let b := ZRange.split_bounds (ZRange.eight_corners (fun x y z => (y - z - x)%Z) x y z) split_at in
894 (* N.B. sub_get_borrow returns - ((x - y) / split_at) as the borrow, so we need to negate *)
895 (fst b, ZRange.opp (snd b)))
896 | _, _, _, _ => ZRange.type.base.option.None
897 end
898 | ident.Z_zselect
899 => fun _ y z => y <- y; z <- z; Some (ZRange.union y z)
900 | ident.Z_add_modulo
901 => fun x y m
902 => (x <- x;
903 y <- y;
904 m <- m;
905 Some (ZRange.union
906 (ZRange.four_corners Z.add x y)
907 (ZRange.eight_corners (fun x y m => Z.max 0 (x + y - m))
908 x y m)))
909 | ident.Z_combine_at_bitwidth as idc
910 => fun bitwidth lo hi
911 => bitwidth <- to_literal bitwidth;
912 lo <- lo;
913 hi <- hi;
914 Some (ZRange.four_corners (ident.interp idc bitwidth) lo hi)
915 | ident.Z_cast
916 => fun (range : option zrange) (r : option zrange)
917 => interp_Z_cast range r
918 | ident.Z_cast2
919 => fun '((r1, r2) : option zrange * option zrange) '((r1', r2') : option zrange * option zrange)
920 => (interp_Z_cast r1 r1', interp_Z_cast r2 r2')
921 | ident.fancy_add
922 | ident.fancy_sub
923 => fun '((log2wordmax, imm), args)
924 => match to_literal (t:=tZ) log2wordmax, to_literal (t:=tZ) imm with
925 | Some log2wordmax, _
926 => let wordmax := 2^log2wordmax in
927 let r := r[0~>wordmax-1] in
928 if ZRange.type.base.option.is_tighter_than (t:=tZ*tZ) args (Some r, Some r)
929 then (Some r, Some r[0~>1])
930 else ZRange.type.base.option.None
931 | _, _ => ZRange.type.base.option.None
932 end
933 | ident.fancy_addc
934 | ident.fancy_subb
935 => fun '((log2wordmax, imm), args)
936 => match to_literal (t:=tZ) log2wordmax, to_literal (t:=tZ) imm with
937 | Some log2wordmax, _
938 => let wordmax := 2^log2wordmax in
939 let r := r[0~>wordmax-1] in
940 if ZRange.type.base.option.is_tighter_than (t:=tZ*tZ*tZ) args (Some r[0~>1], Some r, Some r)
941 then (Some r, Some r[0~>1])
942 else ZRange.type.base.option.None
943 | _, _ => ZRange.type.base.option.None
944 end
945 | ident.fancy_mulll
946 | ident.fancy_mullh
947 | ident.fancy_mulhl
948 | ident.fancy_mulhh
949 => fun '(log2wordmax, args)
950 => match to_literal (t:=tZ) log2wordmax with
951 | Some log2wordmax
952 => let wordmax := 2^log2wordmax in
953 let r := r[0~>wordmax-1] in
954 if ZRange.type.base.option.is_tighter_than (t:=tZ*tZ) args (Some r, Some r)
955 then if (Z.eqb (log2wordmax mod 2) 0)
956 then Some r
957 else ZRange.type.base.option.None
958 else ZRange.type.base.option.None
959 | _ => ZRange.type.base.option.None
960 end
961 | ident.fancy_rshi as idc
962 => fun '((log2wordmax, n), args)
963 => match to_literal (t:=tZ) log2wordmax, to_literal (t:=tZ) n with
964 | Some log2wordmax, Some n
965 => let wordmax := 2^log2wordmax in
966 let r := r[0~>wordmax-1] in
967 let r_nbits := r[0~>2^n-1] in
968 if (0 <=? log2wordmax)%Z
969 then if (ZRange.type.base.option.is_tighter_than (t:=tZ*tZ) args (Some r_nbits, Some r) && (0 <=? n)%Z)
970 then
971 hi_range <- fst args;
972 lo_range <- snd args;
973 Some (ZRange.four_corners (fun x y => ident.interp idc ((log2wordmax, n), (x, y))) hi_range lo_range)
974 else if ZRange.type.base.option.is_tighter_than (t:=tZ*tZ) args (Some r, Some r)
975 then Some r
976 else ZRange.type.base.option.None
977 else ZRange.type.base.option.None
978 | _, _ => ZRange.type.base.option.None
979 end
980 | ident.fancy_selm
981 => fun '(_, (_, y, z)) => y <- y; z <- z; Some (ZRange.union y z)
982 | ident.fancy_selc
983 | ident.fancy_sell
984 => fun '(_, y, z) => y <- y; z <- z; Some (ZRange.union y z)
985 | ident.fancy_addm
986 => fun '(x, y, m)
987 => (x <- x;
988 y <- y;
989 m <- m;
990 Some (ZRange.union
991 (ZRange.four_corners Z.add x y)
992 (ZRange.eight_corners (fun x y m => Z.max 0 (x + y - m))
993 x y m)))
994 end%option.
995 End option.
996 End ident.
997 End ZRange.
998 Export ZRange.Settings.
999 End Compilers.