| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import Bool. |
| 3 |
From Coq Require Import List. |
| 4 |
From Coq Require Import Lia. |
| 5 |
Require Import (*hints*) Coq.btauto.Algebra. |
| 6 |
Require Import bedrock2.Syntax. |
| 7 |
Require Import coqutil.Word.Interface coqutil.Word.Properties. |
| 8 |
Require Import coqutil.Map.Interface. |
| 9 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 10 |
Require Import Crypto.Bedrock.Field.Translation.Expr. |
| 11 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.Expr. |
| 12 |
Require Import Crypto.Language.API. |
| 13 |
Require Import Crypto.Util.Decidable. |
| 14 |
Require Import Crypto.Util.ZRange. |
| 15 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 16 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 17 |
Require Import Crypto.Util.Bool.Reflect. |
| 18 |
Local Open Scope Z_scope. |
| 19 |
Local Open Scope bool_scope. |
| 20 |
|
| 21 |
Import API.Compilers. |
| 22 |
Import Types.Notations. |
| 23 |
|
| 24 |
Section Expr. |
| 25 |
Context |
| 26 |
{width BW word mem locals ext_spec varname_gen error} |
| 27 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}. |
| 28 |
Context {ok : ok}. |
| 29 |
|
| 30 |
Local Existing Instance Types.rep.Z. |
| 31 |
Local Existing Instance Types.rep.listZ_local. |
| 32 |
|
| 33 |
Definition is_fst_snd_ident {t} (i : ident.ident t) : bool := |
| 34 |
match i with |
| 35 |
| ident.fst base_Z base_Z => true |
| 36 |
| ident.snd base_Z base_Z => true |
| 37 |
| _ => false |
| 38 |
end. |
| 39 |
|
| 40 |
Definition valid_expr_App1_bool {t} (require_casts : bool) |
| 41 |
(e : @API.expr (fun _ => unit) t) : bool := |
| 42 |
match e with |
| 43 |
| expr.App type_range (type.arrow type_Z type_Z) f r => |
| 44 |
is_cast_ident_expr f && is_cast_literal(width:=width) r |
| 45 |
| expr.App type_range2 (type.arrow type_ZZ type_ZZ) f r => |
| 46 |
is_cast_ident_expr f && is_cast2_literal(width:=width) r |
| 47 |
| expr.Ident (type.arrow type_ZZ type_Z) i => |
| 48 |
negb require_casts && is_fst_snd_ident i |
| 49 |
| _ => false |
| 50 |
end. |
| 51 |
|
| 52 |
Definition is_mul_high_ident {t} (i : ident.ident t) : bool := |
| 53 |
match i with |
| 54 |
| ident.Z_mul_high => true |
| 55 |
| _ => false |
| 56 |
end. |
| 57 |
Definition is_mul_high_ident_expr {t} |
| 58 |
(e : @API.expr (fun _ => unit) t) : bool := |
| 59 |
match e with |
| 60 |
| expr.Ident (type.arrow type_Z |
| 61 |
(type.arrow type_Z |
| 62 |
(type.arrow type_Z type_Z))) |
| 63 |
i => is_mul_high_ident i |
| 64 |
| _ => false |
| 65 |
end. |
| 66 |
|
| 67 |
Definition valid_expr_binop_bool {t} (require_casts : bool) |
| 68 |
(e : @API.expr (fun _ => unit) t) : bool := |
| 69 |
match e with |
| 70 |
| expr.App |
| 71 |
type_Z (type.arrow type_Z (type.arrow type_Z type_Z)) |
| 72 |
f (expr.Ident _ (ident.Literal base.type.Z s)) => |
| 73 |
(is_mul_high_ident_expr f) |
| 74 |
&& (negb require_casts) |
| 75 |
&& (Z.eqb s (2 ^ width)) |
| 76 |
| expr.Ident (type.arrow type_Z (type.arrow type_Z type_Z)) i => |
| 77 |
match translate_binop i with |
| 78 |
| None => false |
| 79 |
| Some _ => negb require_casts |
| 80 |
end |
| 81 |
| _ => false |
| 82 |
end. |
| 83 |
|
| 84 |
Definition is_shiftl_ident {t} (i : ident.ident t) := |
| 85 |
match i with |
| 86 |
| ident.Z_truncating_shiftl => true |
| 87 |
| _ => false |
| 88 |
end. |
| 89 |
Definition is_shiftl_ident_expr |
| 90 |
{t} (e : @API.expr (fun _ => unit) t) := |
| 91 |
match e with |
| 92 |
| expr.Ident |
| 93 |
(type.arrow type_Z (type.arrow type_Z (type.arrow type_Z type_Z))) |
| 94 |
i => is_shiftl_ident i |
| 95 |
| _ => false |
| 96 |
end. |
| 97 |
Definition valid_expr_shift_bool {t} (require_casts : bool) |
| 98 |
(e : @API.expr (fun _ => unit) t) : bool := |
| 99 |
match e with |
| 100 |
| expr.App |
| 101 |
type_Z (type.arrow type_Z (type.arrow type_Z type_Z)) |
| 102 |
f (expr.Ident _ (ident.Literal base.type.Z s)) => |
| 103 |
(is_shiftl_ident_expr f) |
| 104 |
&& (negb require_casts) |
| 105 |
&& Z.eqb s width |
| 106 |
| expr.Ident _ ident.Z_shiftr => |
| 107 |
negb require_casts |
| 108 |
| expr.Ident _ ident.Z_shiftl => |
| 109 |
negb require_casts |
| 110 |
| _ => false |
| 111 |
end. |
| 112 |
|
| 113 |
Definition valid_shifter {t} |
| 114 |
(e : @API.expr (fun _ => unit) t) : bool := |
| 115 |
match e with |
| 116 |
| expr.Ident _ (ident.Literal base.type.Z n) => |
| 117 |
is_bounded_by_bool n (@width_range width) |
| 118 |
| _ => false |
| 119 |
end. |
| 120 |
|
| 121 |
Definition valid_expr_nth_default_App3_bool {t} |
| 122 |
(e : @API.expr (fun _ => unit) t) : bool := |
| 123 |
match e with |
| 124 |
| expr.Ident _ (ident.List_nth_default base_Z) => |
| 125 |
true |
| 126 |
| _ => false |
| 127 |
end. |
| 128 |
Definition valid_expr_nth_default_App2_bool {t} |
| 129 |
(e : @API.expr (fun _ => unit) t) : bool := |
| 130 |
match e with |
| 131 |
| expr.App |
| 132 |
type_Z (type.arrow |
| 133 |
type_listZ |
| 134 |
(type.arrow type_nat type_Z)) |
| 135 |
f (expr.Ident _ (ident.Literal base.type.Z d)) => |
| 136 |
valid_expr_nth_default_App3_bool f && is_bounded_by_bool d (@max_range width) |
| 137 |
| _ => false |
| 138 |
end. |
| 139 |
Definition valid_expr_nth_default_App1_bool {t} |
| 140 |
(e : @API.expr (fun _ => unit) t) : bool := |
| 141 |
match e with |
| 142 |
| expr.App type_listZ (type.arrow type_nat type_Z) |
| 143 |
f (expr.Var _ l) => |
| 144 |
valid_expr_nth_default_App2_bool f |
| 145 |
| _ => false |
| 146 |
end. |
| 147 |
Definition valid_expr_nth_default_bool {t} |
| 148 |
(e : @API.expr (fun _ => unit) t) : bool := |
| 149 |
match e with |
| 150 |
| expr.App |
| 151 |
type_nat type_Z |
| 152 |
f (expr.Ident _ (ident.Literal base.type.nat i)) => |
| 153 |
valid_expr_nth_default_App1_bool f |
| 154 |
| _ => false |
| 155 |
end. |
| 156 |
|
| 157 |
Definition is_literalz {t} |
| 158 |
(e : @API.expr (fun _ => unit) t) (z : Z) : bool := |
| 159 |
match e with |
| 160 |
| expr.Ident _ (ident.Literal base.type.Z x) => Z.eqb x z |
| 161 |
| _ => false |
| 162 |
end. |
| 163 |
Definition valid_expr_select_bool {t} |
| 164 |
rc (e : @API.expr (fun _ => unit) t) : bool := |
| 165 |
match e with |
| 166 |
| expr.Ident _ ident.Z_zselect => negb rc |
| 167 |
| _ => false |
| 168 |
end. |
| 169 |
|
| 170 |
Definition valid_expr_lnot_modulo_bool {t} (require_casts : bool) |
| 171 |
(e : @API.expr (fun _ => unit) t) : bool := |
| 172 |
match e with |
| 173 |
| expr.Ident _ ident.Z_lnot_modulo => |
| 174 |
negb require_casts |
| 175 |
| _ => false |
| 176 |
end. |
| 177 |
Definition valid_lnot_modulus {t} |
| 178 |
(e : @API.expr (fun _ => unit) t) : bool := |
| 179 |
match e with |
| 180 |
| expr.Ident _ (ident.Literal base.type.Z n) => |
| 181 |
(is_bounded_by_bool (n-1) (@max_range width)) && (n =? 2 ^ Z.log2 n) |
| 182 |
| _ => false |
| 183 |
end. |
| 184 |
|
| 185 |
(* opp is the only unary operation we accept *) |
| 186 |
Definition is_opp_ident {t} (i : ident t) : bool := |
| 187 |
match i with |
| 188 |
| ident.Z_opp => true |
| 189 |
| _ => false |
| 190 |
end. |
| 191 |
Definition is_opp_ident_expr |
| 192 |
{t} (e : @API.expr (fun _ => unit) t) := |
| 193 |
match e with |
| 194 |
| expr.Ident (type.arrow type_Z type_Z) i => is_opp_ident i |
| 195 |
| _ => false |
| 196 |
end. |
| 197 |
|
| 198 |
(* Because some operations are only valid if the arguments obey certain |
| 199 |
constraints, and to make the inductive logic work out, it helps to be able |
| 200 |
to call valid_expr_bool' recursively but have it return false for anything |
| 201 |
other than a specific kind of operation. This lets us, on encountering the |
| 202 |
very last application in a multi-argument function, take a sneak peek ahead |
| 203 |
to see if the rest of the applications match a certain kind of operation, |
| 204 |
and then enforce any constraints on the last argument. *) |
| 205 |
Inductive PartialMode := NotPartial | Binop | Shift | Select | Lnot. |
| 206 |
|
| 207 |
Fixpoint valid_expr_bool' {t} |
| 208 |
(mode : PartialMode) (require_casts : bool) |
| 209 |
(e : @API.expr (fun _ => unit) t) {struct e} : bool := |
| 210 |
match mode with |
| 211 |
| Binop => |
| 212 |
match e with |
| 213 |
| expr.App type_Z (type.arrow type_Z type_Z) f x => |
| 214 |
(valid_expr_binop_bool require_casts f) |
| 215 |
&& valid_expr_bool' NotPartial true x |
| 216 |
| _ => false |
| 217 |
end |
| 218 |
| Shift => |
| 219 |
match e with |
| 220 |
| expr.App type_Z (type.arrow type_Z type_Z) f x => |
| 221 |
(valid_expr_shift_bool require_casts f) |
| 222 |
&& valid_expr_bool' NotPartial true x |
| 223 |
| _ => false |
| 224 |
end |
| 225 |
| Select => |
| 226 |
match e with |
| 227 |
| expr.App type_Z (type.arrow type_Z type_Z) f x => |
| 228 |
(valid_expr_bool' Select require_casts f) |
| 229 |
&& is_literalz x 0 |
| 230 |
| expr.App type_Z |
| 231 |
(type.arrow type_Z |
| 232 |
(type.arrow type_Z type_Z)) f x => |
| 233 |
(valid_expr_select_bool require_casts f) |
| 234 |
&& valid_expr_bool' NotPartial true x |
| 235 |
| _ => false |
| 236 |
end |
| 237 |
| Lnot => |
| 238 |
match e with |
| 239 |
| expr.App type_Z (type.arrow type_Z type_Z) f x => |
| 240 |
(valid_expr_lnot_modulo_bool require_casts f) |
| 241 |
&& valid_expr_bool' NotPartial true x |
| 242 |
| _ => false |
| 243 |
end |
| 244 |
| NotPartial => |
| 245 |
match e with |
| 246 |
| expr.App type_nat _ f x => |
| 247 |
(* only thing accepting nat is nth_default *) |
| 248 |
if require_casts |
| 249 |
then false |
| 250 |
else valid_expr_nth_default_bool (expr.App f x) |
| 251 |
| expr.App type_Z type_Z f x => |
| 252 |
if valid_expr_bool' Binop require_casts f |
| 253 |
then (* f is a binop applied to one valid argument; |
| 254 |
check that x (second argument) is also valid *) |
| 255 |
valid_expr_bool' NotPartial true x |
| 256 |
else if valid_expr_bool' Shift require_casts f |
| 257 |
then (* f is a shift applied to one valid |
| 258 |
argument; check that x (shifting index) is a |
| 259 |
valid shifter *) |
| 260 |
valid_shifter x |
| 261 |
else if valid_expr_bool' Select require_casts f |
| 262 |
then (* f is a select; make sure x = 2^w-1 *) |
| 263 |
is_literalz x (2^width-1) |
| 264 |
else |
| 265 |
if valid_expr_bool' Lnot require_casts f |
| 266 |
then (* f is lnot_modulo; make sure argument is a valid |
| 267 |
modulus *) |
| 268 |
(negb require_casts) |
| 269 |
&& valid_lnot_modulus x |
| 270 |
else |
| 271 |
if is_opp_ident_expr f |
| 272 |
then (* f is opp *) |
| 273 |
(negb require_casts) |
| 274 |
&& valid_expr_bool' NotPartial true x |
| 275 |
else (* must be a cast *) |
| 276 |
(valid_expr_App1_bool require_casts f) |
| 277 |
&& valid_expr_bool' NotPartial false x |
| 278 |
| expr.App type_ZZ type_Z f x => |
| 279 |
(* fst or snd *) |
| 280 |
(valid_expr_App1_bool require_casts f) |
| 281 |
&& valid_expr_bool' NotPartial true x |
| 282 |
| expr.App type_ZZ type_ZZ f x => |
| 283 |
(valid_expr_App1_bool require_casts f) |
| 284 |
&& valid_expr_bool' NotPartial false x |
| 285 |
| expr.Ident _ (ident.Literal base.type.Z z) => |
| 286 |
is_bounded_by_bool z (@max_range width)|| negb require_casts |
| 287 |
| expr.Ident _ (ident.Literal base.type.nat n) => |
| 288 |
negb require_casts |
| 289 |
| expr.Var type_Z v => true |
| 290 |
| expr.Var type_listZ v => true |
| 291 |
| _ => false |
| 292 |
end |
| 293 |
end. |
| 294 |
|
| 295 |
Definition valid_expr_bool {t} := @valid_expr_bool' t NotPartial. |
| 296 |
|
| 297 |
Lemma valid_expr_App1_bool_type {t} rc (e : API.expr t) : |
| 298 |
valid_expr_App1_bool rc e = true -> |
| 299 |
(exists d, |
| 300 |
t = type.arrow type_Z d |
| 301 |
\/ t = type.arrow type_ZZ d). |
| 302 |
Proof. |
| 303 |
cbv [valid_expr_App1_bool]. |
| 304 |
destruct e; |
| 305 |
match goal with |
| 306 |
| idc : ident.ident _ |- _ => |
| 307 |
destruct idc; try congruence; |
| 308 |
break_match; try congruence; intros; |
| 309 |
eexists; right; reflexivity |
| 310 |
| v: unit, t : API.type |- _ => |
| 311 |
destruct t; congruence |
| 312 |
| f : API.expr (?s -> ?d) |- _ => |
| 313 |
destruct d; |
| 314 |
break_match; try congruence; |
| 315 |
try (eexists; right; reflexivity); |
| 316 |
eexists; left; reflexivity |
| 317 |
| |- forall (_: false = true), _ => congruence |
| 318 |
| |- context [@expr.LetIn _ _ _ _ ?B] => |
| 319 |
destruct B; congruence |
| 320 |
| _ => idtac |
| 321 |
end. |
| 322 |
Qed. |
| 323 |
|
| 324 |
Lemma is_mul_high_ident_expr_type {t} (f : API.expr t) : |
| 325 |
is_mul_high_ident_expr f = true -> |
| 326 |
t = type.arrow type_Z |
| 327 |
(type.arrow type_Z |
| 328 |
(type.arrow type_Z type_Z)). |
| 329 |
Proof. |
| 330 |
cbv [is_mul_high_ident_expr]. |
| 331 |
break_match; congruence. |
| 332 |
Qed. |
| 333 |
|
| 334 |
Lemma valid_expr_shift_bool_type {t} rc (f : API.expr t) : |
| 335 |
valid_expr_shift_bool rc f = true -> |
| 336 |
t = type.arrow type_Z |
| 337 |
(type.arrow type_Z type_Z). |
| 338 |
Proof. |
| 339 |
cbv [valid_expr_shift_bool]. |
| 340 |
break_match; congruence. |
| 341 |
Qed. |
| 342 |
|
| 343 |
Lemma valid_expr_binop_bool_type {t} rc (f : API.expr t) : |
| 344 |
valid_expr_binop_bool rc f = true -> |
| 345 |
t = type.arrow type_Z |
| 346 |
(type.arrow type_Z type_Z). |
| 347 |
Proof. |
| 348 |
cbv [valid_expr_binop_bool translate_binop]. |
| 349 |
destruct f; |
| 350 |
match goal with |
| 351 |
| idc : ident.ident _ |- _ => |
| 352 |
destruct idc; try congruence; |
| 353 |
break_match; congruence |
| 354 |
| |- forall (_: false = true), _ => congruence |
| 355 |
| _ => idtac |
| 356 |
end; [ ]. |
| 357 |
break_match; congruence. |
| 358 |
Qed. |
| 359 |
|
| 360 |
Lemma valid_expr_nth_default_App2_bool_type {t} |
| 361 |
(f : API.expr t) : |
| 362 |
valid_expr_nth_default_App2_bool f = true -> |
| 363 |
match t with |
| 364 |
| type.arrow type_listZ |
| 365 |
(type.arrow type_nat type_Z) => |
| 366 |
True |
| 367 |
| _ => False |
| 368 |
end. |
| 369 |
Proof. |
| 370 |
cbv [valid_expr_nth_default_App2_bool]. |
| 371 |
break_match; try congruence; [ ]. |
| 372 |
tauto. |
| 373 |
Qed. |
| 374 |
|
| 375 |
Lemma valid_expr_nth_default_App1_bool_type {t} |
| 376 |
(f : API.expr t) : |
| 377 |
valid_expr_nth_default_App1_bool f = true -> |
| 378 |
match t with |
| 379 |
| type.arrow type_nat type_Z => |
| 380 |
True |
| 381 |
| _ => False |
| 382 |
end. |
| 383 |
Proof. |
| 384 |
cbv [valid_expr_nth_default_App1_bool]. |
| 385 |
break_match; try congruence; [ ]. |
| 386 |
tauto. |
| 387 |
Qed. |
| 388 |
|
| 389 |
Lemma valid_expr_nth_default_App3_bool_impl1 {t} |
| 390 |
(f : API.expr t) n l d : |
| 391 |
valid_expr_nth_default_App3_bool f = true -> |
| 392 |
is_bounded_by_bool d (@max_range width) = true -> |
| 393 |
(match t as t0 return expr.expr t0 -> Prop with |
| 394 |
| type.arrow type_Z |
| 395 |
(type.arrow type_listZ (type.arrow type_nat type_Z)) => |
| 396 |
fun f => |
| 397 |
valid_expr |
| 398 |
false (expr.App |
| 399 |
(expr.App |
| 400 |
(expr.App |
| 401 |
f |
| 402 |
(expr.Ident |
| 403 |
(ident.Literal (t:=base.type.Z) d))) |
| 404 |
(expr.Var l)) |
| 405 |
(expr.Ident (ident.Literal (t:=base.type.nat) n))) |
| 406 |
| _ => fun _ => False |
| 407 |
end) f. |
| 408 |
Proof. |
| 409 |
cbv [valid_expr_nth_default_App3_bool]. |
| 410 |
break_match; try congruence; [ ]. |
| 411 |
intros. constructor. |
| 412 |
cbv [is_bounded_by_bool upper lower max_range] in *. |
| 413 |
repeat match goal with |
| 414 |
| H : _ && _ = true |- _ => |
| 415 |
apply andb_true_iff in H; destruct H |
| 416 |
end. |
| 417 |
Z.ltb_to_lt; lia. |
| 418 |
Qed. |
| 419 |
|
| 420 |
Lemma valid_expr_nth_default_App2_bool_impl1 {t} |
| 421 |
(f : API.expr t) n l : |
| 422 |
valid_expr_nth_default_App2_bool f = true -> |
| 423 |
(match t as t0 return expr.expr t0 -> Prop with |
| 424 |
| type.arrow type_listZ (type.arrow type_nat type_Z) => |
| 425 |
fun f => |
| 426 |
valid_expr |
| 427 |
false |
| 428 |
(expr.App |
| 429 |
(expr.App f (expr.Var l)) |
| 430 |
(expr.Ident (ident.Literal (t:=base.type.nat) n))) |
| 431 |
| _ => fun _ => False |
| 432 |
end) f. |
| 433 |
Proof. |
| 434 |
cbv [valid_expr_nth_default_App2_bool]. |
| 435 |
break_match; try congruence; [ ]. |
| 436 |
intros; |
| 437 |
repeat match goal with |
| 438 |
| H : _ && _ = true |- _ => |
| 439 |
apply andb_true_iff in H; destruct H |
| 440 |
| H : valid_expr_nth_default_App3_bool _ = true |- _ => |
| 441 |
eapply valid_expr_nth_default_App3_bool_impl1 in H; |
| 442 |
eassumption |
| 443 |
end. |
| 444 |
Qed. |
| 445 |
|
| 446 |
Lemma valid_expr_nth_default_App1_bool_impl1 {t} |
| 447 |
(f : API.expr t) n : |
| 448 |
valid_expr_nth_default_App1_bool f = true -> |
| 449 |
(match t as t0 return expr.expr t0 -> Prop with |
| 450 |
| type.arrow type_nat type_Z => |
| 451 |
fun f => |
| 452 |
valid_expr |
| 453 |
false (expr.App f |
| 454 |
(expr.Ident (ident.Literal (t:=base.type.nat) n))) |
| 455 |
| _ => fun _ => False |
| 456 |
end) f. |
| 457 |
Proof. |
| 458 |
cbv [valid_expr_nth_default_App1_bool]. |
| 459 |
break_match; try congruence; [ ]. |
| 460 |
intros; |
| 461 |
repeat match goal with |
| 462 |
| H : _ && _ = true |- _ => |
| 463 |
apply andb_true_iff in H; destruct H |
| 464 |
end; |
| 465 |
match goal with |
| 466 |
| H : valid_expr_nth_default_App2_bool _ = true |- _ => |
| 467 |
eapply valid_expr_nth_default_App2_bool_impl1 in H |
| 468 |
end. |
| 469 |
break_match_hyps; try tauto; [ ]. |
| 470 |
eassumption. |
| 471 |
Qed. |
| 472 |
|
| 473 |
Lemma is_fst_snd_ident_impl1 {t} (i : ident.ident t) : |
| 474 |
is_fst_snd_ident i = true -> |
| 475 |
(match t as t0 return ident.ident t0 -> Prop with |
| 476 |
| type.arrow type_ZZ type_Z => |
| 477 |
fun i => |
| 478 |
forall x : API.expr type_ZZ, |
| 479 |
valid_expr true x -> |
| 480 |
valid_expr false (expr.App (expr.Ident i) x) |
| 481 |
| _ => fun _ => False |
| 482 |
end) i. |
| 483 |
Proof. |
| 484 |
cbv [is_fst_snd_ident]. |
| 485 |
break_match; try congruence; [ | ]; |
| 486 |
intros; constructor; eauto. |
| 487 |
Qed. |
| 488 |
|
| 489 |
Lemma is_cast_literal_ident_eq {t} (i : ident.ident t) : |
| 490 |
is_cast_literal_ident (width:=width) i = true -> |
| 491 |
(match t as t0 return ident.ident t0 -> Prop with |
| 492 |
| type_range => |
| 493 |
fun i => i = ident.Literal (t:=base.type.zrange) (@max_range width) |
| 494 |
| _ => fun _ => False |
| 495 |
end) i. |
| 496 |
Proof. |
| 497 |
cbv [is_cast_literal_ident]. |
| 498 |
break_match; try congruence; [ ]. |
| 499 |
cbv [range_good]. |
| 500 |
intros; progress reflect_beq_to_eq zrange_beq; subst. |
| 501 |
reflexivity. |
| 502 |
Qed. |
| 503 |
|
| 504 |
Lemma is_cast_literal_eq {t} (r : API.expr t) : |
| 505 |
is_cast_literal (width:=width) r = true -> |
| 506 |
(match t as t0 return @API.expr (fun _ => unit) t0 -> Prop with |
| 507 |
| type_range => |
| 508 |
fun r => |
| 509 |
r = expr.Ident (ident.Literal (t:=base.type.zrange) (@max_range width)) |
| 510 |
| _ => fun _ => False |
| 511 |
end) r. |
| 512 |
Proof. |
| 513 |
cbv [is_cast_literal]. |
| 514 |
break_match; try congruence; [ ]. |
| 515 |
intros; |
| 516 |
match goal with |
| 517 |
| H : is_cast_literal_ident _ = true |- _ => |
| 518 |
apply is_cast_literal_ident_eq in H |
| 519 |
end. |
| 520 |
congruence. |
| 521 |
Qed. |
| 522 |
|
| 523 |
Lemma is_pair_range_eq {t} (i : ident.ident t) : |
| 524 |
is_pair_range i = true -> |
| 525 |
(match t as t0 return ident.ident t0 -> Prop with |
| 526 |
| type.arrow type_range (type.arrow type_range type_range2) => |
| 527 |
fun i => i = ident.pair |
| 528 |
| _ => fun _ => False |
| 529 |
end) i. |
| 530 |
Proof. |
| 531 |
cbv [is_pair_range]. |
| 532 |
break_match; congruence. |
| 533 |
Qed. |
| 534 |
|
| 535 |
Lemma is_cast2_literal_App2_eq {t} (r : API.expr t) : |
| 536 |
is_cast2_literal_App2 r = true -> |
| 537 |
(match t as t0 return @API.expr (fun _ => unit) t0 -> Prop with |
| 538 |
| type.arrow type_range (type.arrow type_range type_range2) => |
| 539 |
fun r => r = expr.Ident ident.pair |
| 540 |
| _ => fun _ => False |
| 541 |
end) r. |
| 542 |
Proof. |
| 543 |
cbv [is_cast2_literal_App2]. |
| 544 |
break_match; try congruence; [ ]. |
| 545 |
intros; |
| 546 |
match goal with |
| 547 |
| H : is_pair_range _ = true |- _ => |
| 548 |
apply is_pair_range_eq in H |
| 549 |
end. |
| 550 |
congruence. |
| 551 |
Qed. |
| 552 |
|
| 553 |
Lemma is_cast2_literal_App1_eq {t} (r : API.expr t) : |
| 554 |
is_cast2_literal_App1 (width:=width) r = true -> |
| 555 |
(match t as t0 return @API.expr (fun _ => unit) t0 -> Prop with |
| 556 |
| type.arrow type_range type_range2 => |
| 557 |
fun r => |
| 558 |
r = expr.App |
| 559 |
(expr.Ident ident.pair) |
| 560 |
(expr.Ident |
| 561 |
(ident.Literal (t:=base.type.zrange) (@max_range width))) |
| 562 |
| _ => fun _ => False |
| 563 |
end) r. |
| 564 |
Proof. |
| 565 |
cbv [is_cast2_literal_App1]. |
| 566 |
break_match; try congruence; [ ]. |
| 567 |
intros; |
| 568 |
repeat match goal with |
| 569 |
| H : _ && _ = true |- _ => |
| 570 |
apply andb_true_iff in H; destruct H |
| 571 |
| H : is_cast_literal _ = true |- _ => |
| 572 |
apply is_cast_literal_eq in H; subst |
| 573 |
| H : is_cast2_literal_App2 _ = true |- _ => |
| 574 |
apply is_cast2_literal_App2_eq in H; subst |
| 575 |
end. |
| 576 |
congruence. |
| 577 |
Qed. |
| 578 |
|
| 579 |
Lemma is_cast2_literal_eq {t} (r : API.expr t) : |
| 580 |
is_cast2_literal (width:=width) r = true -> |
| 581 |
(match t as t0 return @API.expr (fun _ => unit) t0 -> Prop with |
| 582 |
| type_range2 => |
| 583 |
fun r => |
| 584 |
r = expr.App |
| 585 |
(expr.App |
| 586 |
(expr.Ident ident.pair) |
| 587 |
(expr.Ident |
| 588 |
(ident.Literal (t:=base.type.zrange) (@max_range width)))) |
| 589 |
(expr.Ident |
| 590 |
(ident.Literal (t:=base.type.zrange) (@max_range width))) |
| 591 |
| _ => fun _ => False |
| 592 |
end) r. |
| 593 |
Proof. |
| 594 |
cbv [is_cast2_literal]. |
| 595 |
break_match; try congruence; [ ]. |
| 596 |
intros; |
| 597 |
repeat match goal with |
| 598 |
| H : _ && _ = true |- _ => |
| 599 |
apply andb_true_iff in H; destruct H |
| 600 |
| H : is_cast_literal _ = true |- _ => |
| 601 |
apply is_cast_literal_eq in H; subst |
| 602 |
| H : is_cast2_literal_App1 _ = true |- _ => |
| 603 |
apply is_cast2_literal_App1_eq in H; subst |
| 604 |
end. |
| 605 |
congruence. |
| 606 |
Qed. |
| 607 |
|
| 608 |
Lemma is_cast_ident_expr_impl1 {t} rc (f : API.expr t) : |
| 609 |
is_cast_ident_expr f = true -> |
| 610 |
(match t as t0 return expr.expr t0 -> Prop with |
| 611 |
| type.arrow type_range (type.arrow type_Z type_Z) => |
| 612 |
fun f => |
| 613 |
forall |
| 614 |
(r : API.expr type_range) |
| 615 |
(x : API.expr type_Z), |
| 616 |
is_cast_literal (width:=width) r = true -> |
| 617 |
valid_expr false x -> |
| 618 |
valid_expr rc (expr.App (expr.App f r) x) |
| 619 |
| type.arrow type_range2 (type.arrow type_ZZ type_ZZ) => |
| 620 |
fun f => |
| 621 |
forall |
| 622 |
(r : API.expr type_range2) |
| 623 |
(x : API.expr type_ZZ), |
| 624 |
is_cast2_literal (width:=width) r = true -> |
| 625 |
valid_expr false x -> |
| 626 |
valid_expr rc (expr.App (expr.App f r) x) |
| 627 |
| _ => fun _ => False |
| 628 |
end) f. |
| 629 |
Proof. |
| 630 |
cbv [is_cast_ident_expr is_cast_ident]. |
| 631 |
break_match; try congruence; [ | ]; |
| 632 |
intros; |
| 633 |
match goal with |
| 634 |
| H : is_cast_literal _ = true |- _ => |
| 635 |
apply is_cast_literal_eq in H; subst |
| 636 |
| H : is_cast2_literal _ = true |- _ => |
| 637 |
apply is_cast2_literal_eq in H; subst |
| 638 |
end. |
| 639 |
{ constructor; |
| 640 |
cbv [range_good]; auto using zrange_lb. } |
| 641 |
{ constructor; |
| 642 |
cbv [range_good]; auto using zrange_lb. } |
| 643 |
Qed. |
| 644 |
|
| 645 |
Lemma valid_expr_App1_bool_impl1 {t} |
| 646 |
rc (f : API.expr t) : |
| 647 |
valid_expr_App1_bool rc f = true -> |
| 648 |
(match t as t0 return expr.expr t0 -> Prop with |
| 649 |
| type.arrow type_Z _ => |
| 650 |
fun f => |
| 651 |
forall x : API.expr type_Z, |
| 652 |
valid_expr false x -> |
| 653 |
valid_expr rc (expr.App f x) |
| 654 |
| type.arrow type_ZZ type_Z => |
| 655 |
fun f => |
| 656 |
forall x : API.expr type_ZZ, |
| 657 |
valid_expr true x -> |
| 658 |
valid_expr rc (expr.App f x) |
| 659 |
| type.arrow type_ZZ type_ZZ => |
| 660 |
fun f => |
| 661 |
forall x : API.expr type_ZZ, |
| 662 |
valid_expr false x -> |
| 663 |
valid_expr rc (expr.App f x) |
| 664 |
| _ => fun _ => False |
| 665 |
end) f. |
| 666 |
Proof. |
| 667 |
cbv [valid_expr_App1_bool]. |
| 668 |
remember t. |
| 669 |
destruct t; try congruence. |
| 670 |
{ intros; exfalso. |
| 671 |
break_match_hyps; congruence. } |
| 672 |
{ break_match; try congruence; [ | | ]; intros; |
| 673 |
repeat match goal with |
| 674 |
| H : _ && _ = true |- _ => |
| 675 |
apply andb_true_iff in H; destruct H |
| 676 |
| H : negb ?rc = true |- _ => |
| 677 |
destruct rc; cbn [negb] in *; try congruence; [ ] |
| 678 |
| H : is_fst_snd_ident _ = true |- _ => |
| 679 |
apply is_fst_snd_ident_impl1 in H; solve [eauto] |
| 680 |
| H : is_cast_ident_expr _ = true |- _ => |
| 681 |
eapply is_cast_ident_expr_impl1 in H; |
| 682 |
apply H; solve [eauto] |
| 683 |
end. } |
| 684 |
Qed. |
| 685 |
|
| 686 |
Lemma valid_expr_nth_default_bool_impl1 {t} |
| 687 |
(e : API.expr t) : |
| 688 |
(exists b, t = type.base b) -> |
| 689 |
valid_expr_nth_default_bool e = true -> |
| 690 |
valid_expr false e. |
| 691 |
Proof. |
| 692 |
cbv [valid_expr_nth_default_bool]. |
| 693 |
intro; destruct e; try congruence; [ ]. |
| 694 |
match goal with |
| 695 |
| _ : API.expr (type.arrow _ ?d) |- _ => |
| 696 |
destruct d |
| 697 |
end; |
| 698 |
break_match; try congruence; intros; [ ]. |
| 699 |
match goal with |
| 700 |
| H : valid_expr_nth_default_App1_bool _ = true |- _ => |
| 701 |
eapply valid_expr_nth_default_App1_bool_impl1 in H |
| 702 |
end. |
| 703 |
eassumption. |
| 704 |
Qed. |
| 705 |
|
| 706 |
Lemma is_mul_high_ident_eq {t} (i : ident.ident t) : |
| 707 |
is_mul_high_ident i = true -> |
| 708 |
(match t as t0 return ident.ident t0 -> Prop with |
| 709 |
| type.arrow type_Z (type.arrow type_Z (type.arrow type_Z type_Z)) => |
| 710 |
fun i => i = ident.Z_mul_high |
| 711 |
| _ => fun _ => False |
| 712 |
end) i. |
| 713 |
Proof. |
| 714 |
cbv [is_mul_high_ident]. |
| 715 |
break_match; congruence. |
| 716 |
Qed. |
| 717 |
|
| 718 |
Lemma is_mul_high_ident_expr_eq {t} (f : API.expr t) : |
| 719 |
is_mul_high_ident_expr f = true -> |
| 720 |
(match t as t0 return API.expr t0 -> Prop with |
| 721 |
| type.arrow type_Z (type.arrow type_Z (type.arrow type_Z type_Z)) => |
| 722 |
fun f => f = expr.Ident ident.Z_mul_high |
| 723 |
| _ => fun _ => False |
| 724 |
end) f. |
| 725 |
Proof. |
| 726 |
cbv [is_mul_high_ident_expr]. |
| 727 |
break_match; try congruence; [ ]. |
| 728 |
intros; |
| 729 |
match goal with |
| 730 |
| H : is_mul_high_ident _ = true |- _ => |
| 731 |
apply is_mul_high_ident_eq in H |
| 732 |
end. |
| 733 |
congruence. |
| 734 |
Qed. |
| 735 |
|
| 736 |
Lemma valid_expr_binop_bool_impl1 {t} |
| 737 |
rc (f : API.expr t) : |
| 738 |
valid_expr_binop_bool rc f = true -> |
| 739 |
(match t as t0 return expr.expr t0 -> Prop with |
| 740 |
| type.arrow type_Z (type.arrow type_Z type_Z) => |
| 741 |
fun f => |
| 742 |
forall x y : API.expr type_Z, |
| 743 |
valid_expr true x -> |
| 744 |
valid_expr true y -> |
| 745 |
valid_expr rc (expr.App (expr.App f x) y) |
| 746 |
| _ => fun _ => False |
| 747 |
end) f. |
| 748 |
Proof. |
| 749 |
cbv [valid_expr_binop_bool]. |
| 750 |
break_match; try congruence; [ | ]; |
| 751 |
intros; |
| 752 |
repeat match goal with |
| 753 |
| H : _ && _ = true |- _ => |
| 754 |
apply andb_true_iff in H; destruct H |
| 755 |
| H : negb ?rc = true |- _ => |
| 756 |
destruct rc; cbn [negb] in *; try congruence; [ ] |
| 757 |
| H : is_mul_high_ident_expr _ = true |- _ => |
| 758 |
apply is_mul_high_ident_expr_eq in H; subst |
| 759 |
end; [ | ]. |
| 760 |
{ constructor; eauto; congruence. } |
| 761 |
{ constructor; eauto; Z.ltb_to_lt; congruence. } |
| 762 |
Qed. |
| 763 |
|
| 764 |
Lemma is_shiftl_ident_eq {t} (i : ident.ident t) : |
| 765 |
is_shiftl_ident i = true -> |
| 766 |
(match t as t0 return ident.ident t0 -> Prop with |
| 767 |
| type.arrow type_Z (type.arrow type_Z (type.arrow type_Z type_Z)) => |
| 768 |
fun i => i = ident.Z_truncating_shiftl |
| 769 |
| _ => fun _ => False |
| 770 |
end) i. |
| 771 |
Proof. |
| 772 |
cbv [is_shiftl_ident]. |
| 773 |
break_match; congruence. |
| 774 |
Qed. |
| 775 |
|
| 776 |
Lemma is_shiftl_ident_expr_eq {t} (f : API.expr t) : |
| 777 |
is_shiftl_ident_expr f = true -> |
| 778 |
(match t as t0 return API.expr t0 -> Prop with |
| 779 |
| type.arrow type_Z (type.arrow type_Z (type.arrow type_Z type_Z)) => |
| 780 |
fun f => f = expr.Ident ident.Z_truncating_shiftl |
| 781 |
| _ => fun _ => False |
| 782 |
end) f. |
| 783 |
Proof. |
| 784 |
cbv [is_shiftl_ident_expr]. |
| 785 |
break_match; try congruence; [ ]. |
| 786 |
intros; |
| 787 |
match goal with |
| 788 |
| H : is_shiftl_ident _ = true |- _ => |
| 789 |
apply is_shiftl_ident_eq in H |
| 790 |
end. |
| 791 |
congruence. |
| 792 |
Qed. |
| 793 |
|
| 794 |
Lemma valid_shifter_eq {t} (x : API.expr t) : |
| 795 |
valid_shifter x = true -> |
| 796 |
(match t as t0 return expr.expr t0 -> Prop with |
| 797 |
| type_Z => |
| 798 |
fun x => |
| 799 |
exists n, |
| 800 |
is_bounded_by_bool n (@width_range width) = true /\ |
| 801 |
x = expr.Ident (ident.Literal (t:=base.type.Z) n) |
| 802 |
| _ => fun _ => False |
| 803 |
end) x. |
| 804 |
Proof. |
| 805 |
cbv [valid_shifter]. |
| 806 |
break_match; try congruence; [ ]. |
| 807 |
intros; eexists; eauto. |
| 808 |
Qed. |
| 809 |
|
| 810 |
Lemma valid_expr_shift_bool_impl1 {t} |
| 811 |
rc (f : API.expr t) : |
| 812 |
valid_expr_shift_bool rc f = true -> |
| 813 |
(match t as t0 return expr.expr t0 -> Prop with |
| 814 |
| type.arrow type_Z (type.arrow type_Z type_Z) => |
| 815 |
fun f => |
| 816 |
forall x y : API.expr type_Z, |
| 817 |
valid_expr true x -> |
| 818 |
valid_shifter y = true -> |
| 819 |
valid_expr rc (expr.App (expr.App f x) y) |
| 820 |
| _ => fun _ => False |
| 821 |
end) f. |
| 822 |
Proof. |
| 823 |
cbv [valid_expr_shift_bool]. |
| 824 |
break_match; try congruence; [ | | ]; |
| 825 |
intros; |
| 826 |
repeat match goal with |
| 827 |
| H : _ && _ = true |- _ => |
| 828 |
apply andb_true_iff in H; destruct H |
| 829 |
| H : negb ?rc = true |- _ => |
| 830 |
destruct rc; cbn [negb] in *; try congruence; [ ] |
| 831 |
| H : is_bounded_by_bool _ width_range = true |- _ => |
| 832 |
cbv [is_bounded_by_bool upper lower width_range] in H |
| 833 |
| H : is_shiftl_ident_expr _ = true |- _ => |
| 834 |
apply is_shiftl_ident_expr_eq in H; subst |
| 835 |
| H : valid_shifter _ = true |- _ => |
| 836 |
apply valid_shifter_eq in H; |
| 837 |
destruct H as [? [? ?] ]; subst |
| 838 |
end. |
| 839 |
{ Z.ltb_to_lt. |
| 840 |
constructor; eauto; lia. } |
| 841 |
{ Z.ltb_to_lt. |
| 842 |
constructor; eauto; lia. } |
| 843 |
{ Z.ltb_to_lt. |
| 844 |
constructor; eauto; lia. } |
| 845 |
Qed. |
| 846 |
|
| 847 |
Lemma is_literalz_impl1 {t} (e : API.expr t) (x : Z) : |
| 848 |
is_literalz e x = true -> |
| 849 |
(match t as t0 return expr.expr t0 -> Prop with |
| 850 |
| type_Z => |
| 851 |
fun e => e = expr.Ident (ident.Literal (t:=base.type.Z) x) |
| 852 |
| _ => fun _ => False |
| 853 |
end) e. |
| 854 |
Proof. |
| 855 |
cbv [is_literalz]. |
| 856 |
break_match; try congruence; intros. |
| 857 |
Z.ltb_to_lt; subst; congruence. |
| 858 |
Qed. |
| 859 |
|
| 860 |
Lemma is_opp_ident_eq {t} (i : ident.ident t) : |
| 861 |
is_opp_ident i = true -> |
| 862 |
(match t as t0 return ident.ident t0 -> Prop with |
| 863 |
| type.arrow type_Z type_Z => fun i => i = ident.Z_opp |
| 864 |
| _ => fun _ => False |
| 865 |
end) i. |
| 866 |
Proof. |
| 867 |
cbv [is_opp_ident]. break_match; congruence. |
| 868 |
Qed. |
| 869 |
|
| 870 |
Lemma is_opp_ident_expr_eq {t} (f : API.expr t) : |
| 871 |
is_opp_ident_expr f = true -> |
| 872 |
(match t as t0 return API.expr t0 -> Prop with |
| 873 |
| type.arrow type_Z type_Z => |
| 874 |
fun f => f = expr.Ident ident.Z_opp |
| 875 |
| _ => fun _ => False |
| 876 |
end) f. |
| 877 |
Proof. |
| 878 |
cbv [is_opp_ident_expr]. |
| 879 |
break_match; try congruence; [ ]. |
| 880 |
intros; |
| 881 |
match goal with |
| 882 |
| H : is_opp_ident _ = true |- _ => |
| 883 |
apply is_opp_ident_eq in H |
| 884 |
end. |
| 885 |
congruence. |
| 886 |
Qed. |
| 887 |
|
| 888 |
Lemma valid_expr_select_bool_impl1 {t} |
| 889 |
rc (f : API.expr t) : |
| 890 |
valid_expr_select_bool rc f = true -> |
| 891 |
(match t as t0 return expr.expr t0 -> Prop with |
| 892 |
| type.arrow type_Z (type.arrow type_Z (type.arrow type_Z type_Z)) => |
| 893 |
fun f => |
| 894 |
forall c x y : API.expr type_Z, |
| 895 |
valid_expr true c -> |
| 896 |
is_literalz x 0 = true -> |
| 897 |
is_literalz y (2^width-1) = true -> |
| 898 |
valid_expr rc |
| 899 |
(expr.App |
| 900 |
(expr.App |
| 901 |
(expr.App f c) x) y) |
| 902 |
| _ => fun _ => False |
| 903 |
end) f. |
| 904 |
Proof. |
| 905 |
cbv [valid_expr_select_bool]. |
| 906 |
break_match; try congruence; [ ]; intros. |
| 907 |
repeat match goal with |
| 908 |
| H : negb ?rc = true |- _ => |
| 909 |
destruct rc; cbn [negb] in *; try congruence; [ ] |
| 910 |
| H : is_literalz _ _ = true |- _ => |
| 911 |
apply is_literalz_impl1 in H |
| 912 |
end. |
| 913 |
subst; constructor; eauto. |
| 914 |
Qed. |
| 915 |
|
| 916 |
Lemma valid_lnot_modulus_eq {t} (x : API.expr t) : |
| 917 |
valid_lnot_modulus x = true -> |
| 918 |
(match t as t0 return expr.expr t0 -> Prop with |
| 919 |
| type_Z => |
| 920 |
fun x => |
| 921 |
exists n, |
| 922 |
is_bounded_by_bool (n-1) (@max_range width) = true |
| 923 |
/\ n = 2 ^ Z.log2 n |
| 924 |
/\ x = expr.Ident (ident.Literal (t:=base.type.Z) n) |
| 925 |
| _ => fun _ => False |
| 926 |
end) x. |
| 927 |
Proof. |
| 928 |
cbv [valid_lnot_modulus]. |
| 929 |
break_match; try congruence; [ ]. |
| 930 |
intros; eexists; repeat split; eauto. |
| 931 |
Qed. |
| 932 |
|
| 933 |
Lemma valid_expr_lnot_modulo_bool_impl1 {t} |
| 934 |
rc (f : API.expr t) : |
| 935 |
valid_expr_lnot_modulo_bool rc f = true -> |
| 936 |
(match t as t0 return expr.expr t0 -> Prop with |
| 937 |
| type.arrow type_Z (type.arrow type_Z type_Z) => |
| 938 |
fun f => |
| 939 |
forall x y : API.expr type_Z, |
| 940 |
valid_expr true x -> |
| 941 |
valid_lnot_modulus y = true -> |
| 942 |
valid_expr rc (expr.App (expr.App f x) y) |
| 943 |
| _ => fun _ => False |
| 944 |
end) f. |
| 945 |
Proof. |
| 946 |
cbv [valid_expr_lnot_modulo_bool]. |
| 947 |
break_match; try congruence; [ ]. |
| 948 |
intros; |
| 949 |
repeat match goal with |
| 950 |
| H : _ && _ = true |- _ => |
| 951 |
apply andb_true_iff in H; destruct H |
| 952 |
| H : negb ?rc = true |- _ => |
| 953 |
destruct rc; cbn [negb] in *; try congruence; [ ] |
| 954 |
| H : valid_lnot_modulus _ = true |- _ => |
| 955 |
apply valid_lnot_modulus_eq in H; |
| 956 |
destruct H as [? [? [? ?] ] ]; subst |
| 957 |
end. |
| 958 |
constructor; eauto. |
| 959 |
Qed. |
| 960 |
|
| 961 |
Lemma valid_expr_bool'_impl1 {t} (e : API.expr t) : |
| 962 |
forall mode rc, |
| 963 |
valid_expr_bool' mode rc e = true -> |
| 964 |
match mode with |
| 965 |
| Binop => |
| 966 |
(match t as t0 return expr.expr t0 -> Prop with |
| 967 |
| type.arrow type_Z type_Z => |
| 968 |
fun f => |
| 969 |
forall x, |
| 970 |
valid_expr true x -> |
| 971 |
valid_expr rc (expr.App f x) |
| 972 |
| _ => fun _ => False |
| 973 |
end) e |
| 974 |
| Shift => |
| 975 |
(match t as t0 return expr.expr t0 -> Prop with |
| 976 |
| type.arrow type_Z type_Z => |
| 977 |
fun f => |
| 978 |
forall x, |
| 979 |
valid_shifter x = true -> |
| 980 |
valid_expr rc (expr.App f x) |
| 981 |
| _ => fun _ => False |
| 982 |
end) e |
| 983 |
| Select => |
| 984 |
(match t as t0 return expr.expr t0 -> Prop with |
| 985 |
| type.arrow type_Z type_Z => |
| 986 |
fun f => |
| 987 |
forall x, |
| 988 |
is_literalz x (2^width-1) = true -> |
| 989 |
valid_expr rc (expr.App f x) |
| 990 |
| type.arrow type_Z (type.arrow type_Z type_Z) => |
| 991 |
fun f => |
| 992 |
forall x y, |
| 993 |
is_literalz x 0 = true -> |
| 994 |
is_literalz y (2^width-1) = true -> |
| 995 |
valid_expr rc (expr.App (expr.App f x) y) |
| 996 |
| type.arrow |
| 997 |
type_Z |
| 998 |
(type.arrow type_Z (type.arrow type_Z type_Z)) => |
| 999 |
fun f => |
| 1000 |
forall c x y, |
| 1001 |
valid_expr true c -> |
| 1002 |
valid_expr_select_bool rc f = true -> |
| 1003 |
is_literalz x 0 = true -> |
| 1004 |
is_literalz y (2^width-1) = true -> |
| 1005 |
valid_expr rc (expr.App (expr.App (expr.App f c) x) y) |
| 1006 |
| _ => fun _ => False |
| 1007 |
end) e |
| 1008 |
| Lnot => |
| 1009 |
(match t as t0 return expr.expr t0 -> Prop with |
| 1010 |
| type.arrow type_Z type_Z => |
| 1011 |
fun f => |
| 1012 |
forall x, |
| 1013 |
valid_lnot_modulus x = true -> |
| 1014 |
valid_expr rc (expr.App f x) |
| 1015 |
| _ => fun _ => False |
| 1016 |
end) e |
| 1017 |
| NotPartial => (exists b, t = type.base b) -> valid_expr rc e |
| 1018 |
end. |
| 1019 |
Proof. |
| 1020 |
induction e; intros; |
| 1021 |
cbn [valid_expr_bool'] in *; |
| 1022 |
[ | | | | ]. |
| 1023 |
{ break_match_hyps; try congruence. |
| 1024 |
{ constructor. |
| 1025 |
repeat match goal with |
| 1026 |
| _ => progress cbn [orb] |
| 1027 |
| H : _ || _ = true |- _ => |
| 1028 |
apply orb_true_iff in H; destruct H |
| 1029 |
| H : _ = true |- _ => rewrite H |
| 1030 |
| _ => rewrite Bool.orb_true_r |
| 1031 |
| _ => reflexivity |
| 1032 |
end. } |
| 1033 |
{ destruct rc; cbn [negb] in *; try congruence. |
| 1034 |
constructor. } } |
| 1035 |
{ break_match_hyps; try congruence. |
| 1036 |
{ constructor. } |
| 1037 |
{ constructor. } } |
| 1038 |
{ break_match_hyps; congruence. } |
| 1039 |
{ remember s. |
| 1040 |
remember d. |
| 1041 |
break_match_hyps; try congruence; |
| 1042 |
repeat match goal with |
| 1043 |
| H : _ && _ = true |- _ => |
| 1044 |
apply andb_true_iff in H; destruct H |
| 1045 |
| H: valid_expr_App1_bool _ _ = true |- _ => |
| 1046 |
apply valid_expr_App1_bool_type in H; |
| 1047 |
destruct H; destruct H; congruence |
| 1048 |
| IH : forall mode _ _, |
| 1049 |
match mode with |
| 1050 |
| NotPartial => _ |
| 1051 |
| Binop => False |
| 1052 |
| Shift => False |
| 1053 |
| Select => False |
| 1054 |
| Lnot => False |
| 1055 |
end |- _ => |
| 1056 |
specialize (IH NotPartial); (cbn match in IH) |
| 1057 |
end. |
| 1058 |
{ (* fully-applied binop case *) |
| 1059 |
intros. apply (IHe1 Binop); eauto. } |
| 1060 |
{ (* fully-applied shift case *) |
| 1061 |
intros. apply (IHe1 Shift); eauto. } |
| 1062 |
{ (* fully-applied select case *) |
| 1063 |
intros. apply (IHe1 Select); eauto. } |
| 1064 |
{ (* fully-applied lnot_modulo case *) |
| 1065 |
intros. apply (IHe1 Lnot); eauto. } |
| 1066 |
{ (* opp case *) |
| 1067 |
intros. |
| 1068 |
repeat match goal with |
| 1069 |
| H : is_opp_ident_expr _ = true |- _ => |
| 1070 |
apply is_opp_ident_expr_eq in H; subst |
| 1071 |
| H : negb ?rc = true |- _ => |
| 1072 |
destruct rc; cbn [negb] in *; try congruence; [ ] |
| 1073 |
end. |
| 1074 |
econstructor. |
| 1075 |
eauto. } |
| 1076 |
{ (* cast Z case *) |
| 1077 |
intros. |
| 1078 |
apply (valid_expr_App1_bool_impl1 |
| 1079 |
(t := type_Z -> type_Z)); eauto. } |
| 1080 |
{ (* nth_default case *) |
| 1081 |
eauto using valid_expr_nth_default_bool_impl1. } |
| 1082 |
{ (* fst/snd *) |
| 1083 |
intros. |
| 1084 |
apply (valid_expr_App1_bool_impl1 |
| 1085 |
(t := type_ZZ -> type_Z)); eauto. } |
| 1086 |
{ (* cast ZZ *) |
| 1087 |
intros. |
| 1088 |
apply (valid_expr_App1_bool_impl1 |
| 1089 |
(t := type_ZZ -> type_ZZ)); eauto. } |
| 1090 |
{ (* partially-applied binop case *) |
| 1091 |
intros. |
| 1092 |
apply (valid_expr_binop_bool_impl1 |
| 1093 |
(t:=type_Z -> type_Z -> type_Z)); eauto. } |
| 1094 |
{ (* partially-applied shift case *) |
| 1095 |
intros. |
| 1096 |
apply (valid_expr_shift_bool_impl1 |
| 1097 |
(t:=type_Z -> type_Z -> type_Z)); eauto. } |
| 1098 |
{ (* partially-applied select case (last 2 arguments) *) |
| 1099 |
intros. apply (IHe1 Select); eauto. } |
| 1100 |
{ (* partially-applied select case (all 3 arguments) *) |
| 1101 |
intros. |
| 1102 |
apply (valid_expr_select_bool_impl1 |
| 1103 |
(t:=type_Z -> type_Z -> type_Z -> type_Z)); eauto. } |
| 1104 |
{ (* partially-applied lnot_modulo case *) |
| 1105 |
intros. |
| 1106 |
apply (valid_expr_lnot_modulo_bool_impl1 |
| 1107 |
(t:=type_Z -> type_Z -> type_Z)); eauto. } } |
| 1108 |
{ break_match; try congruence. } |
| 1109 |
Qed. |
| 1110 |
|
| 1111 |
Lemma valid_expr_bool_impl1 {t} (e : API.expr t) : |
| 1112 |
(exists b, t = type.base b) -> |
| 1113 |
forall rc, |
| 1114 |
valid_expr_bool rc e = true -> valid_expr rc e. |
| 1115 |
Proof. |
| 1116 |
cbv [valid_expr_bool]; intros. |
| 1117 |
match goal with H : _ = true |- _ => |
| 1118 |
apply valid_expr_bool'_impl1 in H end. |
| 1119 |
eauto. |
| 1120 |
Qed. |
| 1121 |
|
| 1122 |
Lemma valid_expr_bool_impl2 {t} (e : API.expr t) : |
| 1123 |
forall rc, |
| 1124 |
valid_expr rc e -> |
| 1125 |
valid_expr_bool rc e = true. |
| 1126 |
Proof. |
| 1127 |
cbv [valid_expr_bool]. |
| 1128 |
induction 1; intros; subst; cbn; |
| 1129 |
repeat match goal with |
| 1130 |
| _ => progress cbn [andb] |
| 1131 |
| H : valid_expr_bool' _ _ _ = true |- _ => |
| 1132 |
rewrite H |
| 1133 |
| _ => rewrite Z.eqb_refl |
| 1134 |
end; |
| 1135 |
auto using |
| 1136 |
Bool.andb_true_iff, Bool.orb_true_iff, |
| 1137 |
is_bounded_by_bool_max_range, |
| 1138 |
is_bounded_by_bool_width_range; [ | | ]. |
| 1139 |
{ (* lnot_modulo *) |
| 1140 |
apply Bool.andb_true_iff; split; |
| 1141 |
Z.ltb_to_lt; auto. } |
| 1142 |
{ (* select *) |
| 1143 |
break_match; |
| 1144 |
repeat match goal with |
| 1145 |
| H : (_ && _)%bool = true |- _ => |
| 1146 |
apply Bool.andb_true_iff in H; destruct H |
| 1147 |
end; congruence. } |
| 1148 |
{ (* binop *) |
| 1149 |
match goal with |
| 1150 |
| H : translate_binop ?i <> None |
| 1151 |
|- context [match translate_binop ?i with _ => _ end] => |
| 1152 |
destruct (translate_binop i) |
| 1153 |
end; cbn [andb]; congruence. } |
| 1154 |
Qed. |
| 1155 |
|
| 1156 |
Lemma valid_expr_bool_iff {t} (e : API.expr (type.base t)) : |
| 1157 |
forall rc, |
| 1158 |
valid_expr_bool rc e = true <-> valid_expr rc e. |
| 1159 |
Proof. |
| 1160 |
split; eauto using valid_expr_bool_impl1, valid_expr_bool_impl2. |
| 1161 |
Qed. |
| 1162 |
End Expr. |