| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import Lia. |
| 4 |
Require Import bedrock2.Syntax. |
| 5 |
Require Import bedrock2.WeakestPreconditionProperties. |
| 6 |
Require Import coqutil.Word.Interface coqutil.Word.Properties. |
| 7 |
Require Import coqutil.Map.Interface. |
| 8 |
Require Import Crypto.CastLemmas. |
| 9 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 10 |
Require Import Crypto.Bedrock.Field.Common.Tactics. |
| 11 |
Require Import Crypto.Bedrock.Field.Common.Util. |
| 12 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.Equivalence. |
| 13 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.EquivalenceProperties. |
| 14 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.UsedVarnames. |
| 15 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.VarnameSet. |
| 16 |
Require Import Crypto.Bedrock.Field.Translation.Expr. |
| 17 |
Require Import Crypto.Language.API. |
| 18 |
Require Import Rewriter.Util.Bool.Reflect. |
| 19 |
Require Import Crypto.Util.ZRange. |
| 20 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 21 |
Require Import Crypto.Util.ZUtil.LandLorShiftBounds. |
| 22 |
Require Import Crypto.Util.ZUtil.LnotModulo. |
| 23 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 24 |
Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall. |
| 25 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 26 |
Local Open Scope Z_scope. |
| 27 |
|
| 28 |
Import API.Compilers. |
| 29 |
Import Wf.Compilers.expr. |
| 30 |
Import Types.Notations. |
| 31 |
|
| 32 |
Section Expr. |
| 33 |
Context |
| 34 |
{width BW word mem locals ext_spec varname_gen error} |
| 35 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}. |
| 36 |
Context {ok : ok}. |
| 37 |
|
| 38 |
Local Existing Instance Types.rep.Z. |
| 39 |
Local Existing Instance Types.rep.listZ_local. |
| 40 |
|
| 41 |
Inductive valid_expr |
| 42 |
: forall {t}, |
| 43 |
bool (* require_casts *) -> |
| 44 |
@API.expr (fun _ => unit) t -> Prop := |
| 45 |
| valid_cast1 : |
| 46 |
forall rc r x, |
| 47 |
valid_expr false x -> |
| 48 |
range_good (width:=width) r = true -> |
| 49 |
valid_expr rc |
| 50 |
(expr.App |
| 51 |
(expr.App (expr.Ident ident.Z_cast) |
| 52 |
(expr.Ident (ident.Literal (t:=base.type.zrange) r))) x) |
| 53 |
| valid_cast2 : |
| 54 |
forall (rc : bool) r1 r2 x, |
| 55 |
valid_expr false x -> |
| 56 |
range_good (width:=width) r1 = true -> |
| 57 |
range_good (width:=width) r2 = true -> |
| 58 |
valid_expr rc |
| 59 |
(expr.App |
| 60 |
(expr.App (expr.Ident ident.Z_cast2) |
| 61 |
(expr.App |
| 62 |
(expr.App |
| 63 |
(expr.Ident ident.pair) |
| 64 |
(expr.Ident (ident.Literal (t:=base.type.zrange) r1))) |
| 65 |
(expr.Ident (ident.Literal (t:=base.type.zrange) r2)))) x) |
| 66 |
| valid_fst : |
| 67 |
forall (x : API.expr type_ZZ), |
| 68 |
valid_expr true x -> |
| 69 |
valid_expr false (expr.App (expr.Ident ident.fst) x) |
| 70 |
| valid_snd : |
| 71 |
forall (x : API.expr type_ZZ), |
| 72 |
valid_expr true x -> |
| 73 |
valid_expr false (expr.App (expr.Ident ident.snd) x) |
| 74 |
| valid_literalz : |
| 75 |
forall rc z, |
| 76 |
(is_bounded_by_bool z (max_range(width:=width)) || negb rc)%bool = true -> |
| 77 |
valid_expr rc (expr.Ident (ident.Literal (t:=base.type.Z) z)) |
| 78 |
| valid_literalnat : |
| 79 |
forall n, |
| 80 |
valid_expr false (expr.Ident (ident.Literal (t:=base.type.nat) n)) |
| 81 |
| valid_var_z : |
| 82 |
forall rc v, valid_expr (t:=type_Z) rc (expr.Var v) |
| 83 |
| valid_var_listz : |
| 84 |
forall rc v, valid_expr (t:=type_listZ) rc (expr.Var v) |
| 85 |
| valid_nth_default : |
| 86 |
forall d l i, |
| 87 |
(* only accepting literal d, because it makes things easier for |
| 88 |
computable condition *) |
| 89 |
0 <= d < 2 ^ width -> |
| 90 |
valid_expr |
| 91 |
false |
| 92 |
(expr.App |
| 93 |
(expr.App |
| 94 |
(expr.App |
| 95 |
(expr.Ident |
| 96 |
(ident.List_nth_default (t:=base_Z))) |
| 97 |
(expr.Ident (ident.Literal (t:=base.type.Z) d))) |
| 98 |
(expr.Var l)) |
| 99 |
(expr.Ident (ident.Literal (t:=base.type.nat) i))) |
| 100 |
| valid_shiftr : |
| 101 |
forall (x : API.expr type_Z) n, |
| 102 |
valid_expr true x -> |
| 103 |
0 <= n < width -> |
| 104 |
valid_expr (t:=type_Z) false |
| 105 |
(expr.App (expr.App (expr.Ident ident.Z_shiftr) x) |
| 106 |
(expr.Ident (ident.Literal (t:=base.type.Z) n))) |
| 107 |
| valid_shiftl : |
| 108 |
forall (x : API.expr type_Z) n, |
| 109 |
valid_expr true x -> |
| 110 |
0 <= n < width -> |
| 111 |
valid_expr (t:=type_Z) false |
| 112 |
(expr.App (expr.App (expr.Ident ident.Z_shiftl) x) |
| 113 |
(expr.Ident (ident.Literal (t:=base.type.Z) n))) |
| 114 |
| valid_mul_high : |
| 115 |
forall (s : Z) (x y : API.expr type_Z), |
| 116 |
s = 2 ^ width -> |
| 117 |
valid_expr true x -> |
| 118 |
valid_expr true y -> |
| 119 |
valid_expr (t:=type_Z) false |
| 120 |
(expr.App (expr.App |
| 121 |
(expr.App (expr.Ident ident.Z_mul_high) |
| 122 |
(expr.Ident (ident.Literal (t:=base.type.Z) s))) |
| 123 |
x) y) |
| 124 |
| valid_truncating_shiftl : |
| 125 |
forall (s n : Z) (x : API.expr type_Z), |
| 126 |
s = width -> |
| 127 |
0 <= n < width -> |
| 128 |
valid_expr true x -> |
| 129 |
valid_expr (t:=type_Z) false |
| 130 |
(expr.App (expr.App |
| 131 |
(expr.App (expr.Ident ident.Z_truncating_shiftl) |
| 132 |
(expr.Ident (ident.Literal (t:=base.type.Z) s))) |
| 133 |
x) (expr.Ident (ident.Literal (t:=base.type.Z) n))) |
| 134 |
| valid_lnot_modulo : |
| 135 |
forall (x : API.expr type_Z) (m : Z), |
| 136 |
is_bounded_by_bool (m-1) (max_range(width:=width)) = true -> |
| 137 |
2 ^ Z.log2 m = m -> |
| 138 |
valid_expr true x -> |
| 139 |
valid_expr (t:=type_Z) false |
| 140 |
(expr.App (expr.App (expr.Ident ident.Z_lnot_modulo) |
| 141 |
x) |
| 142 |
(expr.Ident (ident.Literal (t:=base.type.Z) m))) |
| 143 |
| valid_zselect : |
| 144 |
forall (c : API.expr type_Z) (x y : Z), |
| 145 |
x = 0 -> |
| 146 |
y = 2^width-1 -> |
| 147 |
valid_expr true c -> |
| 148 |
valid_expr (t:=type_Z) false |
| 149 |
(expr.App (expr.App |
| 150 |
(expr.App (expr.Ident ident.Z_zselect) c) |
| 151 |
(expr.Ident (ident.Literal (t:=base.type.Z) x))) |
| 152 |
(expr.Ident (ident.Literal (t:=base.type.Z) y))) |
| 153 |
| valid_opp : |
| 154 |
forall (x : API.expr type_Z), |
| 155 |
valid_expr true x -> |
| 156 |
valid_expr (t:=type_Z) false |
| 157 |
(expr.App (expr.Ident ident.Z_opp) x) |
| 158 |
| valid_binop : |
| 159 |
forall i (x y : API.expr type_Z), |
| 160 |
translate_binop i <> None -> |
| 161 |
valid_expr (t:=type_Z) true x -> |
| 162 |
valid_expr (t:=type_Z) true y -> |
| 163 |
valid_expr (t:=type_Z) false (expr.App (expr.App (expr.Ident i) x) y) |
| 164 |
. |
| 165 |
|
| 166 |
(* version generalized to any t, necessary to destruct i *) |
| 167 |
Lemma translate_binop_correct' {t} : |
| 168 |
forall (i : ident.ident t) |
| 169 |
(xargs : type.for_each_lhs_of_arrow API.interp_type t) |
| 170 |
(yargs : type.for_each_lhs_of_arrow rtype t) |
| 171 |
op locals, |
| 172 |
translate_binop i = Some op -> |
| 173 |
locally_equivalent_args xargs yargs locals -> |
| 174 |
(match t as t0 |
| 175 |
return |
| 176 |
rtype t0 -> |
| 177 |
API.interp_type t0 -> |
| 178 |
type.for_each_lhs_of_arrow API.interp_type t0 -> |
| 179 |
type.for_each_lhs_of_arrow rtype t0 -> |
| 180 |
Prop with |
| 181 |
| (type_Z -> type_Z -> type_Z)%etype => |
| 182 |
fun op (f : Z -> Z -> Z) (x : Z * (Z * unit)) |
| 183 |
(y : (Syntax.expr * (Syntax.expr * unit))) => |
| 184 |
let x1 := fst x in |
| 185 |
let x2 := fst (snd x) in |
| 186 |
let y1 := fst y in |
| 187 |
let y2 := fst (snd y) in |
| 188 |
WeakestPrecondition.dexpr |
| 189 |
map.empty locals (op y1 y2) |
| 190 |
(word.of_Z (f x1 x2)) |
| 191 |
| _ => fun _ _ _ _ => False |
| 192 |
end) op (Compilers.ident_interp i) xargs yargs. |
| 193 |
Proof. |
| 194 |
cbv [translate_binop]; intros *. |
| 195 |
break_match; try congruence; intros. |
| 196 |
all:repeat match goal with |
| 197 |
| _ => progress sepsimpl |
| 198 |
| _ => progress |
| 199 |
cbn [fst snd |
| 200 |
Compilers.ident_interp |
| 201 |
type.app_curried |
| 202 |
type.final_codomain |
| 203 |
equivalent_base |
| 204 |
rep.equiv rep.Z |
| 205 |
equivalent_args |
| 206 |
locally_equivalent_args |
| 207 |
Semantics.interp_binop] in * |
| 208 |
| _ => progress |
| 209 |
cbn [Language.Compilers.base.interp |
| 210 |
Compilers.type.interp |
| 211 |
Compilers.base_interp] in * |
| 212 |
| _ => progress |
| 213 |
(cbv [WeakestPrecondition.dexpr] in *; |
| 214 |
cbn [WeakestPrecondition.expr WeakestPrecondition.expr_body]) |
| 215 |
| H : Some _ = Some _ |- _ => inversion H; subst; clear H |
| 216 |
| H : word.unsigned _ = _ |- _ => rewrite <-H |
| 217 |
| _ => break_match; [ ]; cbn [fst snd] |
| 218 |
end. |
| 219 |
all: (do 2 (eapply Proper_expr; [ | eassumption]; repeat intro; subst)). |
| 220 |
(* solves ring operations *) |
| 221 |
all:autorewrite with rew_word_morphism. |
| 222 |
all:rewrite ?word.of_Z_unsigned; try reflexivity. |
| 223 |
|
| 224 |
(* solves and/or/xor *) |
| 225 |
all:apply word.unsigned_inj; rewrite ?word.unsigned_of_Z. |
| 226 |
all:first [ rewrite word.unsigned_and |
| 227 |
| rewrite word.unsigned_ltu |
| 228 |
| rewrite word.unsigned_xor |
| 229 |
| rewrite word.unsigned_or ]. |
| 230 |
all: try reflexivity. |
| 231 |
|
| 232 |
(* last goal: ltu *) |
| 233 |
{ cbv [Definitions.Z.ltz]. |
| 234 |
rewrite Bool.pull_bool_if, word.unsigned_of_Z. |
| 235 |
reflexivity. } |
| 236 |
Qed. |
| 237 |
|
| 238 |
Lemma translate_binop_correct |
| 239 |
(i : ident.ident (type_Z -> type_Z -> type_Z)) |
| 240 |
(x1 x2 : base.interp base_Z) |
| 241 |
(y1 y2 : Syntax.expr) |
| 242 |
op l : |
| 243 |
translate_binop i = Some op -> |
| 244 |
locally_equivalent (t:=type_Z) x1 y1 l -> |
| 245 |
locally_equivalent (t:=type_Z) x2 y2 l -> |
| 246 |
WeakestPrecondition.dexpr |
| 247 |
map.empty l |
| 248 |
(op y1 y2) (word.of_Z (Compilers.ident_interp i x1 x2)). |
| 249 |
Proof. |
| 250 |
intros. |
| 251 |
apply (translate_binop_correct' |
| 252 |
i (x1, (x2, tt)) (y1, (y2, tt))); |
| 253 |
eauto; [ ]. |
| 254 |
cbv [locally_equivalent] in *. |
| 255 |
cbn [locally_equivalent_args equivalent_args fst snd]. |
| 256 |
repeat lazymatch goal with |
| 257 |
| |- exists _, _ => exists (Separation.emp True) |
| 258 |
| |- _ /\ _ => split |
| 259 |
| |- True => tauto |
| 260 |
| _ => solve [sepsimpl; eauto] |
| 261 |
end. |
| 262 |
Qed. |
| 263 |
|
| 264 |
Fixpoint locally_equivalent_nobounds_base {t} |
| 265 |
: base.interp t -> base_rtype t -> locals -> Prop := |
| 266 |
match t as t0 return |
| 267 |
base.interp t0 -> |
| 268 |
base_rtype t0 -> |
| 269 |
locals -> Prop with |
| 270 |
| base.type.prod a b => |
| 271 |
fun x y locals => |
| 272 |
locally_equivalent_nobounds_base (fst x) (fst y) locals /\ |
| 273 |
locally_equivalent_nobounds_base (snd x) (snd y) locals |
| 274 |
| base_Z => |
| 275 |
fun x y locals => |
| 276 |
WeakestPrecondition.dexpr map.empty locals y (word.of_Z x) |
| 277 |
| base_nat => |
| 278 |
fun x y locals => |
| 279 |
WeakestPrecondition.dexpr map.empty locals y (word.of_Z (Z.of_nat x)) |
| 280 |
| base_listZ => |
| 281 |
(* we never assign to lists, so they get a pass *) |
| 282 |
fun _ _ _ => True |
| 283 |
| _ => fun _ _ _ => False |
| 284 |
end. |
| 285 |
Definition locally_equivalent_nobounds {t} |
| 286 |
: API.interp_type t -> rtype t -> locals -> Prop := |
| 287 |
match t with |
| 288 |
| type.base b => locally_equivalent_nobounds_base |
| 289 |
| type.arrow _ _ => fun _ _ _ => False |
| 290 |
end. |
| 291 |
|
| 292 |
Lemma locally_equivalent_nobounds_impl {t} : |
| 293 |
forall x y locals, |
| 294 |
locally_equivalent (t:=type.base t) x y locals -> |
| 295 |
locally_equivalent_nobounds x y locals. |
| 296 |
Proof. |
| 297 |
cbv [locally_equivalent equivalent locally_equivalent_nobounds]. |
| 298 |
induction t; |
| 299 |
cbn [equivalent_base |
| 300 |
locally_equivalent |
| 301 |
locally_equivalent_nobounds_base |
| 302 |
rep.equiv rep.Z rep.listZ_local]; |
| 303 |
break_match; intros; sepsimpl; subst; |
| 304 |
rewrite ?word.of_Z_unsigned; eauto. |
| 305 |
Qed. |
| 306 |
|
| 307 |
Lemma require_cast_for_arg_binop {var t} : |
| 308 |
forall i : ident.ident t, |
| 309 |
translate_binop i <> None -> |
| 310 |
require_cast_for_arg (width:=width) (var:=var) (expr.Ident i) = true. |
| 311 |
Proof. |
| 312 |
destruct i; |
| 313 |
cbn [translate_binop require_cast_for_arg]; |
| 314 |
congruence. |
| 315 |
Qed. |
| 316 |
|
| 317 |
Lemma require_cast_for_arg_binop2 {var s d} : |
| 318 |
forall (i : ident.ident (s -> d)) x, |
| 319 |
translate_binop i <> None -> |
| 320 |
require_cast_for_arg (width:=width) (var:=var) (expr.App (expr.Ident i) x) = true. |
| 321 |
Proof. |
| 322 |
(* destruct is too weak *) |
| 323 |
intro i. |
| 324 |
refine match i with |
| 325 |
| ident.Literal _ _ => _ |
| 326 |
| _ => _ |
| 327 |
end; try exact idProp; try reflexivity; |
| 328 |
cbn [translate_binop]; congruence. |
| 329 |
Qed. |
| 330 |
|
| 331 |
Lemma translate_ident_binop {t} : |
| 332 |
forall i : ident.ident t, |
| 333 |
translate_binop i <> None -> |
| 334 |
translate_binop i = Some (translate_ident i). |
| 335 |
Proof. |
| 336 |
destruct i; |
| 337 |
cbn [translate_binop]; try congruence. |
| 338 |
all:cbn [translate_ident translate_binop]. |
| 339 |
all:reflexivity. |
| 340 |
Qed. |
| 341 |
|
| 342 |
Lemma translate_binop_cast_exempt {var t} : |
| 343 |
forall i : ident.ident t, |
| 344 |
translate_binop i <> None -> |
| 345 |
cast_exempt (var:=var) (expr.Ident i) = false. |
| 346 |
Proof. |
| 347 |
destruct i; |
| 348 |
cbn [translate_binop cast_exempt]; |
| 349 |
congruence. |
| 350 |
Qed. |
| 351 |
|
| 352 |
Lemma is_bounded_by_bool_max_range n : |
| 353 |
0 <= n < 2 ^ width -> |
| 354 |
is_bounded_by_bool n (max_range(width:=width)) = true. |
| 355 |
Proof. |
| 356 |
intros; cbv [is_bounded_by_bool upper lower max_range]. |
| 357 |
apply Bool.andb_true_iff; split; apply Z.leb_le; lia. |
| 358 |
Qed. |
| 359 |
|
| 360 |
Lemma is_bounded_by_bool_width_range n : |
| 361 |
0 <= n < width -> |
| 362 |
is_bounded_by_bool n (@width_range width) = true. |
| 363 |
Proof. |
| 364 |
intros; cbv [is_bounded_by_bool upper lower width_range]. |
| 365 |
apply Bool.andb_true_iff; split; apply Z.leb_le; lia. |
| 366 |
Qed. |
| 367 |
|
| 368 |
(* useful fact to say anything in width_range is also in max_range *) |
| 369 |
Lemma width_lt_pow2width : width < 2 ^ width. |
| 370 |
Proof. pose proof word.width_pos. apply Z.pow_gt_lin_r; lia. Qed. |
| 371 |
|
| 372 |
Lemma pow2width_pos : 0 < 2 ^ width. |
| 373 |
Proof. |
| 374 |
pose proof word.width_pos. |
| 375 |
apply Z.pow_pos_nonneg; lia. |
| 376 |
Qed. |
| 377 |
|
| 378 |
(** TODO: Find a better place for this *) |
| 379 |
Hint Rewrite word.testbit_wrap : Ztestbit_full. |
| 380 |
Lemma translate_expr_correct' {t} |
| 381 |
(* three exprs, representing the same Expr with different vars *) |
| 382 |
(e1 : @API.expr (fun _ => unit) (type.base t)) |
| 383 |
(e2 : @API.expr API.interp_type (type.base t)) |
| 384 |
(e3 : @API.expr ltype (type.base t)) |
| 385 |
(require_cast : bool) : |
| 386 |
(* e1 is a valid input to translate_carries_correct *) |
| 387 |
valid_expr require_cast e1 -> |
| 388 |
forall G locals, |
| 389 |
wf3 G e1 e2 e3 -> |
| 390 |
let out := translate_expr require_cast e3 in |
| 391 |
context_equiv G locals -> |
| 392 |
if require_cast |
| 393 |
then locally_equivalent (API.interp e2) out locals |
| 394 |
else locally_equivalent_nobounds (API.interp e2) out locals. |
| 395 |
Proof. |
| 396 |
cbv zeta. |
| 397 |
pose proof width_lt_pow2width. |
| 398 |
pose proof pow2width_pos. |
| 399 |
|
| 400 |
induction 1; inversion 1; cleanup_wf; intros. |
| 401 |
|
| 402 |
all:repeat match goal with |
| 403 |
| _ => progress cleanup_wf |
| 404 |
| H : wf3 _ ?x ?y _ |- _ => |
| 405 |
(* for specific cases, repeatedly do inversion *) |
| 406 |
progress match y with |
| 407 |
| expr.App _ _ => idtac (* don't invert original, already-inverted hypothesis *) |
| 408 |
| _ => match x with |
| 409 |
| context [expr.Ident] => |
| 410 |
inversion H; clear H |
| 411 |
| context [expr.Var] => |
| 412 |
inversion H; clear H |
| 413 |
end |
| 414 |
end |
| 415 |
end. |
| 416 |
|
| 417 |
all:cbv [locally_equivalent equivalent]. |
| 418 |
all:cbn [locally_equivalent_nobounds] in *. |
| 419 |
all:cbn [expr.interp Compilers.ident_interp]. |
| 420 |
all:cbn [translate_expr is_cast is_cast_literal_ident |
| 421 |
is_cast_literal is_cast2_literal |
| 422 |
is_cast2_literal_App1 |
| 423 |
is_cast2_literal_App2 |
| 424 |
is_cast_ident is_cast_ident_expr |
| 425 |
is_pair_range negb andb]. |
| 426 |
all:rewrite ?require_cast_for_arg_binop by auto. |
| 427 |
all:rewrite ?require_cast_for_arg_binop2 by auto. |
| 428 |
all:rewrite ?translate_binop_cast_exempt by auto. |
| 429 |
all:cbn [require_cast_for_arg cast_exempt |
| 430 |
translate_cast_exempt]. |
| 431 |
all:repeat match goal with |
| 432 |
| H : range_good _ = _ |- _ => |
| 433 |
rewrite H |
| 434 |
| H : is_bounded_by_bool _ _ = true |- _ => |
| 435 |
rewrite H |
| 436 |
| _ => rewrite is_bounded_by_bool_width_range by lia |
| 437 |
| _ => rewrite is_bounded_by_bool_max_range by lia |
| 438 |
end. |
| 439 |
all:cbn [negb andb]; |
| 440 |
rewrite ?Bool.andb_false_r, ?Bool.andb_true_r, ?Bool.if_const. |
| 441 |
|
| 442 |
(* solve the binop goal first (nasty when translate_ident unfolded) *) |
| 443 |
all: try match goal with |
| 444 |
| H : translate_binop _ <> None |- _ => |
| 445 |
apply translate_binop_correct; |
| 446 |
eauto using translate_ident_binop end. |
| 447 |
|
| 448 |
all:cbn [translate_ident translate_binop]. |
| 449 |
|
| 450 |
{ (* cast1 *) |
| 451 |
specialize (IHvalid_expr _ _ _ _ |
| 452 |
ltac:(eassumption) ltac:(eassumption)). |
| 453 |
cbn [locally_equivalent equivalent_base rep.equiv rep.Z |
| 454 |
locally_equivalent_nobounds_base] in *. |
| 455 |
cbv [range_good max_range ident.literal] in *. |
| 456 |
intros; progress reflect_beq_to_eq zrange_beq; subst. |
| 457 |
rewrite ident.cast_out_of_bounds_simple_0_mod by lia. |
| 458 |
cleanup. |
| 459 |
rewrite Z.sub_simpl_r. |
| 460 |
erewrite word.of_Z_inj_mod |
| 461 |
by (rewrite Z.mod_mod by lia; reflexivity). |
| 462 |
destruct rc; try eexists; sepsimpl; |
| 463 |
try apply Z.mod_pos_bound; try lia; |
| 464 |
eauto; [ ]. |
| 465 |
rewrite word.unsigned_of_Z. reflexivity. } |
| 466 |
{ (* cast2 *) |
| 467 |
specialize (IHvalid_expr _ _ _ _ |
| 468 |
ltac:(eassumption) ltac:(eassumption)). |
| 469 |
cbv [range_good max_range ident.literal ident.cast2] in *. |
| 470 |
cbn [locally_equivalent equivalent_base rep.equiv rep.Z fst snd |
| 471 |
locally_equivalent_nobounds_base] in *. |
| 472 |
cbn [Compilers.base_interp] in *. |
| 473 |
intros; progress reflect_beq_to_eq zrange_beq; subst. |
| 474 |
rewrite !ident.cast_out_of_bounds_simple_0_mod by lia. |
| 475 |
rewrite Z.sub_simpl_r. |
| 476 |
destruct rc; repeat match goal with |
| 477 |
| _ => progress sepsimpl |
| 478 |
| _ => rewrite word.unsigned_of_Z |
| 479 |
| _ => eassumption |
| 480 |
| _ => eexists |
| 481 |
| _ => |
| 482 |
erewrite word.of_Z_inj_mod |
| 483 |
by (rewrite Z.mod_mod by lia; reflexivity); |
| 484 |
solve [eauto] |
| 485 |
end. } |
| 486 |
{ (* fst *) |
| 487 |
specialize (IHvalid_expr _ _ _ _ |
| 488 |
ltac:(eassumption) ltac:(eassumption)). |
| 489 |
cbn [locally_equivalent equivalent] in *. |
| 490 |
cbn [locally_equivalent_nobounds_base |
| 491 |
locally_equivalent_nobounds |
| 492 |
equivalent_base rep.equiv rep.Z] in *. |
| 493 |
sepsimpl; eauto. |
| 494 |
match goal with |
| 495 |
| H : word.unsigned _ = _ |- _ => rewrite <-H |
| 496 |
end. |
| 497 |
rewrite word.of_Z_unsigned. auto. } |
| 498 |
{ (* snd *) |
| 499 |
specialize (IHvalid_expr _ _ _ _ |
| 500 |
ltac:(eassumption) ltac:(eassumption)). |
| 501 |
cbn [locally_equivalent equivalent] in *. |
| 502 |
cbn [locally_equivalent_nobounds_base |
| 503 |
locally_equivalent_nobounds |
| 504 |
equivalent_base rep.equiv rep.Z] in *. |
| 505 |
sepsimpl; eauto. |
| 506 |
match goal with |
| 507 |
| H : word.unsigned _ = _ |- _ => rewrite <-H |
| 508 |
end. |
| 509 |
rewrite word.of_Z_unsigned. auto. } |
| 510 |
{ (* literal Z *) |
| 511 |
cbn [locally_equivalent_nobounds_base |
| 512 |
locally_equivalent equivalent_base rep.equiv rep.Z]. |
| 513 |
cbv [ident.literal] in *. |
| 514 |
repeat match goal with |
| 515 |
| H : (_ || _)%bool = true |- _ => |
| 516 |
apply Bool.orb_true_iff in H; destruct H |
| 517 |
| H : is_bounded_by_bool _ _ = true |- _ => |
| 518 |
rewrite H |
| 519 |
| H : is_bounded_by_bool _ _ = true |- _ => |
| 520 |
cbv [is_bounded_by_bool upper lower max_range] in H; |
| 521 |
apply Bool.andb_true_iff in H; cleanup |
| 522 |
end; Z.ltb_to_lt. |
| 523 |
all:destruct rc; cbn [negb] in *; try congruence. |
| 524 |
all:eexists; sepsimpl; try reflexivity. |
| 525 |
all:rewrite ?word.unsigned_of_Z; cbv [word.wrap]. |
| 526 |
all:Z.rewrite_mod_small; try reflexivity. } |
| 527 |
{ (* literal nat *) |
| 528 |
reflexivity. } |
| 529 |
{ (* var (Z) *) |
| 530 |
match goal with |
| 531 |
| H : context_equiv _ _ |- _ => |
| 532 |
cbv [context_equiv] in H; |
| 533 |
rewrite Forall_forall in H; |
| 534 |
specialize (H _ ltac:(eassumption)) |
| 535 |
end. |
| 536 |
cbv [equiv3 locally_equivalent] in *. |
| 537 |
destruct rc; |
| 538 |
try apply locally_equivalent_nobounds_impl; |
| 539 |
eauto. } |
| 540 |
{ (* var (list Z) *) |
| 541 |
match goal with |
| 542 |
| H : context_equiv _ _ |- _ => |
| 543 |
cbv [context_equiv] in H; |
| 544 |
rewrite Forall_forall in H; |
| 545 |
specialize (H _ ltac:(eassumption)) |
| 546 |
end. |
| 547 |
cbv [equiv3 locally_equivalent] in *. |
| 548 |
destruct rc; |
| 549 |
try apply locally_equivalent_nobounds_impl; |
| 550 |
eauto. } |
| 551 |
{ (* nth_default *) |
| 552 |
cbv [ident.literal rnth_default]. rewrite Nat2Z.id. |
| 553 |
apply locally_equivalent_nobounds_impl. |
| 554 |
match goal with |
| 555 |
|- locally_equivalent |
| 556 |
(nth_default ?d1 ?l1 ?i) (nth_default ?d2 ?l2 ?i) ?locals => |
| 557 |
let R := constr:(fun x y => |
| 558 |
locally_equivalent (t:=type_Z) x y locals) in |
| 559 |
assert (length l1 = length l2 /\ |
| 560 |
(forall i, |
| 561 |
R (nth_default d1 l1 i) (nth_default d2 l2 i))); |
| 562 |
[ apply (@Forall.Forall2_forall_iff'' _ _ R) |
| 563 |
| cleanup; solve [eauto] ] |
| 564 |
end. |
| 565 |
split. |
| 566 |
{ match goal with |
| 567 |
| H : context_equiv _ _ |- _ => |
| 568 |
cbv [context_equiv] in H; |
| 569 |
rewrite Forall_forall in H; |
| 570 |
specialize (H _ ltac:(eassumption)) |
| 571 |
end. |
| 572 |
eauto. } |
| 573 |
{ cbn [locally_equivalent_nobounds |
| 574 |
locally_equivalent_nobounds_base |
| 575 |
locally_equivalent equivalent_base |
| 576 |
equivalent rep.equiv rep.Z ident.literal] in *. |
| 577 |
eexists; sepsimpl; try reflexivity; [ ]. |
| 578 |
rewrite word.unsigned_of_Z. cbv [word.wrap]. |
| 579 |
Z.rewrite_mod_small; reflexivity. } } |
| 580 |
{ (* shiftr *) |
| 581 |
specialize (IHvalid_expr _ _ _ _ |
| 582 |
ltac:(eassumption) ltac:(eassumption)). |
| 583 |
cbv [rshiftr literal_ltwidth invert_literal]. |
| 584 |
rewrite is_bounded_by_bool_width_range by lia. |
| 585 |
cbn [locally_equivalent_nobounds |
| 586 |
locally_equivalent_nobounds_base |
| 587 |
locally_equivalent equivalent |
| 588 |
equivalent_base rep.equiv rep.Z ident.literal] in *. |
| 589 |
cbv [WeakestPrecondition.dexpr ident.literal] in *. |
| 590 |
cbn [WeakestPrecondition.expr WeakestPrecondition.expr_body |
| 591 |
Semantics.interp_binop]. |
| 592 |
sepsimpl_hyps. |
| 593 |
eapply Proper_expr; [ | eassumption ]. |
| 594 |
repeat intro; subst. |
| 595 |
cbv [WeakestPrecondition.literal dlet.dlet ident.literal]. |
| 596 |
apply word.unsigned_inj. |
| 597 |
rewrite word.unsigned_sru, !word.unsigned_of_Z |
| 598 |
by (rewrite word.unsigned_of_Z; cbv [word.wrap]; |
| 599 |
Z.rewrite_mod_small; lia). |
| 600 |
cbv [word.wrap]; Z.rewrite_mod_small. |
| 601 |
congruence. } |
| 602 |
{ (* shiftl *) |
| 603 |
specialize (IHvalid_expr _ _ _ _ |
| 604 |
ltac:(eassumption) ltac:(eassumption)). |
| 605 |
cbv [rshiftl literal_ltwidth invert_literal]. |
| 606 |
rewrite is_bounded_by_bool_width_range by lia. |
| 607 |
cbn [locally_equivalent_nobounds |
| 608 |
locally_equivalent_nobounds_base |
| 609 |
locally_equivalent equivalent |
| 610 |
equivalent_base rep.equiv rep.Z ident.literal] in *. |
| 611 |
cbv [WeakestPrecondition.dexpr ident.literal] in *. |
| 612 |
cbn [WeakestPrecondition.expr WeakestPrecondition.expr_body |
| 613 |
Semantics.interp_binop]. |
| 614 |
sepsimpl; [ lia .. | ]. |
| 615 |
eapply Proper_expr; [ | eassumption ]. |
| 616 |
repeat intro; subst. |
| 617 |
cbv [WeakestPrecondition.literal dlet.dlet ident.literal]. |
| 618 |
apply word.unsigned_inj. |
| 619 |
rewrite word.unsigned_slu, !word.unsigned_of_Z |
| 620 |
by (rewrite word.unsigned_of_Z; cbv [word.wrap]; |
| 621 |
Z.rewrite_mod_small; lia). |
| 622 |
cbv [word.wrap]. Z.rewrite_mod_small. |
| 623 |
congruence. } |
| 624 |
{ (* mul_high *) |
| 625 |
cbv [ident.literal rmul_high literal_eqb invert_literal]. |
| 626 |
rewrite Z.eqb_refl. |
| 627 |
match goal with |
| 628 |
|- context [_ (translate_expr _ ?a) (translate_expr _ ?b)] => |
| 629 |
specialize (IHvalid_expr1 _ a _ _ |
| 630 |
ltac:(eassumption) ltac:(eassumption)); |
| 631 |
specialize (IHvalid_expr2 _ b _ _ |
| 632 |
ltac:(eassumption) ltac:(eassumption)) |
| 633 |
end. |
| 634 |
cbn [locally_equivalent_nobounds |
| 635 |
locally_equivalent_nobounds_base |
| 636 |
locally_equivalent equivalent |
| 637 |
equivalent_base rep.equiv rep.Z ident.literal] in *. |
| 638 |
cbv [WeakestPrecondition.dexpr ident.literal] in *. |
| 639 |
cbn [WeakestPrecondition.expr WeakestPrecondition.expr_body |
| 640 |
Semantics.interp_binop]. |
| 641 |
sepsimpl_hyps. |
| 642 |
eapply Proper_expr; [ | eassumption ]. |
| 643 |
repeat intro; subst. |
| 644 |
eapply Proper_expr; [ | eassumption ]. |
| 645 |
repeat intro; subst. |
| 646 |
rewrite MulSplit.Z.mul_high_div. |
| 647 |
apply word.unsigned_inj. |
| 648 |
rewrite word.unsigned_mulhuu, !word.unsigned_of_Z. |
| 649 |
cbv [word.wrap]. Z.rewrite_mod_small. |
| 650 |
congruence. } |
| 651 |
{ (* truncating_shiftl *) |
| 652 |
cbv [rtruncating_shiftl literal_eqb literal_ltwidth invert_literal]. |
| 653 |
rewrite Z.eqb_refl, is_bounded_by_bool_width_range by lia. |
| 654 |
specialize (IHvalid_expr _ _ _ _ |
| 655 |
ltac:(eassumption) ltac:(eassumption)). |
| 656 |
cbn [locally_equivalent_nobounds |
| 657 |
locally_equivalent_nobounds_base |
| 658 |
locally_equivalent equivalent |
| 659 |
equivalent_base rep.equiv rep.Z ident.literal] in *. |
| 660 |
cbv [WeakestPrecondition.dexpr ident.literal] in *. |
| 661 |
cbn [WeakestPrecondition.expr WeakestPrecondition.expr_body |
| 662 |
Semantics.interp_binop]. |
| 663 |
sepsimpl_hyps. |
| 664 |
eapply Proper_expr; [ | eassumption ]. |
| 665 |
repeat intro; subst. |
| 666 |
cbv [WeakestPrecondition.literal dlet.dlet]. |
| 667 |
rewrite TruncatingShiftl.Z.truncating_shiftl_correct. |
| 668 |
apply word.unsigned_inj. |
| 669 |
rewrite word.unsigned_slu, !word.unsigned_of_Z |
| 670 |
by (rewrite word.unsigned_of_Z; cbv [word.wrap]; |
| 671 |
Z.rewrite_mod_small; lia). |
| 672 |
cbv [word.wrap]. Z.rewrite_mod_small. |
| 673 |
congruence. } |
| 674 |
{ (* lnot_modulo *) |
| 675 |
cbv [rlnot_modulo invert_literal]. |
| 676 |
rewrite (proj2 (Z.eqb_eq _ _)) by lia. |
| 677 |
specialize (IHvalid_expr _ _ _ _ |
| 678 |
ltac:(eassumption) ltac:(eassumption)). |
| 679 |
break_innermost_match; Z.ltb_to_lt. |
| 680 |
all: cbn [locally_equivalent_nobounds |
| 681 |
locally_equivalent_nobounds_base |
| 682 |
locally_equivalent equivalent |
| 683 |
equivalent_base rep.equiv rep.Z ident.literal] in *. |
| 684 |
all: cbv [WeakestPrecondition.dexpr ident.literal] in *. |
| 685 |
all: cbn [WeakestPrecondition.expr WeakestPrecondition.expr_body |
| 686 |
Semantics.interp_binop]. |
| 687 |
all: sepsimpl_hyps. |
| 688 |
all: eapply Proper_expr; [ | eassumption ]. |
| 689 |
all: repeat intro; subst. |
| 690 |
all: cbv [WeakestPrecondition.literal dlet.dlet]. |
| 691 |
all: apply word.unsigned_inj. |
| 692 |
all: let H := match goal with H : 2^Z.log2 _ = ?m |- context[Definitions.Z.lnot_modulo _ ?m] => H end in |
| 693 |
rewrite <- H, Z.lnot_modulo_correct, <- Z.land_ones, -> ?H by auto using Z.log2_nonneg. |
| 694 |
all: rewrite word.unsigned_xor, ?word.unsigned_and_nowrap, !word.unsigned_of_Z. |
| 695 |
all: apply Z.bits_inj'; intros; autorewrite with Ztestbit Ztestbit_full. |
| 696 |
all: cbv [xorb negb andb orb]; break_innermost_match; Z.ltb_to_lt; try congruence; try lia. } |
| 697 |
|
| 698 |
{ (* select *) |
| 699 |
cbv [ident.literal rselect literal_eqb invert_literal]. |
| 700 |
rewrite !Z.eqb_refl. |
| 701 |
specialize (IHvalid_expr _ _ _ _ |
| 702 |
ltac:(eassumption) ltac:(eassumption)). |
| 703 |
cbn [locally_equivalent_nobounds |
| 704 |
locally_equivalent_nobounds_base |
| 705 |
locally_equivalent equivalent |
| 706 |
equivalent_base rep.equiv rep.Z ident.literal] in *. |
| 707 |
cbv [WeakestPrecondition.dexpr ident.literal] in *. |
| 708 |
cbn [WeakestPrecondition.expr WeakestPrecondition.expr_body |
| 709 |
Semantics.interp_binop Semantics.interp_op1]. |
| 710 |
sepsimpl_hyps. |
| 711 |
eapply Proper_expr; [ | eassumption ]. |
| 712 |
repeat intro; subst. |
| 713 |
rewrite Zselect.Z.zselect_correct. |
| 714 |
apply word.unsigned_inj. |
| 715 |
rewrite word.unsigned_add, word.unsigned_eqb. |
| 716 |
rewrite <-Bool.pull_bool_if, !word.unsigned_of_Z. |
| 717 |
cbv [word.wrap]. Z.rewrite_mod_small. |
| 718 |
pose proof word.width_pos. |
| 719 |
break_match; subst; Z.rewrite_mod_small; |
| 720 |
Z.ltb_to_lt; try lia. |
| 721 |
all:pull_Zmod. |
| 722 |
all:autorewrite with zsimplify_fast. |
| 723 |
all:rewrite <-?word.ring_morph_opp, ?word.unsigned_of_Z. |
| 724 |
all:cbv [word.wrap]; rewrite ?Zplus_mod_idemp_l, ?Zmod_mod; trivial. |
| 725 |
rewrite Z.mod_opp_l_nz |
| 726 |
by (rewrite ?Z.mod_1_l; auto with zarith). |
| 727 |
Z.rewrite_mod_small. |
| 728 |
reflexivity. } |
| 729 |
{ (* opp *) |
| 730 |
specialize (IHvalid_expr _ _ _ _ |
| 731 |
ltac:(eassumption) ltac:(eassumption)). |
| 732 |
cbn [locally_equivalent_nobounds |
| 733 |
locally_equivalent_nobounds_base |
| 734 |
locally_equivalent equivalent |
| 735 |
equivalent_base rep.equiv rep.Z ident.literal] in *. |
| 736 |
cbv [WeakestPrecondition.dexpr ident.literal] in *. |
| 737 |
cbn [WeakestPrecondition.expr WeakestPrecondition.expr_body |
| 738 |
Semantics.interp_binop]. |
| 739 |
sepsimpl_hyps. |
| 740 |
eapply Proper_expr; [ | eassumption ]. |
| 741 |
repeat intro; subst. |
| 742 |
apply word.unsigned_inj. |
| 743 |
rewrite word.unsigned_sub, word.unsigned_of_Z_0. |
| 744 |
rewrite !word.unsigned_of_Z. |
| 745 |
f_equal. lia. } |
| 746 |
Qed. |
| 747 |
|
| 748 |
Lemma translate_expr_correct {t} |
| 749 |
(* three exprs, representing the same Expr with different vars *) |
| 750 |
(e1 : @API.expr (fun _ => unit) (type.base t)) |
| 751 |
(e2 : @API.expr API.interp_type (type.base t)) |
| 752 |
(e3 : @API.expr ltype (type.base t)) : |
| 753 |
(* e1 is a valid input to translate_expr *) |
| 754 |
valid_expr true e1 -> |
| 755 |
forall G locals, |
| 756 |
wf3 G e1 e2 e3 -> |
| 757 |
let out := translate_expr true e3 in |
| 758 |
context_equiv G locals -> |
| 759 |
locally_equivalent (API.interp e2) out locals. |
| 760 |
Proof. |
| 761 |
apply (translate_expr_correct' _ _ _ true). |
| 762 |
Qed. |
| 763 |
End Expr. |