| 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 |
Require Import Crypto.Util.ZRange. |
| 7 |
Require Import Crypto.Util.ZRange.Operations. |
| 8 |
Require Import Crypto.Util.ZRange.BasicLemmas. |
| 9 |
Require Import Crypto.Util.ZRange.SplitBounds. |
| 10 |
Require Import Crypto.Util.Sum. |
| 11 |
Require Import Crypto.Util.LetIn. |
| 12 |
Require Import Crypto.Util.Prod. |
| 13 |
Require Import Crypto.Util.Sigma. |
| 14 |
Require Import Crypto.Util.Option. |
| 15 |
Require Import Crypto.Util.ListUtil. |
| 16 |
Require Import Crypto.Util.NatUtil. |
| 17 |
Require Import Crypto.Util.ZUtil.AddGetCarry. |
| 18 |
Require Import Crypto.Util.ZUtil.AddModulo. |
| 19 |
Require Import Crypto.Util.ZUtil.CC. |
| 20 |
Require Import Crypto.Util.ZUtil.MulSplit. |
| 21 |
Require Import Crypto.Util.ZUtil.Rshi. |
| 22 |
Require Import Crypto.Util.ZUtil.Zselect. |
| 23 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 24 |
Require Import Crypto.Util.ZUtil.Tactics.SplitMinMax. |
| 25 |
Require Import Crypto.Util.ZUtil.Tactics.ReplaceNegWithPos. |
| 26 |
Require Import Crypto.Util.HProp. |
| 27 |
Require Import Crypto.Util.PER. |
| 28 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 29 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 30 |
Require Import Crypto.Util.Tactics.SplitInContext. |
| 31 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 32 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 33 |
Require Import Crypto.Util.Tactics.SpecializeAllWays. |
| 34 |
Require Import Crypto.Util.Tactics.Head. |
| 35 |
Require Import Crypto.Util.Tactics.DoWithHyp. |
| 36 |
Require Import Rewriter.Language.Language. |
| 37 |
Require Import Rewriter.Language.Inversion. |
| 38 |
Require Import Rewriter.Language.Wf. |
| 39 |
Require Import Crypto.Language.API. |
| 40 |
Require Import Crypto.CastLemmas. |
| 41 |
Require Import Rewriter.Language.UnderLetsProofs. |
| 42 |
Require Import Crypto.AbstractInterpretation.AbstractInterpretation. |
| 43 |
Require Import Crypto.AbstractInterpretation.Wf. |
| 44 |
Require Import Crypto.AbstractInterpretation.ZRangeProofs. |
| 45 |
|
| 46 |
Module Compilers. |
| 47 |
Import Language.Compilers. |
| 48 |
Import UnderLets.Compilers. |
| 49 |
Import AbstractInterpretation.Compilers. |
| 50 |
Import Language.Inversion.Compilers. |
| 51 |
Import Language.Wf.Compilers. |
| 52 |
Import Language.API.Compilers. |
| 53 |
Import UnderLetsProofs.Compilers. |
| 54 |
Import AbstractInterpretation.Wf.Compilers. |
| 55 |
Import AbstractInterpretation.ZRangeProofs.Compilers. |
| 56 |
Import AbstractInterpretation.Wf.Compilers.partial. |
| 57 |
Import invert_expr. |
| 58 |
|
| 59 |
Local Notation related_bounded' b v1 v2 |
| 60 |
:= (ZRange.type.base.option.is_bounded_by b v1 = true |
| 61 |
/\ ZRange.type.base.option.is_bounded_by b v2 = true |
| 62 |
/\ v1 = v2) (only parsing). |
| 63 |
Local Notation related_bounded |
| 64 |
:= (@type.related_hetero3 _ _ _ _ (fun t b v1 v2 => related_bounded' b v1 v2)). |
| 65 |
|
| 66 |
Module ZRange. |
| 67 |
Module type. |
| 68 |
Global Instance is_bounded_by_Proper_related_eq {t} |
| 69 |
: Proper (eq ==> type.eqv ==> eq) (@ZRange.type.is_bounded_by t). |
| 70 |
Proof. |
| 71 |
cbv [Proper respectful]; destruct t; [ cbn | reflexivity ]; |
| 72 |
intros; subst; reflexivity. |
| 73 |
Qed. |
| 74 |
|
| 75 |
Module option. |
| 76 |
Global Instance is_bounded_by_Proper_related_eq {t} |
| 77 |
: Proper (eq ==> type.eqv ==> eq) (@ZRange.type.option.is_bounded_by t). |
| 78 |
Proof. |
| 79 |
cbv [Proper respectful]; destruct t; [ cbn | reflexivity ]; |
| 80 |
intros; subst; reflexivity. |
| 81 |
Qed. |
| 82 |
End option. |
| 83 |
End type. |
| 84 |
End ZRange. |
| 85 |
|
| 86 |
Module Import partial. |
| 87 |
Import AbstractInterpretation.Compilers.partial. |
| 88 |
Import UnderLets.Compilers.UnderLets. |
| 89 |
Section with_type. |
| 90 |
Context {base_type : Type}. |
| 91 |
Local Notation type := (type base_type). |
| 92 |
Let type_base (x : base_type) : type := type.base x. |
| 93 |
Local Coercion type_base : base_type >-> type. |
| 94 |
Context {ident : type -> Type}. |
| 95 |
Local Notation Expr := (@expr.Expr base_type ident). |
| 96 |
Context (abstract_domain' base_interp : base_type -> Type) |
| 97 |
(ident_interp : forall t, ident t -> type.interp base_interp t) |
| 98 |
(abstraction_relation' : forall t, abstract_domain' t -> base_interp t -> Prop) |
| 99 |
(bottom' : forall A, abstract_domain' A) |
| 100 |
(bottom'_related : forall t v, abstraction_relation' t (bottom' t) v) |
| 101 |
(skip_annotations_under : forall t, ident t -> bool) |
| 102 |
(abstract_interp_ident : forall t, ident t -> type.interp abstract_domain' t) |
| 103 |
(ident_interp_Proper : forall t (idc : ident t), type.related_hetero abstraction_relation' (abstract_interp_ident t idc) (ident_interp t idc)) |
| 104 |
(ident_interp_Proper' : forall t, Proper (eq ==> type.eqv) (ident_interp t)) |
| 105 |
(abstract_domain'_R : forall t, abstract_domain' t -> abstract_domain' t -> Prop) |
| 106 |
(abstraction_relation'_Proper : forall t, Proper (abstract_domain'_R t ==> eq ==> Basics.impl) (abstraction_relation' t)) |
| 107 |
{abstract_domain'_R_transitive : forall t, Transitive (@abstract_domain'_R t)} |
| 108 |
{abstract_domain'_R_symmetric : forall t, Symmetric (@abstract_domain'_R t)} |
| 109 |
{bottom'_Proper : forall t, Proper (abstract_domain'_R t) (bottom' t)} |
| 110 |
(abstract_domain'_R_of_related : forall t st v, @abstraction_relation' t st v -> @abstract_domain'_R t st st). |
| 111 |
Local Notation abstract_domain := (@abstract_domain base_type abstract_domain'). |
| 112 |
Definition abstraction_relation {t} : abstract_domain t -> type.interp base_interp t -> Prop |
| 113 |
:= type.related_hetero (@abstraction_relation'). |
| 114 |
Local Notation bottom := (@bottom base_type abstract_domain' (@bottom')). |
| 115 |
Local Notation bottom_for_each_lhs_of_arrow := (@bottom_for_each_lhs_of_arrow base_type abstract_domain' (@bottom')). |
| 116 |
Local Notation abstract_domain_R := (@abstract_domain_R base_type abstract_domain' abstract_domain'_R). |
| 117 |
Local Notation var := (type.interp base_interp). |
| 118 |
Local Notation expr := (@expr.expr base_type ident). |
| 119 |
Local Notation UnderLets := (@UnderLets base_type ident). |
| 120 |
Local Notation value := (@value base_type ident var abstract_domain'). |
| 121 |
Local Notation value_with_lets := (@value_with_lets base_type ident var abstract_domain'). |
| 122 |
Local Notation state_of_value := (@state_of_value base_type ident var abstract_domain' bottom'). |
| 123 |
Context (annotate : forall (is_let_bound : bool) t, abstract_domain' t -> @expr var t -> @UnderLets var (@expr var t)) |
| 124 |
(interp_ident : bool -> forall t, ident t -> value_with_lets t) |
| 125 |
(ident_extract : forall t, ident t -> abstract_domain t) |
| 126 |
(interp_annotate |
| 127 |
: forall is_let_bound t st e |
| 128 |
(He : abstraction_relation' t st (expr.interp (t:=type.base t) (@ident_interp) e)), |
| 129 |
expr.interp (@ident_interp) (UnderLets.interp (@ident_interp) (@annotate is_let_bound t st e)) |
| 130 |
= expr.interp (@ident_interp) e) |
| 131 |
(ident_extract_Proper : forall t, Proper (eq ==> abstract_domain_R) (ident_extract t)). |
| 132 |
Local Notation eta_expand_with_bound' := (@eta_expand_with_bound' base_type ident _ abstract_domain' annotate bottom'). |
| 133 |
Local Notation eval_with_bound' := (@partial.eval_with_bound' base_type ident _ abstract_domain' annotate bottom' skip_annotations_under interp_ident). |
| 134 |
Local Notation extract' := (@extract' base_type ident abstract_domain' bottom' ident_extract). |
| 135 |
Local Notation extract_gen := (@extract_gen base_type ident abstract_domain' bottom' ident_extract). |
| 136 |
Local Notation reify := (@reify base_type ident _ abstract_domain' annotate bottom'). |
| 137 |
Local Notation reflect := (@reflect base_type ident _ abstract_domain' annotate bottom'). |
| 138 |
Local Notation interp := (@interp base_type ident var abstract_domain' annotate bottom' skip_annotations_under interp_ident). |
| 139 |
Local Notation bottomify := (@bottomify base_type ident _ abstract_domain' bottom'). |
| 140 |
|
| 141 |
Lemma bottom_related t v : @abstraction_relation t bottom v. |
| 142 |
Proof using bottom'_related. cbv [abstraction_relation]; induction t; cbn; cbv [respectful_hetero]; eauto. Qed. |
| 143 |
|
| 144 |
Local Hint Resolve bottom_related : core typeclass_instances. |
| 145 |
|
| 146 |
Lemma bottom_for_each_lhs_of_arrow_related t v : type.and_for_each_lhs_of_arrow (@abstraction_relation) (@bottom_for_each_lhs_of_arrow t) v. |
| 147 |
Proof using bottom'_related. induction t; cbn; eauto using bottom_related. Qed. |
| 148 |
|
| 149 |
Local Notation bottom_Proper := (@bottom_Proper base_type abstract_domain' bottom' abstract_domain'_R bottom'_Proper). |
| 150 |
Local Notation bottom_for_each_lhs_of_arrow_Proper := (@bottom_for_each_lhs_of_arrow_Proper base_type abstract_domain' bottom' abstract_domain'_R bottom'_Proper). |
| 151 |
|
| 152 |
Local Hint Extern 0 (Proper _) => simple apply (@bottom_Proper) : typeclass_instances. |
| 153 |
Local Hint Extern 0 (Proper _) => simple apply (@bottom_for_each_lhs_of_arrow_Proper) : typeclass_instances. |
| 154 |
Local Hint Extern 0 => simple apply (@bottom_Proper) : core. |
| 155 |
Local Hint Extern 0 => simple apply (@bottom_for_each_lhs_of_arrow_Proper) : core. |
| 156 |
|
| 157 |
Fixpoint related_bounded_value {t} : abstract_domain t -> value t -> type.interp base_interp t -> Prop |
| 158 |
:= match t return abstract_domain t -> value t -> type.interp base_interp t -> Prop with |
| 159 |
| type.base t |
| 160 |
=> fun st '(e_st, e) v |
| 161 |
=> abstract_domain'_R t st e_st |
| 162 |
/\ expr.interp ident_interp e = v |
| 163 |
/\ abstraction_relation' t st v |
| 164 |
| type.arrow s d |
| 165 |
=> fun st e v |
| 166 |
=> Proper type.eqv v |
| 167 |
/\ forall st_s e_s v_s, |
| 168 |
let st_s := match s with |
| 169 |
| type.base _ => st_s |
| 170 |
| type.arrow _ _ => bottom |
| 171 |
end in |
| 172 |
@related_bounded_value s st_s e_s v_s |
| 173 |
-> @related_bounded_value d (st st_s) (UnderLets.interp ident_interp (e e_s)) (v v_s) |
| 174 |
end. |
| 175 |
Definition related_bounded_value_with_lets {t} : abstract_domain t -> value_with_lets t -> type.interp base_interp t -> Prop |
| 176 |
:= fun st e v => related_bounded_value st (UnderLets.interp ident_interp e) v. |
| 177 |
|
| 178 |
Definition related_of_related_bounded_value {t} st e v |
| 179 |
: @related_bounded_value t st e v -> v == v. |
| 180 |
Proof using Type. destruct t; [ reflexivity | intros [? ?]; assumption ]. Qed. |
| 181 |
|
| 182 |
Lemma abstract_domain'_R_refl_of_rel_l t x y (H : @abstract_domain'_R t x y) |
| 183 |
: @abstract_domain'_R t x x. |
| 184 |
Proof using abstract_domain'_R_symmetric abstract_domain'_R_transitive. eapply PER_valid_l; eassumption. Qed. |
| 185 |
|
| 186 |
Lemma abstract_domain'_R_refl_of_rel_r t x y (H : @abstract_domain'_R t x y) |
| 187 |
: @abstract_domain'_R t y y. |
| 188 |
Proof using abstract_domain'_R_symmetric abstract_domain'_R_transitive. eapply PER_valid_r; eassumption. Qed. |
| 189 |
|
| 190 |
Local Hint Immediate abstract_domain'_R_refl_of_rel_l abstract_domain'_R_refl_of_rel_r : core. |
| 191 |
|
| 192 |
Local Instance abstract_domain_R_Symmetric {t} : Symmetric (@abstract_domain_R t) := _ : Symmetric (type.related _). |
| 193 |
Local Instance abstract_domain_R_Transitive {t} : Transitive (@abstract_domain_R t) := _ : Transitive (type.related _). |
| 194 |
|
| 195 |
Lemma abstract_domain_R_refl_of_rel_l t x y (H : @abstract_domain_R t x y) |
| 196 |
: @abstract_domain_R t x x. |
| 197 |
Proof using abstract_domain'_R_symmetric abstract_domain'_R_transitive. eapply PER_valid_l; eassumption. Qed. |
| 198 |
|
| 199 |
Lemma abstract_domain_R_refl_of_rel_r t x y (H : @abstract_domain_R t x y) |
| 200 |
: @abstract_domain_R t y y. |
| 201 |
Proof using abstract_domain'_R_symmetric abstract_domain'_R_transitive. eapply PER_valid_r; eassumption. Qed. |
| 202 |
|
| 203 |
Local Hint Immediate abstract_domain_R_refl_of_rel_l abstract_domain_R_refl_of_rel_r : core. |
| 204 |
|
| 205 |
Lemma related_bottom_for_each_lhs_of_arrow {t} v |
| 206 |
: type.and_for_each_lhs_of_arrow (@abstraction_relation) (@bottom_for_each_lhs_of_arrow t) v. |
| 207 |
Proof using bottom'_related. induction t; cbn; eauto. Qed. |
| 208 |
|
| 209 |
Local Hint Immediate related_bottom_for_each_lhs_of_arrow : core. |
| 210 |
|
| 211 |
Fixpoint fill_in_bottom_for_arrows {t} : abstract_domain t -> abstract_domain t |
| 212 |
:= match t with |
| 213 |
| type.base t => fun x => x |
| 214 |
| type.arrow s d |
| 215 |
=> fun f x => let x := match s with |
| 216 |
| type.base _ => x |
| 217 |
| type.arrow _ _ => bottom |
| 218 |
end in |
| 219 |
@fill_in_bottom_for_arrows d (f x) |
| 220 |
end. |
| 221 |
|
| 222 |
Lemma abstract_domain_R_bottom_fill_arrows {t} |
| 223 |
: abstract_domain_R (@bottom t) (fill_in_bottom_for_arrows (@bottom t)). |
| 224 |
Proof using bottom'_Proper. |
| 225 |
cbv [abstract_domain_R]; induction t as [t|s IHs d IHd]; cbn [fill_in_bottom_for_arrows bottom type.related]; |
| 226 |
cbv [respectful Proper] in *; auto. |
| 227 |
Qed. |
| 228 |
|
| 229 |
Lemma fill_in_bottom_for_arrows_bottom_related {t} v |
| 230 |
: abstraction_relation (fill_in_bottom_for_arrows (@bottom t)) v. |
| 231 |
Proof using bottom'_related. |
| 232 |
cbv [abstraction_relation]; induction t; cbn; cbv [respectful_hetero]; eauto. |
| 233 |
Qed. |
| 234 |
|
| 235 |
Hint Resolve fill_in_bottom_for_arrows_bottom_related : core. |
| 236 |
|
| 237 |
Local Instance fill_in_bottom_for_arrows_Proper {t} : Proper (abstract_domain_R ==> abstract_domain_R) (@fill_in_bottom_for_arrows t). |
| 238 |
Proof using bottom'_Proper. |
| 239 |
pose proof (@bottom_Proper). |
| 240 |
cbv [Proper respectful abstract_domain_R] in *; induction t; cbn in *; cbv [respectful] in *; |
| 241 |
intros; break_innermost_match; eauto. |
| 242 |
Qed. |
| 243 |
|
| 244 |
Local Instance bottom_eqv_Proper_refl {t} : Proper type.eqv (@bottom t). |
| 245 |
Proof using Type. cbv [Proper]; induction t; cbn in *; cbv [respectful] in *; eauto. Qed. |
| 246 |
|
| 247 |
Lemma bottom_eqv_refl {t} : @bottom t == @bottom t. |
| 248 |
Proof using Type. apply bottom_eqv_Proper_refl. Qed. |
| 249 |
Local Hint Resolve bottom_eqv_refl : core. |
| 250 |
|
| 251 |
Local Instance fill_in_bottom_for_arrows_Proper_eqv {t} : Proper (type.eqv ==> type.eqv) (@fill_in_bottom_for_arrows t). |
| 252 |
Proof using Type. |
| 253 |
cbv [Proper respectful] in *; induction t; cbn in *; cbv [respectful] in *; |
| 254 |
intros; break_innermost_match; cbn in *; cbv [respectful] in *; eauto. |
| 255 |
Qed. |
| 256 |
|
| 257 |
Lemma state_of_value_related_fill {t} v (HP : Proper abstract_domain_R (@state_of_value t v)) |
| 258 |
: abstract_domain_R (@state_of_value t v) (fill_in_bottom_for_arrows (@state_of_value t v)). |
| 259 |
Proof using bottom'_Proper. destruct t; [ assumption | apply abstract_domain_R_bottom_fill_arrows ]. Qed. |
| 260 |
|
| 261 |
Lemma eqv_bottom_fill_bottom {t} |
| 262 |
: @bottom t == fill_in_bottom_for_arrows bottom. |
| 263 |
Proof using Type. induction t; cbn; [ reflexivity | ]; cbv [respectful]; auto. Qed. |
| 264 |
|
| 265 |
Lemma eqv_fill_bottom_idempotent {t} v1 v2 |
| 266 |
: v1 == v2 -> fill_in_bottom_for_arrows (fill_in_bottom_for_arrows v1) == @fill_in_bottom_for_arrows t v2. |
| 267 |
Proof using Type. induction t; cbn; cbv [respectful]; break_innermost_match; auto. Qed. |
| 268 |
|
| 269 |
Lemma abstract_domain_R_fill_bottom_idempotent {t} v1 v2 |
| 270 |
: abstract_domain_R v1 v2 |
| 271 |
-> abstract_domain_R (fill_in_bottom_for_arrows (fill_in_bottom_for_arrows v1)) |
| 272 |
(@fill_in_bottom_for_arrows t v2). |
| 273 |
Proof using bottom'_Proper. |
| 274 |
pose proof (@bottom_Proper) as Hb. |
| 275 |
induction t as [|s IHs d IHd]; cbn; cbv [respectful Proper abstract_domain_R] in *; break_innermost_match; auto. |
| 276 |
Qed. |
| 277 |
|
| 278 |
Lemma app_curried_state_of_value_fill {t} v x y |
| 279 |
(H : type.and_for_each_lhs_of_arrow (@type.eqv) x y) |
| 280 |
: type.app_curried (@state_of_value t v) x = type.app_curried (fill_in_bottom_for_arrows (@state_of_value t v)) y. |
| 281 |
Proof using Type. |
| 282 |
destruct t; [ reflexivity | cbv [state_of_value] ]. |
| 283 |
apply type.app_curried_Proper; [ apply eqv_bottom_fill_bottom | assumption ]. |
| 284 |
Qed. |
| 285 |
|
| 286 |
Lemma first_order_app_curried_fill_in_bottom_for_arrows_eq {t} f xs |
| 287 |
(Ht : type.is_not_higher_order t = true) |
| 288 |
: type.app_curried (t:=t) f xs = type.app_curried (fill_in_bottom_for_arrows f) xs. |
| 289 |
Proof using Type. |
| 290 |
clear -Ht. |
| 291 |
induction t as [| [|s' d'] IHs d IHd]; cbn in *; try discriminate; auto. |
| 292 |
Qed. |
| 293 |
|
| 294 |
Lemma first_order_abstraction_relation_fill_in_bottom_for_arrows_iff |
| 295 |
{t} f v |
| 296 |
(Ht : type.is_not_higher_order t = true) |
| 297 |
: @abstraction_relation t f v |
| 298 |
<-> @abstraction_relation t (fill_in_bottom_for_arrows f) v. |
| 299 |
Proof using Type. |
| 300 |
clear -Ht; cbv [abstraction_relation]. |
| 301 |
split; induction t as [| [|s' d'] IHs d IHd]; |
| 302 |
cbn in *; cbv [respectful_hetero]; try discriminate; auto. |
| 303 |
Qed. |
| 304 |
|
| 305 |
Lemma related_state_of_value_of_related_bounded_value {t} st e v |
| 306 |
: @related_bounded_value t st e v -> abstract_domain_R match t with |
| 307 |
| type.base _ => st |
| 308 |
| type.arrow _ _ => bottom |
| 309 |
end (state_of_value e). |
| 310 |
Proof using bottom'_Proper. intro H; destruct t; cbn in *; [ destruct e; apply H | repeat intro; refine bottom_Proper ]. Qed. |
| 311 |
|
| 312 |
Lemma related_state_of_value_of_related_bounded_value2 {t} st e v (st' := match t with |
| 313 |
| type.base _ => st |
| 314 |
| type.arrow _ _ => bottom |
| 315 |
end) |
| 316 |
: @related_bounded_value t st' e v -> abstract_domain_R st' (state_of_value e). |
| 317 |
Proof using bottom'_Proper. intro H; destruct t; cbn in *; [ destruct e; apply H | repeat intro; refine bottom_Proper ]. Qed. |
| 318 |
|
| 319 |
Lemma related_bounded_value_Proper {t} st1 st2 (Hst : abstract_domain_R (fill_in_bottom_for_arrows st1) (fill_in_bottom_for_arrows st2)) |
| 320 |
a a1 a2 |
| 321 |
(Ha' : type.eqv a1 a2) |
| 322 |
: @related_bounded_value t st1 a a1 -> @related_bounded_value t st2 a a2. |
| 323 |
Proof using abstraction_relation'_Proper abstract_domain'_R_transitive abstract_domain'_R_symmetric bottom'_Proper. |
| 324 |
induction t as [t|s IHs d IHd]; cbn [related_bounded_value type.related] in *; cbv [respectful abstract_domain_R] in *. |
| 325 |
all: cbn [type.andb_each_lhs_of_arrow] in *. |
| 326 |
all: rewrite ?Bool.andb_true_iff in *. |
| 327 |
all: destruct_head'_and. |
| 328 |
{ intros; break_innermost_match; subst; |
| 329 |
destruct_head'_and; repeat apply conj; auto. |
| 330 |
{ etransitivity; (idtac + symmetry); eassumption. } |
| 331 |
{ eapply abstraction_relation'_Proper; (eassumption + reflexivity). } } |
| 332 |
{ intros [? Hrel]. |
| 333 |
split; [ repeat intro; etransitivity; (idtac + symmetry); eapply Ha'; (eassumption + (etransitivity; (idtac + symmetry); eassumption)) | ]. |
| 334 |
pose proof (@bottom_Proper s) as Hsbot. |
| 335 |
intros ?? v_s; destruct s; intros Hx; cbn [type.related] in *; |
| 336 |
cbn [fill_in_bottom_for_arrows] in *; cbv [respectful] in *. |
| 337 |
{ specialize_by_assumption; cbn in *. |
| 338 |
eapply IHd; [ cbn in Hst |- *; eapply Hst | apply Ha'; reflexivity | eapply Hrel, Hx ]; cbv [respectful]. |
| 339 |
cbn [related_bounded_value] in *. |
| 340 |
break_innermost_match_hyps; destruct_head'_and. |
| 341 |
eauto. } |
| 342 |
{ eapply IHd; [ eapply Hst | apply Ha' | eapply Hrel, Hx ]; |
| 343 |
[ eexact Hsbot | refine (@related_of_related_bounded_value _ _ _ v_s _); eassumption | refine bottom ]. } } |
| 344 |
Qed. |
| 345 |
|
| 346 |
Lemma related_bounded_value_fill_bottom_iff {t} st1 st2 (Hst : abstract_domain_R st1 st2) |
| 347 |
a a1 a2 |
| 348 |
(Ha' : type.eqv a1 a2) |
| 349 |
: @related_bounded_value t st1 a a1 <-> @related_bounded_value t (fill_in_bottom_for_arrows st2) a a2. |
| 350 |
Proof using abstraction_relation'_Proper abstract_domain'_R_transitive abstract_domain'_R_symmetric bottom'_Proper. |
| 351 |
split; eapply related_bounded_value_Proper; try solve [ (idtac + symmetry); assumption ]. |
| 352 |
all: (idtac + symmetry); apply abstract_domain_R_fill_bottom_idempotent. |
| 353 |
all: (idtac + symmetry); assumption. |
| 354 |
Qed. |
| 355 |
|
| 356 |
Lemma related_bounded_value_Proper1 {t} |
| 357 |
: Proper (abstract_domain_R ==> eq ==> eq ==> Basics.impl) (@related_bounded_value t). |
| 358 |
Proof using abstraction_relation'_Proper abstract_domain'_R_transitive abstract_domain'_R_symmetric bottom'_Proper. |
| 359 |
repeat intro; subst; eapply related_bounded_value_Proper. |
| 360 |
{ eapply fill_in_bottom_for_arrows_Proper; eassumption. } |
| 361 |
{ eapply related_of_related_bounded_value; eassumption. } |
| 362 |
{ assumption. } |
| 363 |
Qed. |
| 364 |
|
| 365 |
Lemma related_bounded_value_Proper_eq {t} |
| 366 |
: Proper (eq ==> eq ==> eq ==> Basics.impl) (@related_bounded_value t). |
| 367 |
Proof using Type. |
| 368 |
repeat intro; subst; assumption. |
| 369 |
Qed. |
| 370 |
|
| 371 |
Lemma related_bounded_value_Proper_interp_eq_base {t} |
| 372 |
: Proper (eq ==> RelProd eq (fun x y => expr.interp ident_interp x = expr.interp ident_interp y) ==> eq ==> Basics.impl) (@related_bounded_value (type.base t)). |
| 373 |
Proof using Type. |
| 374 |
repeat intro; subst. |
| 375 |
cbv [value RelProd relation_conjunction predicate_intersection pointwise_extension RelCompFun] in *. |
| 376 |
destruct_head'_prod; destruct_head'_and; cbn [fst snd] in *; subst. |
| 377 |
cbv [related_bounded_value] in *; destruct_head'_and; repeat apply conj; subst; (idtac + symmetry); assumption. |
| 378 |
Qed. |
| 379 |
|
| 380 |
Fixpoint interp_reify |
| 381 |
annotate_with_state is_let_bound {t} st e v b_in |
| 382 |
(Hb : Proper (type.and_for_each_lhs_of_arrow (@abstract_domain_R)) b_in) |
| 383 |
(H : related_bounded_value st e v) {struct t} |
| 384 |
: (forall arg1 arg2 |
| 385 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 386 |
(Harg1 : type.and_for_each_lhs_of_arrow (@abstraction_relation) b_in arg1), |
| 387 |
type.app_curried (expr.interp ident_interp (UnderLets.interp ident_interp (@reify annotate_with_state is_let_bound t e b_in))) arg1 |
| 388 |
= type.app_curried v arg2) |
| 389 |
/\ (forall arg1 |
| 390 |
(Harg1 : type.and_for_each_lhs_of_arrow (@abstraction_relation) b_in arg1) |
| 391 |
(Harg11 : Proper (type.and_for_each_lhs_of_arrow (@type.eqv)) arg1), |
| 392 |
abstraction_relation' |
| 393 |
_ |
| 394 |
(type.app_curried (fill_in_bottom_for_arrows st) b_in) |
| 395 |
(type.app_curried (expr.interp ident_interp (UnderLets.interp ident_interp (@reify annotate_with_state is_let_bound t e b_in))) arg1)) |
| 396 |
with interp_reflect |
| 397 |
annotate_with_state {t} st e v |
| 398 |
(Hst_Proper : Proper abstract_domain_R st) |
| 399 |
(H_val : expr.interp ident_interp e == v) |
| 400 |
(Hst1 : abstraction_relation (fill_in_bottom_for_arrows st) (expr.interp ident_interp e)) |
| 401 |
{struct t} |
| 402 |
: related_bounded_value |
| 403 |
st |
| 404 |
(@reflect annotate_with_state t e st) |
| 405 |
v. |
| 406 |
Proof using interp_annotate abstraction_relation'_Proper bottom'_related bottom'_Proper abstract_domain'_R_transitive abstract_domain'_R_symmetric. |
| 407 |
all: destruct t as [t|s d]; |
| 408 |
[ clear interp_reify interp_reflect |
| 409 |
| pose proof (fun is_let_bound => interp_reify annotate_with_state is_let_bound s) as interp_reify_s; |
| 410 |
pose proof (fun is_let_bound => interp_reify annotate_with_state is_let_bound d) as interp_reify_d; |
| 411 |
pose proof (interp_reflect annotate_with_state s) as interp_reflect_s; |
| 412 |
pose proof (interp_reflect annotate_with_state d) as interp_reflect_d; |
| 413 |
clear interp_reify interp_reflect; |
| 414 |
pose proof (@abstract_domain_R_bottom_fill_arrows s); |
| 415 |
pose proof (@abstract_domain_R_bottom_fill_arrows d) ]. |
| 416 |
all: cbn [reify reflect] in *; fold (@reify) (@reflect) in *. |
| 417 |
all: cbn [related_bounded_value type.related type.app_curried] in *. |
| 418 |
all: cbn [UnderLets.interp expr.interp type.final_codomain type.andb_each_lhs_of_arrow type.is_base fst snd fill_in_bottom_for_arrows type.map_for_each_lhs_of_arrow type.for_each_lhs_of_arrow type.and_for_each_lhs_of_arrow partial.bottom_for_each_lhs_of_arrow partial.bottom] in *. |
| 419 |
all: destruct annotate_with_state; try destruct is_let_bound. |
| 420 |
all: repeat first [ reflexivity |
| 421 |
| progress eta_expand |
| 422 |
| progress cbv [type.is_not_higher_order] in * |
| 423 |
| progress cbn [UnderLets.interp expr.interp type.final_codomain fst snd] in * |
| 424 |
| progress subst |
| 425 |
| progress destruct_head'_and |
| 426 |
| progress destruct_head'_prod |
| 427 |
| progress destruct_head_hnf' and |
| 428 |
| progress destruct_head_hnf' prod |
| 429 |
| progress destruct_head_hnf' unit |
| 430 |
| progress split_and |
| 431 |
| progress subst |
| 432 |
| discriminate |
| 433 |
| rewrite UnderLets.interp_splice |
| 434 |
| rewrite UnderLets.interp_to_expr |
| 435 |
| rewrite interp_annotate |
| 436 |
| match goal with |
| 437 |
| [ H : context[andb _ _ = true] |- _ ] => rewrite !Bool.andb_true_iff in H |
| 438 |
| [ |- context[andb _ _ = true] ] => rewrite !Bool.andb_true_iff |
| 439 |
end |
| 440 |
| match goal with |
| 441 |
| [ H : fst ?x = _ |- _ ] => is_var x; destruct x |
| 442 |
| [ H : Proper _ ?st |- ?R (?st _) (?st _) ] => apply H |
| 443 |
| [ |- ?R (state_of_value _) (state_of_value _) ] => cbv [state_of_value] in * |
| 444 |
end |
| 445 |
| solve [ repeat intro; apply bottom_Proper |
| 446 |
| auto; cbv [Proper respectful Basics.impl] in *; eauto ] |
| 447 |
| progress (repeat apply conj; intros * ) |
| 448 |
| progress intros |
| 449 |
| progress destruct_head'_or |
| 450 |
| do_with_exactly_one_hyp ltac:(fun H => eapply H; clear H); |
| 451 |
try assumption; auto; [] |
| 452 |
| match goal with |
| 453 |
| [ |- Proper ?R _ ] => (eapply PER_valid_l + eapply PER_valid_r); eassumption |
| 454 |
| [ |- @related_bounded_value ?t ?st1 (reflect _ _ ?st2) _ ] |
| 455 |
=> (tryif first [ constr_eq st1 st2 | has_evar st1 | has_evar st2 ] |
| 456 |
then fail |
| 457 |
else (eapply (@related_bounded_value_Proper1 t st2 st1); |
| 458 |
try reflexivity)) |
| 459 |
| [ H : ?R ?x ?y |- ?R ?y ?x ] => symmetry; assumption |
| 460 |
end |
| 461 |
| break_innermost_match_step |
| 462 |
| do_with_exactly_one_hyp ltac:(fun H => eapply H; clear H); |
| 463 |
try assumption; auto |
| 464 |
| match goal with |
| 465 |
| [ |- abstraction_relation (fill_in_bottom_for_arrows (?f (state_of_value ?e))) _ ] |
| 466 |
=> replace (state_of_value e) with (match s with |
| 467 |
| type.base _ => state_of_value e |
| 468 |
| type.arrow _ _ => bottom |
| 469 |
end) by (destruct s; reflexivity) |
| 470 |
end |
| 471 |
| progress fold (@reify) (@reflect) (@type.interp) (@type.related) (@type.related_hetero) in * |
| 472 |
| match goal with |
| 473 |
| [ |- type.related _ (expr.interp _ (UnderLets.interp _ (reify _ _ _ _))) _ ] |
| 474 |
=> rewrite type.related_iff_app_curried |
| 475 |
| [ |- type.related_hetero _ (@state_of_value ?t _) _ ] |
| 476 |
=> is_var t; destruct t; cbv [state_of_value]; [ cbn | apply bottom_related ] |
| 477 |
end ]. |
| 478 |
Qed. |
| 479 |
|
| 480 |
Lemma interp_reify_first_order |
| 481 |
annotate_with_state is_let_bound {t} st e v b_in |
| 482 |
(Ht : type.is_not_higher_order t = true) |
| 483 |
(Hb : Proper (type.and_for_each_lhs_of_arrow (@abstract_domain_R)) b_in) |
| 484 |
(H : related_bounded_value st e v) |
| 485 |
: (forall arg1 arg2 |
| 486 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 487 |
(Harg1 : type.and_for_each_lhs_of_arrow (@abstraction_relation) b_in arg1), |
| 488 |
type.app_curried (expr.interp ident_interp (UnderLets.interp ident_interp (@reify annotate_with_state is_let_bound t e b_in))) arg1 |
| 489 |
= type.app_curried v arg2) |
| 490 |
/\ (forall arg1 |
| 491 |
(Harg1 : type.and_for_each_lhs_of_arrow (@abstraction_relation) b_in arg1) |
| 492 |
(Harg11 : Proper (type.and_for_each_lhs_of_arrow (@type.eqv)) arg1), |
| 493 |
abstraction_relation' |
| 494 |
_ |
| 495 |
(type.app_curried st b_in) |
| 496 |
(type.app_curried (expr.interp ident_interp (UnderLets.interp ident_interp (@reify annotate_with_state is_let_bound t e b_in))) arg1)). |
| 497 |
Proof using interp_annotate abstraction_relation'_Proper bottom'_related bottom'_Proper abstract_domain'_R_transitive abstract_domain'_R_symmetric. |
| 498 |
rewrite first_order_app_curried_fill_in_bottom_for_arrows_eq by assumption. |
| 499 |
apply interp_reify; assumption. |
| 500 |
Qed. |
| 501 |
|
| 502 |
Lemma interp_reflect_first_order |
| 503 |
annotate_with_state {t} st e v |
| 504 |
(Ht : type.is_not_higher_order t = true) |
| 505 |
(Hst_Proper : Proper abstract_domain_R st) |
| 506 |
(H_val : expr.interp ident_interp e == v) |
| 507 |
(Hst : abstraction_relation st (expr.interp ident_interp e)) |
| 508 |
: related_bounded_value |
| 509 |
st |
| 510 |
(@reflect annotate_with_state t e st) |
| 511 |
v. |
| 512 |
Proof using interp_annotate abstraction_relation'_Proper bottom'_related bottom'_Proper abstract_domain'_R_transitive abstract_domain'_R_symmetric. |
| 513 |
rewrite first_order_abstraction_relation_fill_in_bottom_for_arrows_iff in Hst by assumption. |
| 514 |
apply interp_reflect; assumption. |
| 515 |
Qed. |
| 516 |
|
| 517 |
Lemma related_bounded_value_annotate_base {t} |
| 518 |
v_st st v |
| 519 |
: @related_bounded_value (type.base t) v_st st v |
| 520 |
-> @related_bounded_value (type.base t) v_st (fst st, UnderLets.interp ident_interp (annotate true t (fst st) (snd st))) v. |
| 521 |
Proof using interp_annotate abstraction_relation'_Proper. |
| 522 |
clear -interp_annotate abstraction_relation'_Proper. |
| 523 |
cbv [Proper respectful Basics.impl] in *. |
| 524 |
cbn; break_innermost_match; cbn; intros. |
| 525 |
destruct_head'_and; subst; repeat apply conj; auto. |
| 526 |
rewrite interp_annotate by eauto; reflexivity. |
| 527 |
Qed. |
| 528 |
|
| 529 |
Lemma related_bounded_value_bottomify {t} v_st st v |
| 530 |
: @related_bounded_value t v_st st v |
| 531 |
-> @related_bounded_value t bottom (UnderLets.interp ident_interp (bottomify st)) v. |
| 532 |
Proof using bottom'_Proper bottom'_related. |
| 533 |
induction t; cbn in *; |
| 534 |
repeat first [ progress subst |
| 535 |
| progress cbv [respectful] in * |
| 536 |
| progress cbn [UnderLets.interp] in * |
| 537 |
| progress destruct_head'_and |
| 538 |
| break_innermost_match_step |
| 539 |
| progress intros |
| 540 |
| apply conj |
| 541 |
| reflexivity |
| 542 |
| apply bottom'_Proper |
| 543 |
| apply bottom'_related |
| 544 |
| solve [ eauto ] |
| 545 |
| rewrite UnderLets.interp_splice ]. |
| 546 |
Qed. |
| 547 |
|
| 548 |
Context (interp_ident_Proper |
| 549 |
: forall annotate_with_state t idc, |
| 550 |
related_bounded_value (ident_extract t idc) (UnderLets.interp ident_interp (interp_ident annotate_with_state t idc)) (ident_interp t idc)). |
| 551 |
|
| 552 |
Lemma interp_interp |
| 553 |
annotate_with_state G G' {t} (e_st e1 e2 e3 : expr t) |
| 554 |
(HG : forall t v1 v2 v3, List.In (existT _ t (v1, v2, v3)) G |
| 555 |
-> related_bounded_value_with_lets v1 v2 v3) |
| 556 |
(HG' : forall t v1 v2, List.In (existT _ t (v1, v2)) G' -> v1 == v2) |
| 557 |
(Hwf : expr.wf3 G e_st e1 e2) |
| 558 |
(Hwf' : expr.wf G' e2 e3) |
| 559 |
: related_bounded_value_with_lets |
| 560 |
(extract' e_st) |
| 561 |
(interp annotate_with_state e1) |
| 562 |
(expr.interp (@ident_interp) e2). |
| 563 |
Proof using interp_ident_Proper interp_annotate abstraction_relation'_Proper ident_interp_Proper' abstract_domain'_R_transitive abstract_domain'_R_symmetric bottom'_Proper bottom'_related. |
| 564 |
clear -ident_interp_Proper' interp_ident_Proper interp_annotate abstraction_relation'_Proper abstract_domain'_R_transitive abstract_domain'_R_symmetric bottom'_Proper bottom'_related HG HG' Hwf Hwf'. |
| 565 |
cbv [related_bounded_value_with_lets] in *; |
| 566 |
revert dependent annotate_with_state; revert dependent G'; induction Hwf; intros; |
| 567 |
cbn [extract' interp expr.interp UnderLets.interp List.In related_bounded_value reify reflect] in *; cbv [Let_In] in *. |
| 568 |
all: destruct annotate_with_state eqn:?. |
| 569 |
all: repeat first [ progress intros |
| 570 |
| progress subst |
| 571 |
| progress inversion_sigma |
| 572 |
| progress inversion_prod |
| 573 |
| progress destruct_head'_and |
| 574 |
| progress destruct_head'_or |
| 575 |
| progress destruct_head'_sig |
| 576 |
| progress destruct_head'_sigT |
| 577 |
| progress destruct_head'_prod |
| 578 |
| progress eta_expand |
| 579 |
| exfalso; assumption |
| 580 |
| progress cbn [UnderLets.interp List.In eq_rect fst snd projT1 projT2] in * |
| 581 |
| rewrite UnderLets.interp_splice |
| 582 |
| rewrite interp_annotate |
| 583 |
| solve [ cbv [Proper respectful Basics.impl] in *; unshelve eauto using related_of_related_bounded_value, related_bounded_value_bottomify ] |
| 584 |
| progress specialize_by_assumption |
| 585 |
| progress cbv [Let_In] in * |
| 586 |
| progress cbn [state_of_value extract'] in * |
| 587 |
| progress expr.invert_subst |
| 588 |
| match goal with |
| 589 |
| [ |- abstract_domain ?t ] => exact (@bottom t) |
| 590 |
| [ H : expr.wf _ _ _ |- Proper type.eqv _ ] |
| 591 |
=> apply expr.wf_interp_Proper_gen1 in H; |
| 592 |
[ cbv [Proper]; etransitivity; (idtac + symmetry); exact H | auto ] |
| 593 |
| [ H : _ |- _ ] |
| 594 |
=> (tryif first [ constr_eq H HG | constr_eq H HG' ] |
| 595 |
then fail |
| 596 |
else (apply H; clear H)) |
| 597 |
| [ |- related_bounded_value _ (fst _, UnderLets.interp _ (annotate _ _ _ _)) _ ] |
| 598 |
=> apply related_bounded_value_annotate_base |
| 599 |
| [ H : context[match ?v with None => _ | _ => _ end] |- _ ] => destruct v eqn:? |
| 600 |
| [ H : context[@related_bounded_value (type.base ?t) ?x _ ?y] |
| 601 |
|- @related_bounded_value (type.base ?t) ?x _ ?y ] |
| 602 |
=> eapply related_bounded_value_Proper_interp_eq_base; [ reflexivity | split; hnf | reflexivity | eapply H ]; |
| 603 |
cbn [fst snd expr.interp]; |
| 604 |
[ reflexivity | reflexivity | .. ] |
| 605 |
end |
| 606 |
| apply conj |
| 607 |
| match goal with |
| 608 |
| [ H : _ = _ |- _ ] => rewrite H |
| 609 |
end |
| 610 |
| break_innermost_match_step |
| 611 |
| progress expr.inversion_wf_one_constr |
| 612 |
| match goal with |
| 613 |
| [ H : _ |- _ ] |
| 614 |
=> (tryif first [ constr_eq H HG | constr_eq H HG' ] |
| 615 |
then fail |
| 616 |
else (eapply H; clear H; |
| 617 |
[ lazymatch goal with |
| 618 |
| [ |- expr.wf _ _ _ ] |
| 619 |
=> solve [ eassumption |
| 620 |
| match goal with |
| 621 |
| [ H : forall v1 v2, expr.wf _ _ _ |- expr.wf _ (?f ?x) _ ] |
| 622 |
=> apply (H x x) |
| 623 |
end ] |
| 624 |
| [ |- bool ] (* unused [annotate_with_state] argument to a [Proper ... /\ ...] lemma which is used for its [Proper] part *) |
| 625 |
=> constructor |
| 626 |
| _ => idtac |
| 627 |
end .. ]; |
| 628 |
match goal with |
| 629 |
[ |- ?G ] => assert_fails has_evar G |
| 630 |
end)) |
| 631 |
end |
| 632 |
| reflexivity ]. |
| 633 |
Qed. |
| 634 |
|
| 635 |
Lemma interp_eval_with_bound' |
| 636 |
annotate_with_state {t} (e_st e1 e2 : expr t) |
| 637 |
(Hwf : expr.wf3 nil e_st e1 e2) |
| 638 |
(Hwf' : expr.wf nil e2 e2) |
| 639 |
(Ht : type.is_not_higher_order t = true) |
| 640 |
(st : type.for_each_lhs_of_arrow abstract_domain t) |
| 641 |
(Hst : Proper (type.and_for_each_lhs_of_arrow (@abstract_domain_R)) st) |
| 642 |
: (forall arg1 arg2 |
| 643 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 644 |
(Harg1 : type.and_for_each_lhs_of_arrow (@abstraction_relation) st arg1), |
| 645 |
type.app_curried (expr.interp ident_interp (eval_with_bound' annotate_with_state e1 st)) arg1 |
| 646 |
= type.app_curried (expr.interp ident_interp e2) arg2) |
| 647 |
/\ (forall arg1 |
| 648 |
(Harg1 : type.and_for_each_lhs_of_arrow (@abstraction_relation) st arg1) |
| 649 |
(Harg11 : Proper (type.and_for_each_lhs_of_arrow (@type.eqv)) arg1), |
| 650 |
abstraction_relation' |
| 651 |
_ |
| 652 |
(extract_gen e_st st) |
| 653 |
(type.app_curried (expr.interp ident_interp (eval_with_bound' annotate_with_state e1 st)) arg1)). |
| 654 |
Proof using interp_annotate abstraction_relation'_Proper abstract_domain'_R_transitive abstract_domain'_R_symmetric bottom'_related interp_ident_Proper bottom'_Proper ident_interp_Proper'. |
| 655 |
cbv [extract_gen extract' eval_with_bound']. |
| 656 |
split; intros; rewrite UnderLets.interp_to_expr, UnderLets.interp_splice. |
| 657 |
all: eapply interp_reify_first_order; eauto. |
| 658 |
all: eapply interp_interp; eauto; wf_t. |
| 659 |
Qed. |
| 660 |
|
| 661 |
Lemma interp_eta_expand_with_bound' |
| 662 |
{t} (e1 e2 : expr t) |
| 663 |
(Hwf : expr.wf nil e1 e2) |
| 664 |
(b_in : type.for_each_lhs_of_arrow abstract_domain t) |
| 665 |
(Hb_in : Proper (type.and_for_each_lhs_of_arrow (@abstract_domain_R)) b_in) |
| 666 |
: forall arg1 arg2 |
| 667 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 668 |
(Harg1 : type.and_for_each_lhs_of_arrow (@abstraction_relation) b_in arg1), |
| 669 |
type.app_curried (expr.interp ident_interp (eta_expand_with_bound' e1 b_in)) arg1 = type.app_curried (expr.interp ident_interp e2) arg2. |
| 670 |
Proof using interp_annotate ident_interp_Proper' bottom'_related abstraction_relation'_Proper bottom'_Proper abstract_domain'_R_transitive abstract_domain'_R_symmetric. |
| 671 |
cbv [eta_expand_with_bound']. |
| 672 |
intros; rewrite UnderLets.interp_to_expr. |
| 673 |
eapply interp_reify; eauto. |
| 674 |
eapply interp_reflect; eauto using bottom_related. |
| 675 |
eapply @expr.wf_interp_Proper_gen; auto using Hwf. |
| 676 |
Qed. |
| 677 |
|
| 678 |
Lemma interp_extract'_from_wf_gen G |
| 679 |
(HG : forall t v1 v2, List.In (existT _ t (v1, v2)) G -> abstract_domain_R v1 v2) |
| 680 |
{t} (e1 e2 : expr t) |
| 681 |
(Hwf : expr.wf G e1 e2) |
| 682 |
: abstract_domain_R (extract' e1) (extract' e2). |
| 683 |
Proof using ident_extract_Proper bottom'_Proper. |
| 684 |
cbv [abstract_domain_R] in *; induction Hwf; cbn [extract']; break_innermost_match. |
| 685 |
all: repeat first [ progress subst |
| 686 |
| progress inversion_sigma |
| 687 |
| progress inversion_prod |
| 688 |
| solve [ cbv [Proper respectful] in *; eauto ] |
| 689 |
| progress cbv [respectful Let_In] in * |
| 690 |
| progress cbn [type.related List.In eq_rect partial.bottom] in * |
| 691 |
| progress intros |
| 692 |
| progress destruct_head'_or |
| 693 |
| apply bottom_Proper |
| 694 |
| match goal with H : _ |- type.related _ _ _ => apply H; clear H end ]. |
| 695 |
Qed. |
| 696 |
|
| 697 |
Lemma interp_extract'_from_wf {t} (e1 e2 : expr t) |
| 698 |
(Hwf : expr.wf nil e1 e2) |
| 699 |
: abstract_domain_R (extract' e1) (extract' e2). |
| 700 |
Proof using ident_extract_Proper bottom'_Proper. |
| 701 |
eapply interp_extract'_from_wf_gen; revgoals; wf_t. |
| 702 |
Qed. |
| 703 |
End with_type. |
| 704 |
|
| 705 |
Module ident. |
| 706 |
Import API. |
| 707 |
Local Notation UnderLets := (@UnderLets base.type ident). |
| 708 |
Section with_type. |
| 709 |
Context (abstract_domain' : base.type -> Type). |
| 710 |
Local Notation abstract_domain := (@abstract_domain base.type abstract_domain'). |
| 711 |
Local Notation var := interp_type. |
| 712 |
Context (annotate_expr : forall t, abstract_domain' t -> option (@expr var (t -> t))) |
| 713 |
(bottom' : forall A, abstract_domain' A) |
| 714 |
(abstract_interp_ident : forall t, ident t -> type.interp abstract_domain' t) |
| 715 |
(update_literal_with_state : forall A : base.type.base, abstract_domain' A -> base.interp A -> base.interp A) |
| 716 |
(extract_list_state : forall A, abstract_domain' (base.type.list A) -> option (list (abstract_domain' A))) |
| 717 |
(extract_option_state : forall A, abstract_domain' (base.type.option A) -> option (option (abstract_domain' A))) |
| 718 |
(is_annotated_for : forall t t', @expr var t -> abstract_domain' t' -> bool) |
| 719 |
(skip_annotations_under : forall t, ident t -> bool) |
| 720 |
(strip_annotation : forall t, ident t -> option (@value base.type ident var abstract_domain' t)) |
| 721 |
(abstraction_relation' : forall t, abstract_domain' t -> base.interp t -> Prop) |
| 722 |
(abstract_domain'_R : forall t, abstract_domain' t -> abstract_domain' t -> Prop) |
| 723 |
(abstraction_relation'_Proper : forall t, Proper (abstract_domain'_R t ==> eq ==> Basics.impl) (abstraction_relation' t)) |
| 724 |
(bottom'_related : forall t v, abstraction_relation' t (bottom' t) v) |
| 725 |
{bottom'_Proper : forall t, Proper (abstract_domain'_R t) (bottom' t)} |
| 726 |
{abstract_domain'_R_transitive : forall t, Transitive (@abstract_domain'_R t)} |
| 727 |
{abstract_domain'_R_symmetric : forall t, Symmetric (@abstract_domain'_R t)}. |
| 728 |
Local Notation abstraction_relation := (@abstraction_relation base.type abstract_domain' base.interp abstraction_relation'). |
| 729 |
Local Notation ident_interp := ident.interp (only parsing). |
| 730 |
Local Notation abstract_domain_R := (@abstract_domain_R base.type abstract_domain' abstract_domain'_R). |
| 731 |
Local Notation fill_in_bottom_for_arrows := (@fill_in_bottom_for_arrows base.type abstract_domain' bottom'). |
| 732 |
Local Notation related_bounded_value := (@related_bounded_value base.type ident abstract_domain' base.interp (@ident_interp) abstraction_relation' bottom' abstract_domain'_R). |
| 733 |
Context {abstract_interp_ident_Proper : forall t, Proper (eq ==> @abstract_domain_R t) (abstract_interp_ident t)} |
| 734 |
(interp_annotate_expr |
| 735 |
: forall t st idc, |
| 736 |
annotate_expr t st = Some idc |
| 737 |
-> forall v, abstraction_relation' _ st v |
| 738 |
-> expr.interp (@ident_interp) idc v = v) |
| 739 |
(abstract_interp_ident_Proper' |
| 740 |
: forall t idc, type.related_hetero (@abstraction_relation') (abstract_interp_ident t idc) (ident_interp idc)) |
| 741 |
(extract_list_state_related |
| 742 |
: forall t st ls v st' v', |
| 743 |
extract_list_state t st = Some ls |
| 744 |
-> abstraction_relation' _ st v |
| 745 |
-> List.In (st', v') (List.combine ls v) |
| 746 |
-> abstraction_relation' t st' v') |
| 747 |
(extract_list_state_length_good |
| 748 |
: forall t st ls v, |
| 749 |
extract_list_state t st = Some ls |
| 750 |
-> abstraction_relation' _ st v |
| 751 |
-> length ls = length v) |
| 752 |
(extract_option_state_related |
| 753 |
: forall t st a v, |
| 754 |
extract_option_state t st = Some a |
| 755 |
-> abstraction_relation' _ st v |
| 756 |
-> option_eq (abstraction_relation' t) a v) |
| 757 |
(strip_annotation_related |
| 758 |
: forall t idc v, |
| 759 |
strip_annotation t idc = Some v |
| 760 |
-> related_bounded_value (abstract_interp_ident t idc) v (ident_interp idc)). |
| 761 |
|
| 762 |
Local Notation update_annotation := (@ident.update_annotation _ abstract_domain' annotate_expr is_annotated_for). |
| 763 |
Local Notation annotate_with_expr := (@ident.annotate_with_expr _ abstract_domain' annotate_expr is_annotated_for). |
| 764 |
Local Notation annotate_base := (@ident.annotate_base _ abstract_domain' annotate_expr is_annotated_for). |
| 765 |
Local Notation annotate := (@ident.annotate _ abstract_domain' annotate_expr abstract_interp_ident extract_list_state extract_option_state is_annotated_for). |
| 766 |
Local Notation reify := (@reify base.type ident _ abstract_domain' annotate bottom'). |
| 767 |
Local Notation reflect := (@reflect base.type ident _ abstract_domain' annotate bottom'). |
| 768 |
|
| 769 |
Lemma abstract_interp_ident_Proper'' t idc |
| 770 |
: type.related_hetero (@abstraction_relation') (fill_in_bottom_for_arrows (abstract_interp_ident t idc)) (ident_interp idc). |
| 771 |
Proof using abstract_interp_ident_Proper' bottom'_related. |
| 772 |
generalize (abstract_interp_ident_Proper' t idc); clear -bottom'_related. |
| 773 |
generalize (ident_interp idc), (abstract_interp_ident t idc); clear idc. |
| 774 |
intros v st H. |
| 775 |
induction t as [| [|s' d'] IHs d IHd]; cbn in *; cbv [respectful_hetero] in *; |
| 776 |
auto. |
| 777 |
intros; apply IHd, H; clear IHd H. |
| 778 |
intros; apply bottom_related; assumption. |
| 779 |
Qed. |
| 780 |
|
| 781 |
Lemma interp_update_annotation t st e |
| 782 |
(He : abstraction_relation' t st (expr.interp (t:=type.base t) (@ident_interp) e)) |
| 783 |
: expr.interp (@ident_interp) (@update_annotation t st e) |
| 784 |
= expr.interp (@ident_interp) e. |
| 785 |
Proof using interp_annotate_expr. |
| 786 |
cbv [update_annotation]; |
| 787 |
repeat first [ reflexivity |
| 788 |
| progress subst |
| 789 |
| progress eliminate_hprop_eq |
| 790 |
| progress cbn [expr.interp eq_rect] in * |
| 791 |
| match goal with |
| 792 |
| [ H : annotate_expr _ _ = Some _ |- _ ] => rewrite (interp_annotate_expr _ _ _ H) by eassumption |
| 793 |
end |
| 794 |
| progress expr.invert_match |
| 795 |
| progress type_beq_to_eq |
| 796 |
| progress rewrite_type_transport_correct |
| 797 |
| progress break_innermost_match_step ]. |
| 798 |
Qed. |
| 799 |
|
| 800 |
Lemma interp_annotate_with_expr is_let_bound t st e |
| 801 |
(He : abstraction_relation' t st (expr.interp (t:=type.base t) (@ident_interp) e)) |
| 802 |
: expr.interp (@ident_interp) (UnderLets.interp (@ident_interp) (@annotate_with_expr is_let_bound t st e)) |
| 803 |
= expr.interp (@ident_interp) e. |
| 804 |
Proof using interp_annotate_expr. |
| 805 |
cbv [annotate_with_expr]; break_innermost_match; cbn [expr.interp UnderLets.interp]; |
| 806 |
apply interp_update_annotation; assumption. |
| 807 |
Qed. |
| 808 |
|
| 809 |
Lemma interp_annotate_base is_let_bound (t : base.type.base) st e |
| 810 |
(He : abstraction_relation' t st (expr.interp (t:=type.base (base.type.type_base t)) (@ident_interp) e)) |
| 811 |
: expr.interp (@ident_interp) (UnderLets.interp (@ident_interp) (@annotate_base is_let_bound t st e)) |
| 812 |
= expr.interp (@ident_interp) e. |
| 813 |
Proof using interp_annotate_expr. |
| 814 |
cbv [annotate_base]; break_innermost_match; expr.invert_subst; cbv beta iota in *; subst. |
| 815 |
{ apply interp_annotate_with_expr; assumption. } |
| 816 |
Qed. |
| 817 |
|
| 818 |
Lemma interp_annotate is_let_bound (t : base.type) st e |
| 819 |
(He : abstraction_relation' t st (expr.interp (t:=type.base t) (@ident_interp) e)) |
| 820 |
: expr.interp (@ident_interp) (UnderLets.interp (@ident_interp) (@annotate is_let_bound t st e)) |
| 821 |
= expr.interp (@ident_interp) e. |
| 822 |
Proof using interp_annotate_expr abstract_interp_ident_Proper' extract_list_state_related extract_list_state_length_good extract_option_state_related bottom'_related. |
| 823 |
induction t; cbn [annotate]; auto using interp_annotate_base. |
| 824 |
all: repeat first [ reflexivity |
| 825 |
| progress subst |
| 826 |
| progress inversion_option |
| 827 |
| progress inversion_prod |
| 828 |
| progress destruct_head'_ex |
| 829 |
| progress destruct_head'_and |
| 830 |
| progress break_innermost_match |
| 831 |
| progress break_innermost_match_hyps |
| 832 |
| progress expr.invert_subst |
| 833 |
| progress cbn [fst snd UnderLets.interp expr.interp ident_interp Nat.add] in * |
| 834 |
| progress cbv [ident.literal] in * |
| 835 |
| match goal with |
| 836 |
| [ H : context[ident_interp (ident.ident_Literal _)] |- _ ] => rewrite ident.interp_ident_Literal in H |
| 837 |
| [ H : context[ident_interp ident.ident_Some] |- _ ] => rewrite ident.interp_ident_Some in H |
| 838 |
| [ H : context[ident_interp ident.ident_pair] |- _ ] => rewrite ident.interp_ident_pair in H |
| 839 |
end |
| 840 |
| rewrite !UnderLets.interp_splice |
| 841 |
| rewrite !UnderLets.interp_splice_list |
| 842 |
| rewrite !List.map_map |
| 843 |
| rewrite expr.interp_reify_list |
| 844 |
| rewrite nth_error_combine |
| 845 |
| apply interp_annotate_with_expr; assumption |
| 846 |
| progress fold (@base.interp) in * |
| 847 |
| progress intros |
| 848 |
| pose proof (@extract_list_state_length_good _ _ _ _ ltac:(eassumption) ltac:(eassumption)); clear extract_list_state_length_good |
| 849 |
| match goal with |
| 850 |
| [ H : context[expr.interp _ (reify_list _)] |- _ ] => rewrite expr.interp_reify_list in H |
| 851 |
| [ H : abstraction_relation' (_ * _) _ (_, _) |- _ ] |
| 852 |
=> pose proof (abstract_interp_ident_Proper'' _ ident.fst _ _ H); |
| 853 |
pose proof (abstract_interp_ident_Proper'' _ ident.snd _ _ H); |
| 854 |
clear H |
| 855 |
| [ H : context[_ = _] |- _ = _ ] => rewrite H by assumption |
| 856 |
| [ |- List.map ?f (List.combine ?l1 ?l2) = List.map ?g ?l2 ] |
| 857 |
=> transitivity (List.map g (List.map (@snd _ _) (List.combine l1 l2))); |
| 858 |
[ rewrite List.map_map; apply List.map_ext_in |
| 859 |
| rewrite map_snd_combine, List.firstn_all2; [ reflexivity | ] ] |
| 860 |
| [ Hls : extract_list_state ?t ?st = Some ?ls, He : abstraction_relation' _ ?st ?v |- abstraction_relation' _ _ _ ] |
| 861 |
=> apply (fun st' v' => extract_list_state_related t st ls v st' v' Hls He) |
| 862 |
| [ H : context[List.nth_error (List.combine _ _) _] |- _ ] => rewrite nth_error_combine in H |
| 863 |
| [ H : List.In _ (List.combine _ _) |- _ ] => apply List.In_nth_error in H |
| 864 |
| [ |- List.In _ (List.combine _ _) ] => eapply nth_error_In |
| 865 |
| [ H : ?x = Some _, H' : context[?x] |- _ ] => rewrite H in H' |
| 866 |
| [ H : List.nth_error (List.map _ _) _ = Some _ |- _ ] => apply nth_error_map_ex in H |
| 867 |
| [ H : List.nth_error _ _ = None |- _ ] => rewrite List.nth_error_None in H |
| 868 |
| [ H : context[length ?ls] |- _ ] => tryif is_var ls then fail else (progress autorewrite with distr_length in H) |
| 869 |
| [ |- context[length ?ls] ] => tryif is_var ls then fail else (progress autorewrite with distr_length) |
| 870 |
| [ H : List.nth_error ?ls ?n = Some _, H' : length ?ls <= ?n |- _ ] |
| 871 |
=> apply nth_error_value_length in H; exfalso; clear -H H'; lia |
| 872 |
| [ H : List.nth_error ?l ?n = _, H' : List.nth_error ?l ?n' = _ |- _ ] |
| 873 |
=> unify n n'; rewrite H in H' |
| 874 |
| [ Hls : extract_list_state ?t ?st = Some ?ls, He : abstraction_relation' _ ?st ?v |- _ ] |
| 875 |
=> pose proof (fun st' v' => extract_list_state_related t st ls v st' v' Hls He); clear extract_list_state_related |
| 876 |
| [ IH : forall st e, _ -> expr.interp _ (UnderLets.interp _ (annotate _ _ _)) = _ |- List.map (fun x => expr.interp _ _) (List.combine _ _) = expr.interp _ _ ] |
| 877 |
=> erewrite List.map_ext_in; |
| 878 |
[ |
| 879 |
| intros; eta_expand; rewrite IH; cbn [expr.interp ident_interp ident.smart_Literal]; [ reflexivity | ] ] |
| 880 |
| [ H : abstraction_relation' _ ?st (List.map (expr.interp _) ?ls), H' : forall st' v', List.In _ (List.combine _ _) -> abstraction_relation' _ _ _, H'' : List.nth_error ?ls _ = Some ?e |- abstraction_relation' _ _ (expr.interp _ ?e) ] |
| 881 |
=> apply H' |
| 882 |
| [ H : context[List.nth_error (List.seq _ _) _] |- _ ] => rewrite nth_error_seq in H |
| 883 |
end |
| 884 |
| apply Nat.eq_le_incl |
| 885 |
| rewrite <- List.map_map with (f:=fst), map_fst_combine |
| 886 |
| match goal with |
| 887 |
| [ |- context[List.map (fun a : ?A * ?B => @?body a) (List.combine _ _)] ] |
| 888 |
=> let aa := fresh in |
| 889 |
let bb := fresh in |
| 890 |
let g' := match (eval cbn [fst] in (fun (bb : B) (aa : A) => body (aa, bb))) with |
| 891 |
| fun _ => ?g => g |
| 892 |
end in |
| 893 |
erewrite <- List.map_map with (f:=fst) (g:=g'), map_fst_combine |
| 894 |
end |
| 895 |
| rewrite Lists.List.firstn_all2 by distr_length |
| 896 |
| apply map_nth_default_seq |
| 897 |
| progress destruct_head' option |
| 898 |
| progress cbn [Option.combine option_map reify_option option_rect UnderLets.splice_option] in * |
| 899 |
| apply (f_equal Some) |
| 900 |
| match goal with |
| 901 |
| [ H : abstraction_relation' _ ?st _, H' : extract_option_state _ ?st = _ |- _ ] |
| 902 |
=> eapply extract_option_state_related in H; [ clear H' | eexact H' ]; |
| 903 |
cbv [option_eq] in H |
| 904 |
| [ H : context[expr.interp _ _ = expr.interp _ _] |- expr.interp _ _ = expr.interp _ _ ] => apply H; clear H |
| 905 |
| [ H : forall st' v', List.In _ (List.combine _ _) -> abstraction_relation' _ _ _ |- abstraction_relation' _ _ _ ] |
| 906 |
=> apply H; clear H; cbv [List.nth_default] |
| 907 |
| [ |- None = Some _ ] => exfalso; lia |
| 908 |
end ]. |
| 909 |
Qed. |
| 910 |
|
| 911 |
Local Notation interp_ident := (@ident.interp_ident _ abstract_domain' annotate_expr bottom' abstract_interp_ident extract_list_state extract_option_state is_annotated_for strip_annotation). |
| 912 |
|
| 913 |
Lemma interp_ident_Proper_not_nth_default_nostrip annotate_with_state t idc |
| 914 |
: related_bounded_value (abstract_interp_ident t idc) (UnderLets.interp (@ident_interp) (Base (reflect annotate_with_state (expr.Ident idc) (abstract_interp_ident _ idc)))) (ident_interp idc). |
| 915 |
Proof using abstract_interp_ident_Proper' abstraction_relation'_Proper bottom'_related extract_list_state_length_good extract_list_state_related extract_option_state_related interp_annotate_expr abstract_interp_ident_Proper bottom'_Proper abstract_domain'_R_transitive abstract_domain'_R_symmetric. |
| 916 |
cbn [UnderLets.interp]. |
| 917 |
eapply interp_reflect; |
| 918 |
try first [ apply ident.interp_Proper |
| 919 |
| apply abstract_interp_ident_Proper'' |
| 920 |
| eapply abstract_interp_ident_Proper; reflexivity |
| 921 |
| apply interp_annotate ]; |
| 922 |
eauto. |
| 923 |
Qed. |
| 924 |
|
| 925 |
Lemma interp_ident_Proper_not_nth_default annotate_with_state t idc |
| 926 |
: related_bounded_value |
| 927 |
(abstract_interp_ident t idc) |
| 928 |
((UnderLets.interp (@ident_interp)) |
| 929 |
(Base match strip_annotation _ idc with |
| 930 |
| Some v => v |
| 931 |
| None => reflect annotate_with_state (expr.Ident idc) (abstract_interp_ident _ idc) |
| 932 |
end)) |
| 933 |
(ident_interp idc). |
| 934 |
Proof using abstract_interp_ident_Proper' abstraction_relation'_Proper bottom'_related extract_list_state_length_good extract_list_state_related extract_option_state_related interp_annotate_expr abstract_interp_ident_Proper bottom'_Proper abstract_domain'_R_transitive abstract_domain'_R_symmetric strip_annotation_related. |
| 935 |
pose proof (strip_annotation_related t idc) as H. |
| 936 |
break_innermost_match; eauto using eq_refl, interp_ident_Proper_not_nth_default_nostrip. |
| 937 |
Qed. |
| 938 |
|
| 939 |
Lemma interp_ident_Proper_nth_default annotate_with_state T (idc:[email protected]_nth_default T) |
| 940 |
: related_bounded_value (abstract_interp_ident _ idc) (UnderLets.interp (@ident_interp) (interp_ident annotate_with_state idc)) (ident_interp idc). |
| 941 |
Proof using abstract_interp_ident_Proper abstract_interp_ident_Proper' abstraction_relation'_Proper extract_list_state_length_good extract_list_state_related extract_option_state_related interp_annotate_expr bottom'_related. |
| 942 |
subst idc; cbn [interp_ident reify reflect fst snd UnderLets.interp ident_interp related_bounded_value abstract_domain value]. |
| 943 |
cbv [abstract_domain]; cbn [type.interp bottom_for_each_lhs_of_arrow state_of_value fst snd]. |
| 944 |
repeat first [ progress intros |
| 945 |
| progress cbn [UnderLets.interp fst snd expr.interp ident_interp] in * |
| 946 |
| progress destruct_head'_prod |
| 947 |
| progress destruct_head'_and |
| 948 |
| progress subst |
| 949 |
| progress eta_expand |
| 950 |
| rewrite UnderLets.interp_splice |
| 951 |
| progress expr.invert_subst |
| 952 |
| break_innermost_match_step |
| 953 |
| progress cbn [type.interp base.interp base.base_interp] in * |
| 954 |
| rewrite interp_annotate |
| 955 |
| solve [ cbv [Proper respectful Basics.impl] in *; eauto ] |
| 956 |
| split; [ apply (@abstract_interp_ident_Proper _ (@ident.List_nth_default T) _ eq_refl) | ] |
| 957 |
| split; [ reflexivity | ] |
| 958 |
| apply (@abstract_interp_ident_Proper'' _ (@ident.List_nth_default T)) |
| 959 |
| apply conj |
| 960 |
| rewrite map_nth_default_always |
| 961 |
| rewrite expr.interp_reify_list |
| 962 |
| match goal with |
| 963 |
| [ H : context[expr.interp _ (UnderLets.interp _ (annotate _ _ _))] |- _ ] |
| 964 |
=> rewrite interp_annotate in H |
| 965 |
| [ H : context[expr.interp _ (reify_list _)] |- _ ] |
| 966 |
=> rewrite expr.interp_reify_list in H |
| 967 |
| [ H : _ = reify_list _ |- _ ] => apply (f_equal (expr.interp (@ident_interp))) in H |
| 968 |
| [ H : expr.interp _ ?x = _ |- context[expr.interp _ ?x] ] => rewrite H |
| 969 |
| [ |- Proper _ _ ] => cbv [Proper type.related respectful] |
| 970 |
end ]. |
| 971 |
Qed. |
| 972 |
|
| 973 |
Lemma interp_ident_Proper annotate_with_state t idc |
| 974 |
: related_bounded_value (abstract_interp_ident t idc) (UnderLets.interp (@ident_interp) (interp_ident annotate_with_state idc)) (ident_interp idc). |
| 975 |
Proof using abstract_domain'_R_symmetric abstract_domain'_R_transitive abstract_interp_ident_Proper abstract_interp_ident_Proper' abstraction_relation'_Proper bottom'_Proper bottom'_related extract_list_state_length_good extract_list_state_related extract_option_state_related interp_annotate_expr strip_annotation_related. |
| 976 |
pose idc as idc'. |
| 977 |
destruct idc; first [ refine (@interp_ident_Proper_not_nth_default _ _ idc') |
| 978 |
| refine (@interp_ident_Proper_nth_default _ _) ]. |
| 979 |
Qed. |
| 980 |
|
| 981 |
Local Notation eval_with_bound := (@partial.ident.eval_with_bound _ abstract_domain' annotate_expr bottom' abstract_interp_ident extract_list_state extract_option_state is_annotated_for strip_annotation). |
| 982 |
Local Notation eta_expand_with_bound := (@partial.ident.eta_expand_with_bound _ abstract_domain' annotate_expr bottom' abstract_interp_ident extract_list_state extract_option_state is_annotated_for). |
| 983 |
Local Notation extract := (@ident.extract abstract_domain' bottom' abstract_interp_ident). |
| 984 |
|
| 985 |
Lemma interp_eval_with_bound |
| 986 |
annotate_with_state {t} (e_st e1 e2 : expr t) |
| 987 |
(Hwf : expr.wf3 nil e_st e1 e2) |
| 988 |
(Hwf' : expr.wf nil e2 e2) |
| 989 |
(Ht : type.is_not_higher_order t = true) |
| 990 |
(st : type.for_each_lhs_of_arrow abstract_domain t) |
| 991 |
(Hst : Proper (type.and_for_each_lhs_of_arrow (@abstract_domain_R)) st) |
| 992 |
: (forall arg1 arg2 |
| 993 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 994 |
(Harg1 : type.and_for_each_lhs_of_arrow (@abstraction_relation) st arg1), |
| 995 |
type.app_curried (expr.interp (@ident_interp) (eval_with_bound skip_annotations_under annotate_with_state e1 st)) arg1 |
| 996 |
= type.app_curried (expr.interp (@ident_interp) e2) arg2) |
| 997 |
/\ (forall arg1 |
| 998 |
(Harg1 : type.and_for_each_lhs_of_arrow (@abstraction_relation) st arg1) |
| 999 |
(Harg11 : Proper (type.and_for_each_lhs_of_arrow (@type.eqv)) arg1), |
| 1000 |
abstraction_relation' |
| 1001 |
_ |
| 1002 |
(extract e_st st) |
| 1003 |
(type.app_curried (expr.interp (@ident_interp) (eval_with_bound skip_annotations_under annotate_with_state e1 st)) arg1)). |
| 1004 |
Proof using abstract_domain'_R_symmetric abstract_domain'_R_transitive abstract_interp_ident_Proper abstract_interp_ident_Proper' abstraction_relation'_Proper bottom'_Proper bottom'_related extract_list_state_length_good extract_list_state_related extract_option_state_related interp_annotate_expr strip_annotation_related. cbv [extract eval_with_bound]; apply @interp_eval_with_bound' with (abstract_domain'_R:=abstract_domain'_R); auto using interp_annotate, interp_ident_Proper, ident.interp_Proper. Qed. |
| 1005 |
|
| 1006 |
Lemma interp_eta_expand_with_bound |
| 1007 |
{t} (e1 e2 : expr t) |
| 1008 |
(Hwf : expr.wf nil e1 e2) |
| 1009 |
(Ht : type.is_not_higher_order t = true) |
| 1010 |
(b_in : type.for_each_lhs_of_arrow abstract_domain t) |
| 1011 |
(Hb_in : Proper (type.and_for_each_lhs_of_arrow (@abstract_domain_R)) b_in) |
| 1012 |
: forall arg1 arg2 |
| 1013 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 1014 |
(Harg1 : type.and_for_each_lhs_of_arrow (@abstraction_relation) b_in arg1), |
| 1015 |
type.app_curried (expr.interp (@ident_interp) (eta_expand_with_bound e1 b_in)) arg1 = type.app_curried (expr.interp (@ident_interp) e2) arg2. |
| 1016 |
Proof using abstract_domain'_R_symmetric abstract_domain'_R_transitive abstract_interp_ident_Proper' abstraction_relation'_Proper bottom'_Proper bottom'_related extract_list_state_length_good extract_list_state_related extract_option_state_related interp_annotate_expr. cbv [partial.ident.eta_expand_with_bound]; eapply interp_eta_expand_with_bound'; eauto using interp_annotate, ident.interp_Proper. Qed. |
| 1017 |
End with_type. |
| 1018 |
End ident. |
| 1019 |
|
| 1020 |
Lemma default_relax_zrange_good |
| 1021 |
: forall r r' z, is_tighter_than_bool z r = true |
| 1022 |
-> default_relax_zrange r = Some r' |
| 1023 |
-> is_tighter_than_bool z r' = true. |
| 1024 |
Proof. |
| 1025 |
cbv [default_relax_zrange]; intros; inversion_option; subst; assumption. |
| 1026 |
Qed. |
| 1027 |
|
| 1028 |
Section specialized. |
| 1029 |
Import API. |
| 1030 |
Local Notation abstract_domain' := ZRange.type.base.option.interp (only parsing). |
| 1031 |
Local Notation abstract_domain := (@partial.abstract_domain base.type abstract_domain'). |
| 1032 |
Local Notation abstract_domain'_R t := (@eq (abstract_domain' t)) (only parsing). |
| 1033 |
Local Notation abstract_domain_R := (@abstract_domain_R base.type abstract_domain' (fun t => abstract_domain'_R t)). |
| 1034 |
Local Notation fill_in_bottom_for_arrows := (@fill_in_bottom_for_arrows base.type abstract_domain' bottom'). |
| 1035 |
|
| 1036 |
Definition abstraction_relation' {t} : abstract_domain' t -> base.interp t -> Prop |
| 1037 |
:= fun st v => @ZRange.type.base.option.is_bounded_by t st v = true. |
| 1038 |
|
| 1039 |
Lemma bottom'_bottom {t} : forall v, abstraction_relation' (bottom' t) v. |
| 1040 |
Proof using Type. |
| 1041 |
cbv [abstraction_relation' bottom']; induction t; cbn; intros; break_innermost_match; cbn; try reflexivity. |
| 1042 |
rewrite Bool.andb_true_iff; split; auto. |
| 1043 |
Qed. |
| 1044 |
|
| 1045 |
Lemma abstract_interp_ident_related {opts : AbstractInterpretation.Options} {assume_cast_truncates : bool} {t} (idc : ident t) |
| 1046 |
: type.related_hetero (@abstraction_relation') (abstract_interp_ident assume_cast_truncates t idc) (ident.interp idc). |
| 1047 |
Proof using Type. apply ZRange.ident.option.interp_related. Qed. |
| 1048 |
|
| 1049 |
Local Ltac zrange_interp_idempotent_t := |
| 1050 |
repeat first [ progress destruct_head'_ex |
| 1051 |
| progress subst |
| 1052 |
| progress cbn in * |
| 1053 |
| progress cbv [ZRange.goodb ZRange.ident.option.interp_Z_cast_truncate ZRange.ident.option.interp_Z_cast] in * |
| 1054 |
| reflexivity |
| 1055 |
| lia |
| 1056 |
| progress Z.ltb_to_lt |
| 1057 |
| progress destruct_head'_and |
| 1058 |
| break_innermost_match_step |
| 1059 |
| break_innermost_match_hyps_step |
| 1060 |
| rewrite (proj1 ZRange.normalize_id_iff_goodb) |
| 1061 |
| progress Bool.split_andb |
| 1062 |
| match goal with |
| 1063 |
| [ H : ?x = ?x |- _ ] => clear H |
| 1064 |
| [ H : (?x <= ?y)%Z, H' : (?y <= ?x)%Z |- _ ] |
| 1065 |
=> assert (x = y) by lia; clear H H' |
| 1066 |
end |
| 1067 |
| progress unshelve Reflect.reflect_hyps; [] |
| 1068 |
| progress destruct_head'_or |
| 1069 |
| progress destruct_head' zrange |
| 1070 |
| rewrite Bool.andb_true_iff |
| 1071 |
| match goal with |
| 1072 |
| [ |- and _ _ ] => split |
| 1073 |
end |
| 1074 |
| progress cbv [is_bounded_by_bool] ]. |
| 1075 |
|
| 1076 |
Lemma zrange_interp_tighter_bounded_Z_cast {opts : AbstractInterpretation.Options} {assume_cast_truncates r1 r2 v} |
| 1077 |
(H1 : ZRange.type.base.option.is_bounded_by (t:=base.type.Z) r1 v = true) |
| 1078 |
(H2 : ZRange.type.base.option.is_bounded_by (t:=base.type.Z) r2 v = true) |
| 1079 |
: ZRange.type.base.option.is_bounded_by (t:=base.type.Z) (ZRange.ident.option.interp assume_cast_truncates ident.Z_cast r1 r2) v = true. |
| 1080 |
Proof using Type. destruct r1, r2, assume_cast_truncates; zrange_interp_idempotent_t. Qed. |
| 1081 |
|
| 1082 |
Lemma zrange_interp_tighter_bounded_Z_cast2 {opts : AbstractInterpretation.Options} {assume_cast_truncates r1 r2 v} |
| 1083 |
(H1 : ZRange.type.base.option.is_bounded_by (t:=base.type.Z*base.type.Z) r1 v = true) |
| 1084 |
(H2 : ZRange.type.base.option.is_bounded_by (t:=base.type.Z*base.type.Z) r2 v = true) |
| 1085 |
: ZRange.type.base.option.is_bounded_by (t:=base.type.Z*base.type.Z) (ZRange.ident.option.interp assume_cast_truncates ident.Z_cast2 r1 r2) v = true. |
| 1086 |
Proof using Type. destruct r1, r2, assume_cast_truncates; zrange_interp_idempotent_t. Qed. |
| 1087 |
|
| 1088 |
Local Notation related_bounded_value := (@related_bounded_value base.type ident abstract_domain' base.interp (@ident.interp) (@abstraction_relation') bottom' (fun t => abstract_domain'_R t)). |
| 1089 |
Lemma always_strip_annotation_related {opts : AbstractInterpretation.Options} {assume_cast_truncates : bool} {t} (idc : ident t) |
| 1090 |
v (Hv : always_strip_annotation assume_cast_truncates t idc = Some v) |
| 1091 |
: related_bounded_value (abstract_interp_ident assume_cast_truncates t idc) v (ident.interp idc). |
| 1092 |
Proof using Type. |
| 1093 |
pose proof (@abstract_interp_ident_related _ assume_cast_truncates _ ident.Z_cast). |
| 1094 |
pose proof (fun x1 x2 y1 y2 H x01 x02 y01 y02 H' => @abstract_interp_ident_related _ assume_cast_truncates _ ident.Z_cast2 (x1, x2) (y1, y2) H (x01, x02) (y01, y02) H'). |
| 1095 |
cbv [always_strip_annotation] in *; break_innermost_match_hyps; inversion_option; subst. |
| 1096 |
all: repeat first [ reflexivity |
| 1097 |
| progress cbn [related_bounded_value UnderLets.interp fst snd type.related_hetero] in * |
| 1098 |
| progress cbv [respectful_hetero] in * |
| 1099 |
| progress intros |
| 1100 |
| progress destruct_head'_and |
| 1101 |
| progress subst |
| 1102 |
| progress inversion_prod |
| 1103 |
| progress inversion_option |
| 1104 |
| match goal with |
| 1105 |
| [ |- Proper _ _ ] => exact _ |
| 1106 |
| [ |- _ /\ _ ] => split |
| 1107 |
| [ H1 : (?x <=? ?y)%zrange = true, H2 : is_bounded_by_bool _ ?x = true |- _ ] |
| 1108 |
=> unique pose proof (ZRange.is_bounded_by_of_is_tighter_than _ _ H1 _ H2) |
| 1109 |
| [ |- andb _ _ = true ] => rewrite Bool.andb_true_iff |
| 1110 |
| [ H : andb _ _ = true |- _ ] => rewrite Bool.andb_true_iff in H |
| 1111 |
| [ H : ZRange.type.base.option.is_bounded_by (Some _) _ = _ |- _ ] |
| 1112 |
=> progress cbn -[zrange_beq prod_beq] in H |
| 1113 |
| [ H : ZRange.type.base.option.is_bounded_by (Some _, Some _) _ = _ |- _ ] |
| 1114 |
=> progress cbn -[zrange_beq prod_beq] in H |
| 1115 |
| [ H : ZRange.type.base.is_tighter_than _ _ = _ |- _ ] |
| 1116 |
=> progress cbn -[zrange_beq prod_beq] in H |
| 1117 |
| [ H : bind _ _ = Some _ |- _ ] => progress cbv [bind] in H |
| 1118 |
end |
| 1119 |
| progress break_innermost_match |
| 1120 |
| progress break_innermost_match_hyps |
| 1121 |
| progress Reflect.reflect_beq_to_eq zrange_beq |
| 1122 |
| progress cbv [abstraction_relation' ZRange.type.base.option.lift_Some] in * |
| 1123 |
| cbn [Compilers.ident_interp]; progress cbv [ident.cast2] |
| 1124 |
| rewrite ident.cast_in_bounds by assumption |
| 1125 |
| now destruct assume_cast_truncates |
| 1126 |
| rewrite zrange_interp_idempotent_Z_cast by (cbn; eexists; eassumption) |
| 1127 |
| rewrite zrange_interp_idempotent_Z_cast2 by (cbn; eexists (_, _); rewrite Bool.andb_true_iff; split; eassumption) |
| 1128 |
| progress cbv [abstract_interp_ident] in * |
| 1129 |
| match goal with |
| 1130 |
| [ H : _ |- _ ] => apply H; cbn; cbn [ZRange.type.base.option.is_bounded_by] in *; Prod.eta_expand; rewrite ?Bool.andb_true_iff in *; destruct_head'_and; try apply conj; try apply zrange_lb; now auto |
| 1131 |
end |
| 1132 |
| progress cbn [ZRange.type.base.option.is_bounded_by] |
| 1133 |
| match goal with |
| 1134 |
| [ H : ZRange.ident.option.interp ?act ident.Z_cast ?r1 ?r2 = Some ?r3 |- ZRange.type.base.is_bounded_by ?r3 ?v = true ] |
| 1135 |
=> let H' := fresh in |
| 1136 |
pose proof (@zrange_interp_tighter_bounded_Z_cast _ act r1 r2 v) as H'; |
| 1137 |
rewrite H in H'; now apply H' |
| 1138 |
| [ H : context G[ZRange.ident.option.interp ?act ident.Z_cast2 ?r1 ?r2] |- _ ] |
| 1139 |
=> let G' := context G[(ZRange.ident.option.interp act ident.Z_cast (fst r1) (fst r2), |
| 1140 |
ZRange.ident.option.interp act ident.Z_cast (snd r1) (snd r2))] in |
| 1141 |
change G' in H |
| 1142 |
end ]. |
| 1143 |
Qed. |
| 1144 |
|
| 1145 |
Lemma strip_annotation_related {opts : AbstractInterpretation.Options} {assume_cast_truncates strip_annotations : bool} {t} (idc : ident t) |
| 1146 |
v (Hv : strip_annotation assume_cast_truncates strip_annotations t idc = Some v) |
| 1147 |
: related_bounded_value (abstract_interp_ident assume_cast_truncates t idc) v (ident.interp idc). |
| 1148 |
Proof using Type. |
| 1149 |
destruct strip_annotations; cbv [strip_annotation] in *; try congruence; |
| 1150 |
now apply always_strip_annotation_related. |
| 1151 |
Qed. |
| 1152 |
|
| 1153 |
Lemma extract_list_state_related {t} st v ls |
| 1154 |
: @abstraction_relation' _ st v |
| 1155 |
-> @extract_list_state t st = Some ls |
| 1156 |
-> length ls = length v |
| 1157 |
/\ forall st' (v' : base.interp t), List.In (st', v') (List.combine ls v) -> @abstraction_relation' t st' v'. |
| 1158 |
Proof using Type. |
| 1159 |
cbv [abstraction_relation' extract_list_state]; cbn [ZRange.type.base.option.is_bounded_by]. |
| 1160 |
intros; subst. |
| 1161 |
split. |
| 1162 |
{ eapply FoldBool.fold_andb_map_length; eassumption. } |
| 1163 |
{ intros *. |
| 1164 |
revert dependent v; induction ls, v; cbn; try tauto. |
| 1165 |
rewrite Bool.andb_true_iff. |
| 1166 |
intros; destruct_head'_and; destruct_head'_or; inversion_prod; subst; eauto. } |
| 1167 |
Qed. |
| 1168 |
|
| 1169 |
Lemma extract_option_state_related {t} st a v |
| 1170 |
: extract_option_state t st = Some a |
| 1171 |
-> @abstraction_relation' _ st v |
| 1172 |
-> option_eq (@abstraction_relation' t) a v. |
| 1173 |
Proof using Type. |
| 1174 |
cbv [abstraction_relation' extract_option_state option_eq]; intros; subst; cbn in *; cbv [option_beq_hetero] in *; break_match; break_match_hyps; auto; congruence. |
| 1175 |
Qed. |
| 1176 |
|
| 1177 |
Lemma Extract_FromFlat_ToFlat' {opts : AbstractInterpretation.Options} {assume_cast_truncates : bool} {t} (e : Expr t) (Hwf : Wf e) b_in1 b_in2 |
| 1178 |
(Hb : type.and_for_each_lhs_of_arrow (fun t => type.eqv) b_in1 b_in2) |
| 1179 |
: partial.Extract assume_cast_truncates (GeneralizeVar.FromFlat (GeneralizeVar.ToFlat e)) b_in1 |
| 1180 |
= partial.Extract assume_cast_truncates e b_in2. |
| 1181 |
Proof using Type. |
| 1182 |
cbv [partial.Extract partial.ident.extract partial.extract_gen]. |
| 1183 |
revert b_in1 b_in2 Hb. |
| 1184 |
rewrite <- (@type.related_iff_app_curried base.type ZRange.type.base.option.interp (fun _ => eq)). |
| 1185 |
apply interp_extract'_from_wf; auto with wf typeclass_instances. |
| 1186 |
apply GeneralizeVar.wf_from_flat_to_flat, Hwf. |
| 1187 |
Qed. |
| 1188 |
|
| 1189 |
Lemma Extract_FromFlat_ToFlat {opts : AbstractInterpretation.Options} {assume_cast_truncates : bool} {t} (e : Expr t) (Hwf : Wf e) b_in |
| 1190 |
(Hb : Proper (type.and_for_each_lhs_of_arrow (fun t => type.eqv)) b_in) |
| 1191 |
: partial.Extract assume_cast_truncates (GeneralizeVar.FromFlat (GeneralizeVar.ToFlat e)) b_in |
| 1192 |
= partial.Extract assume_cast_truncates e b_in. |
| 1193 |
Proof using Type. apply Extract_FromFlat_ToFlat'; assumption. Qed. |
| 1194 |
|
| 1195 |
Lemma Extract_GeneralizeVar {opts : AbstractInterpretation.Options} {assume_cast_truncates : bool} {t} (e : Expr t) (Hwf : Wf e) b_in |
| 1196 |
(Hb : Proper (type.and_for_each_lhs_of_arrow (fun t => type.eqv)) b_in) |
| 1197 |
: partial.Extract assume_cast_truncates (GeneralizeVar.GeneralizeVar (e _)) b_in |
| 1198 |
= partial.Extract assume_cast_truncates e b_in. |
| 1199 |
Proof using Type. apply Extract_FromFlat_ToFlat; assumption. Qed. |
| 1200 |
|
| 1201 |
Section with_relax. |
| 1202 |
Context {relax_zrange : zrange -> option zrange} |
| 1203 |
(Hrelax : forall r r' z, is_tighter_than_bool z r = true |
| 1204 |
-> relax_zrange r = Some r' |
| 1205 |
-> is_tighter_than_bool z r' = true). |
| 1206 |
|
| 1207 |
Local Lemma Hrelax' r r' z |
| 1208 |
: is_bounded_by_bool z r = true |
| 1209 |
-> relax_zrange (ZRange.normalize r) = Some r' |
| 1210 |
-> is_bounded_by_bool z r' = true. |
| 1211 |
Proof using Hrelax. |
| 1212 |
intros H Hr. |
| 1213 |
eapply ZRange.is_bounded_by_of_is_tighter_than; [ eapply Hrelax; [ | eassumption ] | eassumption ]. |
| 1214 |
eapply ZRange.is_tighter_than_bool_normalize_of_goodb, ZRange.goodb_of_is_bounded_by_bool; eassumption. |
| 1215 |
Qed. |
| 1216 |
|
| 1217 |
Lemma interp_annotate_expr {t} st idc |
| 1218 |
(Hst : @annotate_expr relax_zrange _ t st = Some idc) |
| 1219 |
: forall v, abstraction_relation' st v |
| 1220 |
-> expr.interp (@ident.interp) idc v = v. |
| 1221 |
Proof using Hrelax. |
| 1222 |
repeat first [ progress cbv [annotate_expr Crypto.Util.Option.bind annotation_of_state option_map abstraction_relation' ZRange.type.base.option.is_bounded_by ZRange.type.base.is_bounded_by] in * |
| 1223 |
| reflexivity |
| 1224 |
| progress inversion_option |
| 1225 |
| progress subst |
| 1226 |
| break_innermost_match_hyps_step |
| 1227 |
| break_innermost_match_step |
| 1228 |
| progress cbn [ident.interp base.interp base.base_interp expr.interp] in * |
| 1229 |
| progress cbv [ident.cast2] in * |
| 1230 |
| progress intros |
| 1231 |
| progress Bool.split_andb |
| 1232 |
| rewrite ident.cast_in_bounds by assumption |
| 1233 |
| match goal with |
| 1234 |
| [ H : is_bounded_by_bool ?v ?r = true, H' : relax_zrange (ZRange.normalize ?r) = Some ?r' |- _ ] |
| 1235 |
=> unique assert (is_bounded_by_bool v r' = true) by (eapply Hrelax'; eassumption) |
| 1236 |
end ]. |
| 1237 |
Qed. |
| 1238 |
|
| 1239 |
Lemma interp_annotate_expr_Proper {t} st1 st2 (Hst : abstract_domain'_R t st1 st2) |
| 1240 |
: @annotate_expr relax_zrange interp_type t st1 = @annotate_expr relax_zrange interp_type t st2. |
| 1241 |
Proof using Type. congruence. Qed. |
| 1242 |
|
| 1243 |
Local Hint Resolve interp_annotate_expr abstract_interp_ident_related : core. |
| 1244 |
|
| 1245 |
Lemma interp_eval_with_bound |
| 1246 |
{opts : AbstractInterpretation.Options} |
| 1247 |
{assume_cast_truncates : bool} |
| 1248 |
{skip_annotations_under : forall t, ident t -> bool} |
| 1249 |
{strip_preexisting_annotations : bool} |
| 1250 |
{t} (e_st e1 e2 : expr t) |
| 1251 |
(Hwf : expr.wf3 nil e_st e1 e2) |
| 1252 |
(Hwf' : expr.wf nil e2 e2) |
| 1253 |
(Ht : type.is_not_higher_order t = true) |
| 1254 |
(st : type.for_each_lhs_of_arrow abstract_domain t) |
| 1255 |
: (forall arg1 arg2 |
| 1256 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 1257 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) st arg1 = true), |
| 1258 |
type.app_curried (expr.interp (@ident.interp) (eval_with_bound relax_zrange assume_cast_truncates skip_annotations_under strip_preexisting_annotations e1 st)) arg1 |
| 1259 |
= type.app_curried (expr.interp (@ident.interp) e2) arg2) |
| 1260 |
/\ (forall arg1 |
| 1261 |
(Harg11 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg1) |
| 1262 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) st arg1 = true), |
| 1263 |
abstraction_relation' |
| 1264 |
(extract assume_cast_truncates e_st st) |
| 1265 |
(type.app_curried (expr.interp (@ident.interp) (eval_with_bound relax_zrange assume_cast_truncates skip_annotations_under strip_preexisting_annotations e1 st)) arg1)). |
| 1266 |
Proof using Hrelax. |
| 1267 |
cbv [eval_with_bound]; split; |
| 1268 |
[ intros arg1 arg2 Harg12 Harg1 |
| 1269 |
| intros arg1 Harg11 Harg1 ]. |
| 1270 |
all: eapply Compilers.type.andb_bool_impl_and_for_each_lhs_of_arrow in Harg1; [ | apply ZRange.type.option.is_bounded_by_impl_related_hetero ]. |
| 1271 |
all: eapply ident.interp_eval_with_bound with (abstraction_relation':=@abstraction_relation') (abstract_domain'_R:=fun t => abstract_domain'_R t); eauto using bottom'_bottom, interp_annotate_expr with typeclass_instances. |
| 1272 |
all: intros; (eapply extract_list_state_related + eapply extract_option_state_related + eapply strip_annotation_related); eassumption. |
| 1273 |
Qed. |
| 1274 |
|
| 1275 |
Lemma interp_eta_expand_with_bound |
| 1276 |
{t} (e1 e2 : expr t) |
| 1277 |
(Hwf : expr.wf nil e1 e2) |
| 1278 |
(Ht : type.is_not_higher_order t = true) |
| 1279 |
(b_in : type.for_each_lhs_of_arrow abstract_domain t) |
| 1280 |
: forall arg1 arg2 |
| 1281 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 1282 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) b_in arg1 = true), |
| 1283 |
type.app_curried (interp (partial.eta_expand_with_bound relax_zrange e1 b_in)) arg1 = type.app_curried (interp e2) arg2. |
| 1284 |
Proof using Hrelax. |
| 1285 |
cbv [partial.eta_expand_with_bound]; intros arg1 arg2 Harg12 Harg1. |
| 1286 |
eapply Compilers.type.andb_bool_impl_and_for_each_lhs_of_arrow in Harg1. |
| 1287 |
{ apply ident.interp_eta_expand_with_bound with (abstraction_relation':=@abstraction_relation') (abstract_domain'_R:=fun t => abstract_domain'_R t); eauto using bottom'_bottom, interp_annotate_expr with typeclass_instances. |
| 1288 |
all: intros; (eapply extract_list_state_related + eapply extract_option_state_related); eassumption. } |
| 1289 |
{ apply ZRange.type.option.is_bounded_by_impl_related_hetero. } |
| 1290 |
Qed. |
| 1291 |
|
| 1292 |
Lemma Interp_EvalWithBound |
| 1293 |
{opts : AbstractInterpretation.Options} |
| 1294 |
{assume_cast_truncates : bool} |
| 1295 |
{skip_annotations_under : forall t, ident t -> bool} |
| 1296 |
{strip_preexisting_annotations : bool} |
| 1297 |
{t} (e : Expr t) |
| 1298 |
(Hwf : expr.Wf e) |
| 1299 |
(Ht : type.is_not_higher_order t = true) |
| 1300 |
(st : type.for_each_lhs_of_arrow abstract_domain t) |
| 1301 |
(Hst : Proper (type.and_for_each_lhs_of_arrow (@abstract_domain_R)) st) |
| 1302 |
: (forall arg1 arg2 |
| 1303 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 1304 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) st arg1 = true), |
| 1305 |
type.app_curried (expr.Interp (@ident.interp) (EvalWithBound relax_zrange assume_cast_truncates skip_annotations_under strip_preexisting_annotations e st)) arg1 |
| 1306 |
= type.app_curried (expr.Interp (@ident.interp) e) arg2) |
| 1307 |
/\ (forall arg1 |
| 1308 |
(Harg11 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg1) |
| 1309 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) st arg1 = true), |
| 1310 |
abstraction_relation' |
| 1311 |
(Extract assume_cast_truncates e st) |
| 1312 |
(type.app_curried (expr.Interp (@ident.interp) (EvalWithBound relax_zrange assume_cast_truncates skip_annotations_under strip_preexisting_annotations e st)) arg1)). |
| 1313 |
Proof using Hrelax. cbv [Extract EvalWithBound]; apply interp_eval_with_bound; try apply expr.Wf3_of_Wf; auto. Qed. |
| 1314 |
|
| 1315 |
Lemma Interp_EtaExpandWithBound |
| 1316 |
{t} (E : Expr t) |
| 1317 |
(Hwf : Wf E) |
| 1318 |
(Ht : type.is_not_higher_order t = true) |
| 1319 |
(b_in : type.for_each_lhs_of_arrow abstract_domain t) |
| 1320 |
: forall arg1 arg2 |
| 1321 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 1322 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) b_in arg1 = true), |
| 1323 |
type.app_curried (Interp (partial.EtaExpandWithBound relax_zrange E b_in)) arg1 = type.app_curried (Interp E) arg2. |
| 1324 |
Proof using Hrelax. cbv [partial.EtaExpandWithBound]; apply interp_eta_expand_with_bound; eauto with typeclass_instances. Qed. |
| 1325 |
End with_relax. |
| 1326 |
|
| 1327 |
Lemma strip_ranges_is_looser t b v |
| 1328 |
: @ZRange.type.option.is_bounded_by t b v = true |
| 1329 |
-> ZRange.type.option.is_bounded_by (ZRange.type.option.strip_ranges b) v = true. |
| 1330 |
Proof using Type. |
| 1331 |
induction t as [t|s IHs d IHd]; cbn in *; [ | tauto ]. |
| 1332 |
induction t; cbn in *; break_innermost_match; cbn in *; rewrite ?Bool.andb_true_iff; try solve [ intuition ]. |
| 1333 |
{ repeat match goal with ls : list _ |- _ => revert ls end. |
| 1334 |
intros ls1 ls2; revert ls2. |
| 1335 |
induction ls1, ls2; cbn in *; rewrite ?Bool.andb_true_iff; solve [ intuition ]. } |
| 1336 |
{ destruct_head' option; cbn; eauto; congruence. } |
| 1337 |
Qed. |
| 1338 |
|
| 1339 |
Lemma andb_strip_ranges_Proper t (b_in : type.for_each_lhs_of_arrow ZRange.type.option.interp t) arg1 |
| 1340 |
: type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) b_in arg1 = true -> |
| 1341 |
type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) |
| 1342 |
(type.map_for_each_lhs_of_arrow (@ZRange.type.option.strip_ranges) b_in) arg1 = true. |
| 1343 |
Proof using Type. |
| 1344 |
induction t as [t|s IHs d IHd]; cbn [type.andb_bool_for_each_lhs_of_arrow type.map_for_each_lhs_of_arrow type.for_each_lhs_of_arrow] in *; |
| 1345 |
rewrite ?Bool.andb_true_iff; [ tauto | ]. |
| 1346 |
destruct_head'_prod; cbn [fst snd]; intros [? ?]. |
| 1347 |
erewrite IHd by eauto. |
| 1348 |
split; [ | reflexivity ]. |
| 1349 |
apply strip_ranges_is_looser; assumption. |
| 1350 |
Qed. |
| 1351 |
|
| 1352 |
Lemma strip_ranges_Proper t |
| 1353 |
: Proper (abstract_domain_R ==> abstract_domain_R) (@ZRange.type.option.strip_ranges t). |
| 1354 |
Proof using Type. |
| 1355 |
induction t as [t|s IHs d IHd]; cbn in *. |
| 1356 |
all: cbv [Proper respectful abstract_domain_R] in *; intros; subst; eauto. |
| 1357 |
Qed. |
| 1358 |
|
| 1359 |
Lemma and_strip_ranges_Proper' t |
| 1360 |
: Proper (type.and_for_each_lhs_of_arrow (@abstract_domain_R) ==> type.and_for_each_lhs_of_arrow (@abstract_domain_R)) |
| 1361 |
(type.map_for_each_lhs_of_arrow (@ZRange.type.option.strip_ranges) (t:=t)). |
| 1362 |
Proof using Type. |
| 1363 |
induction t as [t|s IHs d IHd]; cbn [type.and_for_each_lhs_of_arrow type.map_for_each_lhs_of_arrow abstract_domain_R type.for_each_lhs_of_arrow] in *; |
| 1364 |
cbv [Proper respectful] in *; [ tauto | ]. |
| 1365 |
intros; destruct_head'_prod; cbn [fst snd] in *; destruct_head'_and. |
| 1366 |
split; [ | solve [ auto ] ]. |
| 1367 |
apply strip_ranges_Proper; auto. |
| 1368 |
Qed. |
| 1369 |
|
| 1370 |
Lemma interp_strip_annotations |
| 1371 |
{opts : AbstractInterpretation.Options} |
| 1372 |
{assume_cast_truncates : bool} |
| 1373 |
{t} (e_st e1 e2 : expr t) |
| 1374 |
(Hwf : expr.wf3 nil e_st e1 e2) |
| 1375 |
(Hwf' : expr.wf nil e2 e2) |
| 1376 |
(Ht : type.is_not_higher_order t = true) |
| 1377 |
(st : type.for_each_lhs_of_arrow abstract_domain t) |
| 1378 |
: (forall arg1 arg2 |
| 1379 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 1380 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) st arg1 = true), |
| 1381 |
type.app_curried (expr.interp (@ident.interp) (strip_annotations assume_cast_truncates e1 st)) arg1 |
| 1382 |
= type.app_curried (expr.interp (@ident.interp) e2) arg2) |
| 1383 |
/\ (forall arg1 |
| 1384 |
(Harg11 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg1) |
| 1385 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) st arg1 = true), |
| 1386 |
abstraction_relation' |
| 1387 |
(extract assume_cast_truncates e_st st) |
| 1388 |
(type.app_curried (expr.interp (@ident.interp) (strip_annotations assume_cast_truncates e1 st)) arg1)). |
| 1389 |
Proof using Type. |
| 1390 |
cbv [strip_annotations]; split; |
| 1391 |
[ intros arg1 arg2 Harg12 Harg1 |
| 1392 |
| intros arg1 Harg11 Harg1 ]. |
| 1393 |
all: eapply Compilers.type.andb_bool_impl_and_for_each_lhs_of_arrow in Harg1; [ | apply ZRange.type.option.is_bounded_by_impl_related_hetero ]. |
| 1394 |
all: eapply ident.interp_eval_with_bound with (abstraction_relation':=@abstraction_relation') (abstract_domain'_R:=fun t => abstract_domain'_R t); eauto using bottom'_bottom, interp_annotate_expr, default_relax_zrange_good, abstract_interp_ident_related with typeclass_instances. |
| 1395 |
all: intros; (eapply extract_list_state_related + eapply extract_option_state_related + eapply strip_annotation_related); eassumption. |
| 1396 |
Qed. |
| 1397 |
|
| 1398 |
Lemma Interp_StripAnnotations |
| 1399 |
{opts : AbstractInterpretation.Options} |
| 1400 |
{assume_cast_truncates : bool} |
| 1401 |
{t} (E : Expr t) |
| 1402 |
(Hwf : Wf E) |
| 1403 |
(Ht : type.is_not_higher_order t = true) |
| 1404 |
(b_in : type.for_each_lhs_of_arrow ZRange.type.option.interp t) |
| 1405 |
: forall arg1 arg2 |
| 1406 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 1407 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) b_in arg1 = true), |
| 1408 |
type.app_curried (expr.Interp (@ident.interp) (StripAnnotations assume_cast_truncates E b_in)) arg1 |
| 1409 |
= type.app_curried (expr.Interp (@ident.interp) E) arg2. |
| 1410 |
Proof using Type. |
| 1411 |
apply (@interp_strip_annotations _ assume_cast_truncates t (E _) (E _) (E _)); try apply expr.Wf3_of_Wf; auto. |
| 1412 |
Qed. |
| 1413 |
|
| 1414 |
Lemma Interp_StripAnnotations_bounded |
| 1415 |
{opts : AbstractInterpretation.Options} |
| 1416 |
{assume_cast_truncates : bool} |
| 1417 |
{t} (E : Expr t) |
| 1418 |
(Hwf : expr.Wf E) |
| 1419 |
(Ht : type.is_not_higher_order t = true) |
| 1420 |
(b_in : type.for_each_lhs_of_arrow ZRange.type.option.interp t) |
| 1421 |
: forall arg1 |
| 1422 |
(Harg11 : Proper (type.and_for_each_lhs_of_arrow (@type.eqv)) arg1) |
| 1423 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) b_in arg1 = true), |
| 1424 |
ZRange.type.base.option.is_bounded_by |
| 1425 |
(partial.Extract assume_cast_truncates E b_in) |
| 1426 |
(type.app_curried (expr.Interp (@ident.interp) (StripAnnotations assume_cast_truncates E b_in)) arg1) |
| 1427 |
= true. |
| 1428 |
Proof using Type. |
| 1429 |
apply (@interp_strip_annotations _ assume_cast_truncates t (E _) (E _) (E _)); try apply expr.Wf3_of_Wf; auto. |
| 1430 |
Qed. |
| 1431 |
|
| 1432 |
Lemma Interp_EtaExpandWithListInfoFromBound |
| 1433 |
{t} (E : Expr t) |
| 1434 |
(Hwf : Wf E) |
| 1435 |
(Ht : type.is_not_higher_order t = true) |
| 1436 |
(b_in : type.for_each_lhs_of_arrow abstract_domain t) |
| 1437 |
: forall arg1 arg2 |
| 1438 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 1439 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) b_in arg1 = true), |
| 1440 |
type.app_curried (Interp (partial.EtaExpandWithListInfoFromBound E b_in)) arg1 = type.app_curried (Interp E) arg2. |
| 1441 |
Proof using Type. |
| 1442 |
cbv [partial.EtaExpandWithListInfoFromBound]. |
| 1443 |
intros; apply Interp_EtaExpandWithBound; trivial. |
| 1444 |
{ exact default_relax_zrange_good. } |
| 1445 |
{ apply andb_strip_ranges_Proper; assumption. } |
| 1446 |
Qed. |
| 1447 |
End specialized. |
| 1448 |
End partial. |
| 1449 |
Import API. |
| 1450 |
|
| 1451 |
Lemma Interp_PartialEvaluateWithBounds |
| 1452 |
{opts : AbstractInterpretation.Options} |
| 1453 |
relax_zrange assume_cast_truncates skip_annotations_under strip_preexisting_annotations |
| 1454 |
(Hrelax : forall r r' z, is_tighter_than_bool z r = true |
| 1455 |
-> relax_zrange r = Some r' |
| 1456 |
-> is_tighter_than_bool z r' = true) |
| 1457 |
{t} (E : Expr t) |
| 1458 |
(Hwf : Wf E) |
| 1459 |
(Ht : type.is_not_higher_order t = true) |
| 1460 |
(b_in : type.for_each_lhs_of_arrow ZRange.type.option.interp t) |
| 1461 |
: forall arg1 arg2 |
| 1462 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 1463 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) b_in arg1 = true), |
| 1464 |
type.app_curried (expr.Interp (@ident.interp) (PartialEvaluateWithBounds relax_zrange skip_annotations_under strip_preexisting_annotations assume_cast_truncates E b_in)) arg1 |
| 1465 |
= type.app_curried (expr.Interp (@ident.interp) E) arg2. |
| 1466 |
Proof. |
| 1467 |
cbv [PartialEvaluateWithBounds]. |
| 1468 |
intros arg1 arg2 Harg12 Harg1. |
| 1469 |
assert (arg1_Proper : Proper (type.and_for_each_lhs_of_arrow (@type.related base.type base.interp (fun _ => eq))) arg1) |
| 1470 |
by (hnf; etransitivity; [ eassumption | symmetry; eassumption ]). |
| 1471 |
assert (arg2_Proper : Proper (type.and_for_each_lhs_of_arrow (@type.related base.type base.interp (fun _ => eq))) arg2) |
| 1472 |
by (hnf; etransitivity; [ symmetry; eassumption | eassumption ]). |
| 1473 |
rewrite <- (GeneralizeVar.Interp_gen1_GeneralizeVar E) by auto with wf. |
| 1474 |
eapply Interp_EvalWithBound; eauto with wf typeclass_instances. |
| 1475 |
Qed. |
| 1476 |
|
| 1477 |
Lemma Interp_PartialEvaluateWithBounds_bounded |
| 1478 |
{opts : AbstractInterpretation.Options} |
| 1479 |
relax_zrange assume_cast_truncates skip_annotations_under strip_preexisting_annotations |
| 1480 |
(Hrelax : forall r r' z, is_tighter_than_bool z r = true |
| 1481 |
-> relax_zrange r = Some r' |
| 1482 |
-> is_tighter_than_bool z r' = true) |
| 1483 |
{t} (E : Expr t) |
| 1484 |
(Hwf : Wf E) |
| 1485 |
(Ht : type.is_not_higher_order t = true) |
| 1486 |
(b_in : type.for_each_lhs_of_arrow ZRange.type.option.interp t) |
| 1487 |
: forall arg1 |
| 1488 |
(Harg11 : Proper (type.and_for_each_lhs_of_arrow (@type.eqv)) arg1) |
| 1489 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) b_in arg1 = true), |
| 1490 |
ZRange.type.base.option.is_bounded_by |
| 1491 |
(partial.Extract assume_cast_truncates E b_in) |
| 1492 |
(type.app_curried (expr.Interp (@ident.interp) (PartialEvaluateWithBounds relax_zrange assume_cast_truncates skip_annotations_under strip_preexisting_annotations E b_in)) arg1) |
| 1493 |
= true. |
| 1494 |
Proof. |
| 1495 |
cbv [PartialEvaluateWithBounds]. |
| 1496 |
intros arg1 Harg11 Harg1. |
| 1497 |
rewrite <- Extract_GeneralizeVar by auto with wf typeclass_instances. |
| 1498 |
eapply @Interp_EvalWithBound; eauto with wf typeclass_instances. |
| 1499 |
Qed. |
| 1500 |
|
| 1501 |
Lemma Interp_PartialEvaluateWithListInfoFromBounds |
| 1502 |
{opts : AbstractInterpretation.Options} |
| 1503 |
{t} (E : Expr t) |
| 1504 |
(Hwf : Wf E) |
| 1505 |
(Ht : type.is_not_higher_order t = true) |
| 1506 |
(b_in : type.for_each_lhs_of_arrow ZRange.type.option.interp t) |
| 1507 |
: forall arg1 arg2 |
| 1508 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 1509 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) b_in arg1 = true), |
| 1510 |
type.app_curried (Interp (PartialEvaluateWithListInfoFromBounds E b_in)) arg1 = type.app_curried (Interp E) arg2. |
| 1511 |
Proof. |
| 1512 |
cbv [PartialEvaluateWithListInfoFromBounds]; intros arg1 arg2 Harg12 Harg1. |
| 1513 |
assert (arg1_Proper : Proper (type.and_for_each_lhs_of_arrow (@type.related base.type base.interp (fun _ => eq))) arg1) |
| 1514 |
by (hnf; etransitivity; [ eassumption | symmetry; eassumption ]). |
| 1515 |
assert (arg2_Proper : Proper (type.and_for_each_lhs_of_arrow (@type.related base.type base.interp (fun _ => eq))) arg2) |
| 1516 |
by (hnf; etransitivity; [ symmetry; eassumption | eassumption ]). |
| 1517 |
rewrite <- (GeneralizeVar.Interp_gen1_GeneralizeVar E) by auto. |
| 1518 |
apply Interp_EtaExpandWithListInfoFromBound; eauto with wf. |
| 1519 |
Qed. |
| 1520 |
|
| 1521 |
Theorem CheckedPartialEvaluateWithBounds_Correct |
| 1522 |
{opts : AbstractInterpretation.Options} |
| 1523 |
(relax_zrange : zrange -> option zrange) |
| 1524 |
(assume_cast_truncates : bool) |
| 1525 |
(skip_annotations_under : forall t, ident t -> bool) |
| 1526 |
(strip_preexisting_annotations : bool) |
| 1527 |
(Hrelax : forall r r' z, is_tighter_than_bool z r = true |
| 1528 |
-> relax_zrange r = Some r' |
| 1529 |
-> is_tighter_than_bool z r' = true) |
| 1530 |
{t} (E : Expr t) |
| 1531 |
(Hwf : Wf E) |
| 1532 |
(Ht : type.is_not_higher_order t = true) |
| 1533 |
(b_in : type.for_each_lhs_of_arrow ZRange.type.option.interp t) |
| 1534 |
(b_out : ZRange.type.base.option.interp (type.final_codomain t)) |
| 1535 |
rv (Hrv : CheckedPartialEvaluateWithBounds relax_zrange assume_cast_truncates skip_annotations_under strip_preexisting_annotations E b_in b_out = inl rv) |
| 1536 |
: (forall arg1 arg2 |
| 1537 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 1538 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) b_in arg1 = true), |
| 1539 |
ZRange.type.base.option.is_bounded_by b_out (type.app_curried (Interp rv) arg1) = true |
| 1540 |
/\ type.app_curried (Interp rv) arg1 = type.app_curried (Interp E) arg2) |
| 1541 |
/\ Wf rv. |
| 1542 |
Proof. |
| 1543 |
cbv [CheckedPartialEvaluateWithBounds Let_In] in *; |
| 1544 |
break_innermost_match_hyps; inversion_sum; subst. |
| 1545 |
all: split; |
| 1546 |
[ intros arg1 arg2 Harg12 Harg1; |
| 1547 |
assert (arg1_Proper : Proper (type.and_for_each_lhs_of_arrow (@type.related base.type base.interp (fun _ => eq))) arg1) |
| 1548 |
by (hnf; etransitivity; [ eassumption | symmetry; eassumption ]); |
| 1549 |
split; |
| 1550 |
repeat first [ rewrite [email protected]_gen1_FromFlat_ToFlat by eauto with wf typeclass_instances |
| 1551 |
| rewrite <- Extract_FromFlat_ToFlat by auto with typeclass_instances; apply Interp_PartialEvaluateWithBounds_bounded; auto |
| 1552 |
| rewrite Extract_FromFlat_ToFlat by auto with wf typeclass_instances |
| 1553 |
| progress intros |
| 1554 |
| eapply ZRange.type.base.option.is_tighter_than_is_bounded_by; [ eassumption | ] |
| 1555 |
| solve [ eauto with wf typeclass_instances ] |
| 1556 |
| erewrite !Interp_PartialEvaluateWithBounds |
| 1557 |
| apply type.app_curried_Proper |
| 1558 |
| apply expr.Wf_Interp_Proper_gen |
| 1559 |
| progress intros ] |
| 1560 |
| eauto with wf typeclass_instances ]. |
| 1561 |
Qed. |
| 1562 |
End Compilers. |