Debian Codegraph @

src/AbstractInterpretation/AbstractInterpretation.v

raw
1 From Coq Require Import ZArith.
2 Require Import Crypto.Util.ListUtil Coq.Lists.List Crypto.Util.ListUtil.FoldBool.
3 Require Import Crypto.Util.ZRange.
4 Require Import Crypto.Util.ZRange.Operations.
5 Require Import Crypto.Util.Option.
6 Require Import Crypto.Util.OptionList.
7 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
8 Require Import Crypto.Util.Bool.Reflect.
9 Require Import Crypto.Util.LetIn.
10 Require Import Rewriter.Language.Language.
11 Require Import Crypto.Language.InversionExtra.
12 Require Import Crypto.Language.API.
13 Require Import Crypto.AbstractInterpretation.ZRange.
14 Require Import Rewriter.Language.UnderLets.
15 Import ListNotations. Local Open Scope bool_scope. Local Open Scope Z_scope.
16
17 Module Compilers.
18 Export Language.Compilers.
19 Export UnderLets.Compilers.
20 Export Language.API.Compilers.
21 Export AbstractInterpretation.ZRange.Compilers.
22 Import invert_expr.
23 Import Language.InversionExtra.Compilers.
24
25 (** XXX TODO: Do we still need to do UnderLets here? *)
26 Module partial.
27 Import UnderLets.
28 Section with_var.
29 Context {base_type : Type}.
30 Local Notation type := (type base_type).
31 Let type_base (x : base_type) : type := type.base x.
32 Local Coercion type_base : base_type >-> type.
33 Context {ident : type -> Type}
34 {var : type -> Type}.
35 Local Notation expr := (@expr base_type ident).
36 Local Notation UnderLets := (@UnderLets base_type ident var).
37 Context (abstract_domain' : base_type -> Type)
38 (annotate : forall (is_let_bound : bool) t, abstract_domain' t -> @expr var t -> UnderLets (@expr var t))
39 (bottom' : forall A, abstract_domain' A)
40 (skip_annotations_under : forall t, ident t -> bool).
41
42 Definition abstract_domain (t : type)
43 := type.interp abstract_domain' t.
44
45 Fixpoint value (t : type)
46 := match t return Type (* COQBUG(https://github.com/coq/coq/issues/7727) *) with
47 | type.base t
48 => abstract_domain t * @expr var t
49 | type.arrow s d
50 => value s -> UnderLets (value d)
51 end%type.
52
53 Definition value_with_lets (t : type)
54 := UnderLets (value t).
55
56 Context (interp_ident : bool (* annotate with state? *) -> forall t, ident t -> value_with_lets t).
57
58 Fixpoint bottom {t} : abstract_domain t
59 := match t with
60 | type.base t => bottom' t
61 | type.arrow s d => fun _ => @bottom d
62 end.
63
64 Fixpoint bottom_for_each_lhs_of_arrow {t} : type.for_each_lhs_of_arrow abstract_domain t
65 := match t return type.for_each_lhs_of_arrow abstract_domain t with
66 | type.base t => tt
67 | type.arrow s d => (bottom, @bottom_for_each_lhs_of_arrow d)
68 end.
69
70 Definition state_of_value {t} : value t -> abstract_domain t
71 := match t return value t -> abstract_domain t with
72 | type.base t => fun '(st, v) => st
73 | type.arrow s d => fun _ => bottom
74 end.
75
76 (** We need to make sure that we ignore the state of
77 higher-order arrows *everywhere*, or else the proofs don't go
78 through. So we sometimes need to replace the state of
79 arrow-typed values with [⊥]. *)
80 Fixpoint bottomify {t} : value t -> value_with_lets t
81 := match t return value t -> value_with_lets t with
82 | type.base t => fun '(st, v) => Base (bottom' t, v)
83 | type.arrow s d => fun f => Base (fun x => fx <-- f x; @bottomify d fx)
84 end%under_lets.
85
86 (** We drop the state of higher-order arrows *)
87 Fixpoint reify (annotate_with_state : bool) (is_let_bound : bool) {t} : value t -> type.for_each_lhs_of_arrow abstract_domain t -> UnderLets (@expr var t)
88 := match t return value t -> type.for_each_lhs_of_arrow abstract_domain t -> UnderLets (@expr var t) with
89 | type.base t
90 => fun '(st, v) 'tt
91 => if annotate_with_state
92 then annotate is_let_bound t st v
93 else if is_let_bound
94 then UnderLets.UnderLet v (fun v' => UnderLets.Base ($$v'))
95 else UnderLets.Base v
96 | type.arrow s d
97 => fun f_e '(sv, dv)
98 => let sv := match s with
99 | type.base _ => sv
100 | type.arrow _ _ => bottom
101 end in
102 Base
103 (λ x , (UnderLets.to_expr
104 (fx <-- f_e (@reflect annotate_with_state _ (expr.Var x) sv);
105 @reify annotate_with_state false _ fx dv)))
106 end%core%expr
107 with reflect (annotate_with_state : bool) {t} : @expr var t -> abstract_domain t -> value t
108 := match t return @expr var t -> abstract_domain t -> value t with
109 | type.base t
110 => fun e st => (st, e)
111 | type.arrow s d
112 => fun e absf
113 => (fun v
114 => let stv := state_of_value v in
115 (rv <-- (@reify annotate_with_state false s v bottom_for_each_lhs_of_arrow);
116 Base (@reflect annotate_with_state d (e @ rv) (absf stv))%expr))
117 end%under_lets.
118
119 Definition skip_annotations_for_App {var'} {t} (e : @expr var' t) : bool
120 := match invert_AppIdent_curried e with
121 | Some (existT _ (idc, args)) => skip_annotations_under _ idc
122 | None => false
123 end.
124
125 Fixpoint interp (annotate_with_state : bool) {t} (e : @expr value_with_lets t) : value_with_lets t
126 := let annotate_with_state := annotate_with_state && negb (skip_annotations_for_App e) in
127 match e in expr.expr t return value_with_lets t with
128 | expr.Ident t idc => interp_ident annotate_with_state _ idc (* Base (reflect (###idc) (abstract_interp_ident _ idc))*)
129 | expr.Var t v => v
130 | expr.Abs s d f => Base (fun x => @interp annotate_with_state d (f (Base x)))
131 | expr.App (type.base s) d f x
132 => (x' <-- @interp annotate_with_state _ x;
133 f' <-- @interp annotate_with_state (_ -> d)%etype f;
134 f' x')
135 | expr.App (type.arrow s' d') d f x
136 => (x' <-- @interp annotate_with_state (s' -> d')%etype x;
137 x'' <-- bottomify x';
138 f' <-- @interp annotate_with_state (_ -> d)%etype f;
139 f' x'')
140 | expr.LetIn (type.arrow _ _) B x f
141 => (x' <-- @interp annotate_with_state _ x;
142 @interp annotate_with_state _ (f (Base x')))
143 | expr.LetIn (type.base A) B x f
144 => (x' <-- @interp annotate_with_state _ x;
145 x'' <-- reify annotate_with_state true (* this forces a let-binder here *) x' tt;
146 @interp annotate_with_state _ (f (Base (reflect annotate_with_state x'' (state_of_value x')))))
147 end%under_lets.
148
149 Definition eval_with_bound' (annotate_with_state : bool) {t} (e : @expr value_with_lets t)
150 (st : type.for_each_lhs_of_arrow abstract_domain t)
151 : expr t
152 := UnderLets.to_expr (e' <-- interp annotate_with_state e; reify annotate_with_state false e' st).
153
154 Definition eval' {t} (e : @expr value_with_lets t) : expr t
155 := eval_with_bound' false e bottom_for_each_lhs_of_arrow.
156
157 Definition eta_expand_with_bound' {t} (e : @expr var t)
158 (st : type.for_each_lhs_of_arrow abstract_domain t)
159 : expr t
160 := UnderLets.to_expr (reify true false (reflect true e bottom) st).
161
162 Section extract.
163 Context (ident_extract : forall t, ident t -> abstract_domain t).
164
165 (** like [expr.interp (@ident_extract) e], except we replace
166 all higher-order state with bottom *)
167 Fixpoint extract' {t} (e : @expr abstract_domain t) : abstract_domain t
168 := match e in expr.expr t return abstract_domain t with
169 | expr.Ident t idc => ident_extract t idc
170 | expr.Var t v => v
171 | expr.Abs s d f => fun v : abstract_domain s => @extract' _ (f v)
172 | expr.App (type.base s) d f x
173 => @extract' _ f (@extract' _ x)
174 | expr.App (type.arrow s' d') d f x
175 => @extract' _ f (@bottom (type.arrow s' d'))
176 | expr.LetIn A B x f => dlet y := @extract' _ x in @extract' _ (f y)
177 end.
178
179 Definition extract_gen {t} (e : @expr abstract_domain t) (bound : type.for_each_lhs_of_arrow abstract_domain t)
180 : abstract_domain' (type.final_codomain t)
181 := type.app_curried (extract' e) bound.
182 End extract.
183 End with_var.
184
185 Module ident.
186 Section with_var.
187 Local Notation type := (type base.type).
188 Let type_base (x : base.type.base) : base.type := base.type.type_base x.
189 Let base {bt} (x : Language.Compilers.base.type bt) : type.type _ := type.base x.
190 Local Coercion base : base.type >-> type.type.
191 Local Coercion type_base : base.type.base >-> base.type.
192 Context {var : type -> Type}.
193 Local Notation expr := (@expr base.type ident).
194 Local Notation UnderLets := (@UnderLets base.type ident var).
195 Context (abstract_domain' : base.type -> Type).
196 Local Notation abstract_domain := (@abstract_domain base.type abstract_domain').
197 Local Notation value_with_lets := (@value_with_lets base.type ident var abstract_domain').
198 Local Notation value := (@value base.type ident var abstract_domain').
199 Context (annotate_expr : forall t, abstract_domain' t -> option (@expr var (t -> t)))
200 (bottom' : forall A, abstract_domain' A)
201 (abstract_interp_ident : forall t, ident t -> type.interp abstract_domain' t)
202 (extract_list_state : forall A, abstract_domain' (base.type.list A) -> option (list (abstract_domain' A)))
203 (extract_option_state : forall A, abstract_domain' (base.type.option A) -> option (option (abstract_domain' A)))
204 (is_annotated_for : forall t t', @expr var t -> abstract_domain' t' -> bool)
205 (annotation_to_cast : forall s d, @expr var (s -> d) -> option (@expr var s -> @expr var d))
206 (skip_annotations_under : forall t, ident t -> bool)
207 (strip_annotation : forall t, ident t -> option (value t)).
208
209 (** TODO: Is it okay to commute annotations? *)
210 Definition update_annotation {t} (st : abstract_domain' t) (e : @expr var t) : @expr var t
211 := match e, annotate_expr _ st with
212 | (cst' @ e'), Some cst
213 => if is_annotated_for _ _ cst' st
214 then e
215 else cst @ e
216 | _, Some cst => cst @ e
217 | _, None => e
218 end%expr_pat%expr.
219
220 Definition annotate_with_expr (is_let_bound : bool) {t}
221 (st : abstract_domain' t) (e : @expr var t)
222 : UnderLets (@expr var t)
223 := let cst_e := update_annotation st e (*match annotate_expr _ st with
224 | Some cst => ###cst @ e
225 | None => e
226 end%expr*) in
227 if is_let_bound
228 then UnderLet cst_e (fun v => Base ($$v)%expr)
229 else Base cst_e.
230
231 Definition annotate_base (is_let_bound : bool) {t : base.type.base}
232 (st : abstract_domain' t) (e : @expr var t)
233 : UnderLets (@expr var t)
234 := annotate_with_expr is_let_bound st e.
235
236 Fixpoint annotate (is_let_bound : bool) {t : base.type} : abstract_domain' t -> @expr var t -> UnderLets (@expr var t)
237 := match t return abstract_domain' t -> @expr var t -> UnderLets (@expr var t) with
238 | base.type.type_base t => annotate_base is_let_bound
239 | base.type.unit
240 => fun _ e
241 => (* we need to keep let-bound unit expressions around for comments *)
242 if is_let_bound
243 then UnderLet e (fun v => Base ($$v)%expr)
244 else Base e
245 | base.type.prod A B
246 => fun st e
247 => match invert_pair e with
248 | Some (x, y)
249 => let stx := abstract_interp_ident _ ident.fst st in
250 let sty := abstract_interp_ident _ ident.snd st in
251 (x' <-- @annotate is_let_bound A stx x;
252 y' <-- @annotate is_let_bound B sty y;
253 Base (x', y')%expr)
254 | None => annotate_with_expr is_let_bound st e
255 end
256 | base.type.list A
257 => fun st e
258 => match extract_list_state _ st, reflect_list e with
259 | Some ls_st, Some ls_e
260 => (retv <---- (List.map
261 (fun '(st', e') => @annotate is_let_bound A st' e')
262 (List.combine ls_st ls_e));
263 Base (reify_list retv))
264 | Some ls_st, None
265 => (retv <---- (List.map
266 (fun '(n, st')
267 => let e' := (#ident.List_nth_default @ DefaultValue.expr.base.default @ e @ ##(n:nat))%expr in
268 @annotate is_let_bound A st' e')
269 (List.combine (List.seq 0 (List.length ls_st)) ls_st));
270 Base (reify_list retv))
271 | None, _ => annotate_with_expr is_let_bound st e
272 end
273 | base.type.option A
274 => fun st e
275 => match extract_option_state _ st, reflect_option e with
276 | Some v_st, Some v_e
277 => (retv <----- (Option.map
278 (fun '(st', e') => @annotate is_let_bound A st' e')
279 (Option.combine v_st v_e));
280 Base (reify_option retv))
281 | Some _, None
282 | None, _
283 => annotate_with_expr is_let_bound st e
284 end
285 end%under_lets.
286
287 Local Notation reify := (@reify base.type ident var abstract_domain' annotate bottom').
288 Local Notation reflect := (@reflect base.type ident var abstract_domain' annotate bottom').
289
290 (** We manually rewrite with the rule for [nth_default], as the eliminator for eta-expanding lists in the input *)
291 Definition interp_ident (annotate_with_state : bool) {t} (idc : ident t) : value_with_lets t
292 := match idc in ident t return value_with_lets t with
293 | ident.List_nth_default T as idc
294 => let default := reflect annotate_with_state (###idc) (abstract_interp_ident _ idc) in
295 Base
296 (fun default_arg
297 => default <-- default default_arg;
298 Base
299 (fun ls_arg
300 => default <-- default ls_arg;
301 Base
302 (fun n_arg
303 => default <-- default n_arg;
304 ls' <-- @reify annotate_with_state false (base.type.list T) ls_arg tt;
305 Base
306 (fst default,
307 match reflect_list ls', invert_Literal (snd n_arg) with
308 | Some ls, Some n
309 => nth_default (snd default_arg) ls n
310 | _, _ => snd default
311 end))))
312 | idc => Base
313 match strip_annotation _ idc with
314 | Some v => v
315 | None => reflect annotate_with_state (###idc) (abstract_interp_ident _ idc)
316 end
317 end%core%under_lets%expr.
318
319 Fixpoint strip_all_annotations' (should_strip : bool) {t} (e : @expr var t) : @expr var t
320 := match e in expr.expr t return expr t with
321 | expr.Ident _ _ as e
322 | expr.Var _ _ as e
323 => e
324 | expr.Abs s d f
325 => expr.Abs (fun x => strip_all_annotations' should_strip (f x))
326 | expr.App s d f x
327 => let should_strip
328 := (should_strip || match invert_AppIdent_curried f with
329 | Some (existT _ (idc, _)) => skip_annotations_under _ idc
330 | None => false
331 end)%bool in
332 let f := strip_all_annotations' should_strip f in
333 let x := strip_all_annotations' should_strip x in
334 if should_strip
335 then match annotation_to_cast _ _ f with
336 | Some f => f x
337 | None => expr.App f x
338 end
339 else expr.App f x
340 | expr.LetIn A B x f
341 => expr.LetIn (strip_all_annotations' should_strip x) (fun v => strip_all_annotations' should_strip (f v))
342 end.
343 Definition strip_all_annotations {t} (e : @expr var t) : @expr var t
344 := @strip_all_annotations' false t e.
345
346 Definition eval_with_bound (skip_annotations_under : forall t, ident t -> bool) (annotate_with_state : bool) {t} (e : @expr value_with_lets t)
347 (st : type.for_each_lhs_of_arrow abstract_domain t)
348 : @expr var t
349 := @eval_with_bound' base.type ident var abstract_domain' annotate bottom' skip_annotations_under interp_ident annotate_with_state t e st.
350
351 Definition eval {t} (e : @expr value_with_lets t) : @expr var t
352 := @eval' base.type ident var abstract_domain' annotate bottom' (fun _ _ => false) interp_ident t e.
353
354 Definition eta_expand_with_bound {t} (e : @expr var t)
355 (st : type.for_each_lhs_of_arrow abstract_domain t)
356 : @expr var t
357 := @eta_expand_with_bound' base.type ident var abstract_domain' annotate bottom' t e st.
358
359 Definition extract {t} (e : @expr _ t) (bound : type.for_each_lhs_of_arrow abstract_domain t) : abstract_domain' (type.final_codomain t)
360 := @extract_gen base.type ident abstract_domain' bottom' abstract_interp_ident t e bound.
361 End with_var.
362 End ident.
363
364 Definition default_relax_zrange (v : zrange) : option zrange := Some v.
365
366 Section specialized.
367 Local Notation abstract_domain' := ZRange.type.base.option.interp.
368 Local Notation abstract_domain := (@partial.abstract_domain base.type abstract_domain').
369 Local Notation value_with_lets var := (@value_with_lets base.type ident var abstract_domain').
370 Local Notation value var := (@value base.type ident var abstract_domain').
371 Notation expr := (@expr base.type ident).
372 Notation Expr := (@expr.Expr base.type ident).
373 Local Notation type := (type base.type).
374 Let type_base (x : base.type.base) : base.type := base.type.type_base x.
375 Let base {bt} (x : Language.Compilers.base.type bt) : type.type _ := type.base x.
376 Local Coercion base : base.type >-> type.type.
377 Local Coercion type_base : base.type.base >-> base.type.
378 Local Notation tZ := (base.type.type_base base.type.Z).
379
380 Section with_relax.
381 Context (relax_zrange : zrange -> option zrange).
382
383 Let always_relax_zrange : zrange -> zrange
384 := fun range => match relax_zrange (ZRange.normalize range) with
385 | Some r => r
386 | None => range
387 end.
388
389 Definition annotation_of_state (st : abstract_domain' base.type.Z) : option zrange
390 := option_map always_relax_zrange st.
391
392 Local Notation cstZ r
393 := (expr.App
394 (d:=type.arrow (type.base tZ) (type.base tZ))
395 (expr.Ident ident.Z_cast)
396 (expr.Ident (@ident.Literal base.type.zrange r%zrange))).
397 Local Notation cstZZ r1 r2
398 := (expr.App
399 (d:=type.arrow (type.base (tZ * tZ)) (type.base (tZ * tZ)))
400 (expr.Ident ident.Z_cast2)
401 (#(@ident.Literal base.type.zrange r1%zrange), #(@ident.Literal base.type.zrange r2%zrange))%expr_pat).
402
403 Definition annotate_expr {var} t : abstract_domain' t -> option (@expr var (t -> t))
404 := match t return abstract_domain' t -> option (expr (t -> t)) with
405 | tZ
406 => fun st => st' <- annotation_of_state st; Some (cstZ st')
407 | tZ * tZ
408 => fun '(sta, stb) => sta' <- annotation_of_state sta; stb' <- annotation_of_state stb; Some (cstZZ sta' stb')
409 | _ => fun _ => None
410 end%option%etype.
411
412 Definition abstract_interp_ident {opts : AbstractInterpretation.Options} (assume_cast_truncates : bool) t (idc : ident t) : type.interp abstract_domain' t
413 := ZRange.ident.option.interp assume_cast_truncates idc.
414
415 Definition always_strip_annotation {opts : AbstractInterpretation.Options} (assume_cast_truncates : bool) {var} t (idc : ident t) : option (value var t)
416 := match idc in ident t return option (value var t) with
417 | ident.Z_cast as idc
418 | ident.Z_cast2 as idc
419 => let tZ_type := (fun t (idc : ident (type.base _ -> type.base t -> type.base t)) => t) _ idc in (* we want Coq to pick up the Z type for bounds tightness, not the zrange type (where "tighter" means "equal") *)
420 Some
421 (fun '(r_orig, re)
422 => Base
423 (fun '(r_known, e)
424 => Base
425 ((abstract_interp_ident assume_cast_truncates _ idc r_orig r_known,
426 let do_strip
427 := match ZRange.type.base.option.lift_Some r_known, ZRange.type.base.option.lift_Some r_orig with
428 | Some r_known, Some r_orig
429 => ZRange.type.base.is_tighter_than
430 (t:=tZ_type)
431 r_known r_orig
432 | _, _ => false
433 end in
434 if do_strip
435 then e
436 else (###idc @ re @ e)%expr))))
437 | _ => None
438 end.
439
440 Definition strip_annotation {opts : AbstractInterpretation.Options} (assume_cast_truncates : bool) (strip_annotations : bool) {var} : forall t, ident t -> option (value var t)
441 := if strip_annotations
442 then always_strip_annotation assume_cast_truncates
443 else fun _ _ => None.
444
445 Definition is_annotated_for {var} t t' (idc : @expr var t) : abstract_domain' t' -> bool
446 := match invert_Z_cast idc, invert_Z_cast2 idc, t' with
447 | Some r, _, tZ
448 => fun r'
449 => option_beq zrange_beq (Some r) (annotation_of_state r')
450 | _, Some (r1, r2), base.type.prod (base.type.type_base base.type.Z) (base.type.type_base base.type.Z)
451 => fun '(r1', r2')
452 => (option_beq zrange_beq (Some r1) (annotation_of_state r1'))
453 && (option_beq zrange_beq (Some r2) (annotation_of_state r2'))
454 | _, _, _ => fun _ => false
455 end.
456
457 Definition annotation_to_cast_helper {var1} {s'sd} (idc : ident s'sd) : option (@expr var1 (type.domain (type.codomain s'sd)) -> @expr var1 (type.codomain (type.codomain s'sd)))
458 := match idc with
459 | ident.Z_cast => Some (fun x => x)
460 | ident.Z_cast2 => Some (fun x => x)
461 | _ => None
462 end.
463
464 Definition annotation_to_cast {var1} {s d} (e : @expr var1 (s -> d)) : option (@expr var1 s -> @expr var1 d)
465 := match invert_AppIdent e with
466 | Some (existT s' (idc, _)) => annotation_to_cast_helper idc
467 | None => None
468 end.
469
470 Definition annotation_to_expr {var1} t (idc : @expr var1 t) : option (forall var2, @expr var2 t)
471 := match invert_Z_cast idc, invert_Z_cast2 idc, t with
472 | Some r, _, (type.base tZ -> type.base tZ)%etype
473 => Some (fun var2 => cstZ r)
474 | _, Some (r1, r2), (type.base (tZ * tZ) -> type.base (tZ * tZ))%etype
475 => Some (fun var2 => cstZZ r1 r2)
476 | _, _, _ => None
477 end.
478 Definition bottom' T : abstract_domain' T
479 := ZRange.type.base.option.None.
480 Definition extract_list_state A (st : abstract_domain' (base.type.list A)) : option (list (abstract_domain' A))
481 := st.
482 Definition extract_option_state A (st : abstract_domain' (base.type.option A)) : option (option (abstract_domain' A))
483 := st.
484
485 Definition strip_all_annotations strip_annotations_under {var} {t} (e : @expr var t) : @expr var t
486 := @ident.strip_all_annotations var (@annotation_to_cast _) strip_annotations_under t e.
487
488 Definition eval_with_bound {opts : AbstractInterpretation.Options} (assume_cast_truncates : bool) (skip_annotations_under : forall t, ident t -> bool) (strip_preexisting_annotations : bool) {var} {t} (e : @expr _ t) (bound : type.for_each_lhs_of_arrow abstract_domain t) : expr t
489 := let annotate_with_state := true in
490 (@partial.ident.eval_with_bound)
491 var abstract_domain' annotate_expr bottom' (abstract_interp_ident assume_cast_truncates) extract_list_state extract_option_state is_annotated_for (strip_annotation assume_cast_truncates strip_preexisting_annotations) skip_annotations_under annotate_with_state t e bound.
492
493 Definition eta_expand_with_bound {var} {t} (e : @expr _ t) (bound : type.for_each_lhs_of_arrow abstract_domain t) : expr t
494 := let assume_cast_truncates := false in
495 let shiftr_avoid_uint1 : shiftr_avoid_uint1_opt := false (* this doesn't actually matter for [eta_expand_with_bound], which only invokes [abstract_interp_ident] on things like [fst], [snd], etc *) in
496 (@partial.ident.eta_expand_with_bound)
497 var abstract_domain' annotate_expr bottom' (abstract_interp_ident assume_cast_truncates) extract_list_state extract_option_state is_annotated_for t e bound.
498
499 Definition StripAllAnnotations strip_annotations_under {t} (e : Expr t) : Expr t
500 := fun var => strip_all_annotations strip_annotations_under (e _).
501
502 Definition EvalWithBound {opts : AbstractInterpretation.Options} (assume_cast_truncates : bool) (skip_annotations_under : forall t, ident t -> bool) (strip_preexisting_annotations : bool) {t} (e : Expr t) (bound : type.for_each_lhs_of_arrow abstract_domain t) : Expr t
503 := fun var => eval_with_bound assume_cast_truncates skip_annotations_under strip_preexisting_annotations (e _) bound.
504 Definition EtaExpandWithBound {t} (e : Expr t) (bound : type.for_each_lhs_of_arrow abstract_domain t) : Expr t
505 := fun var => eta_expand_with_bound (e _) bound.
506 End with_relax.
507
508 Definition strip_annotations {opts : AbstractInterpretation.Options} (assume_cast_truncates : bool) {var} {t} (e : @expr _ t) (bound : type.for_each_lhs_of_arrow abstract_domain t) : expr t
509 := let strip_preexisting_annotations := true in
510 let annotate_with_state := false in
511 let skip_annotations_under := fun _ _ => false in
512 (@partial.ident.eval_with_bound)
513 var abstract_domain' (annotate_expr default_relax_zrange) bottom' (abstract_interp_ident assume_cast_truncates) extract_list_state extract_option_state (is_annotated_for default_relax_zrange) (strip_annotation assume_cast_truncates strip_preexisting_annotations) skip_annotations_under annotate_with_state t e bound.
514
515 Definition StripAnnotations {opts : AbstractInterpretation.Options} (assume_cast_truncates : bool) {t} (e : Expr t) (bound : type.for_each_lhs_of_arrow abstract_domain t) : Expr t
516 := fun var => strip_annotations assume_cast_truncates (e _) bound.
517
518 Definition eval {opts : AbstractInterpretation.Options} {var} {t} (e : @expr _ t) : expr t
519 := let assume_cast_truncates := false in
520 let strip_preexisting_annotations := false in
521 (@partial.ident.eval)
522 var abstract_domain' (annotate_expr default_relax_zrange) bottom' (abstract_interp_ident assume_cast_truncates) extract_list_state extract_option_state (is_annotated_for default_relax_zrange) (strip_annotation assume_cast_truncates strip_preexisting_annotations) t e.
523 Definition Eval {opts : AbstractInterpretation.Options} {t} (e : Expr t) : Expr t
524 := fun var => eval (e _).
525 Definition EtaExpandWithListInfoFromBound {t} (e : Expr t) (bound : type.for_each_lhs_of_arrow abstract_domain t) : Expr t
526 := EtaExpandWithBound default_relax_zrange e (type.map_for_each_lhs_of_arrow (@ZRange.type.option.strip_ranges) bound).
527 Definition extract {opts : AbstractInterpretation.Options} (assume_cast_truncates : bool) {t} (e : expr t) (bound : type.for_each_lhs_of_arrow abstract_domain t) : abstract_domain' (type.final_codomain t)
528 := @partial.ident.extract abstract_domain' bottom' (abstract_interp_ident assume_cast_truncates) t e bound.
529 Definition Extract {opts : AbstractInterpretation.Options} (assume_cast_truncates : bool) {t} (e : Expr t) (bound : type.for_each_lhs_of_arrow abstract_domain t) : abstract_domain' (type.final_codomain t)
530 := @partial.ident.extract abstract_domain' bottom' (abstract_interp_ident assume_cast_truncates) t (e _) bound.
531 End specialized.
532 End partial.
533 Import API.
534
535 Module Import CheckCasts.
536 Fixpoint get_casts {t} (e : expr t) : list { t : _ & forall var, @expr var t }
537 := match partial.annotation_to_expr _ e with
538 | Some e => [existT _ t e]
539 | None
540 => match e with
541 | expr.Ident t idc => nil
542 | expr.Var t v => v
543 | expr.Abs s d f => @get_casts _ (f nil)
544 | expr.App s d f x => @get_casts _ f ++ @get_casts _ x
545 | expr.LetIn A B x f => @get_casts _ x ++ @get_casts _ (f nil)
546 end
547 end%list.
548
549 Definition GetUnsupportedCasts {t} (e : Expr t) : list { t : _ & forall var, @expr var t }
550 := get_casts (e _).
551 End CheckCasts.
552
553 Definition PartialEvaluateWithBounds
554 {shiftr_avoid_uint1 : shiftr_avoid_uint1_opt}
555 (relax_zrange : zrange -> option zrange)
556 (assume_cast_truncates : bool)
557 (skip_annotations_under : forall t, ident t -> bool)
558 (strip_preexisting_annotations : bool)
559 {t} (e : Expr t)
560 (bound : type.for_each_lhs_of_arrow ZRange.type.option.interp t)
561 : Expr t
562 := partial.EvalWithBound relax_zrange assume_cast_truncates skip_annotations_under strip_preexisting_annotations (GeneralizeVar.GeneralizeVar (e _)) bound.
563 Definition PartialEvaluateWithListInfoFromBounds
564 {shiftr_avoid_uint1 : shiftr_avoid_uint1_opt}
565 {t} (e : Expr t)
566 (bound : type.for_each_lhs_of_arrow ZRange.type.option.interp t)
567 : Expr t
568 := partial.EtaExpandWithListInfoFromBound (GeneralizeVar.GeneralizeVar (e _)) bound.
569
570 Definition CheckedPartialEvaluateWithBounds
571 {shiftr_avoid_uint1 : shiftr_avoid_uint1_opt}
572 (relax_zrange : zrange -> option zrange)
573 (assume_cast_truncates : bool)
574 (skip_annotations_under : forall t, ident t -> bool)
575 (strip_preexisting_annotations : bool)
576 {t} (E : Expr t)
577 (b_in : type.for_each_lhs_of_arrow ZRange.type.option.interp t)
578 (b_out : ZRange.type.base.option.interp (type.final_codomain t))
579 : Expr t + (ZRange.type.base.option.interp (type.final_codomain t) * Expr t + list { t : _ & forall var, @expr var t })
580 := dlet_nd e := GeneralizeVar.ToFlat E in
581 let E := GeneralizeVar.FromFlat e in
582 let b_computed := partial.Extract assume_cast_truncates E b_in in
583 let unsupported_casts := if strip_preexisting_annotations
584 then nil
585 else CheckCasts.GetUnsupportedCasts E in
586 match unsupported_casts with
587 | nil => (let E := PartialEvaluateWithBounds relax_zrange assume_cast_truncates skip_annotations_under strip_preexisting_annotations E b_in in
588 if ZRange.type.base.option.is_tighter_than b_computed b_out
589 then @inl (Expr t) _ E
590 else inr (@inl (ZRange.type.base.option.interp (type.final_codomain t) * Expr t) _ (b_computed, E)))
591 | unsupported_casts => inr (inr unsupported_casts)
592 end.
593 End Compilers.