| 1 |
Require Import Rewriter.Language.Language. |
| 2 |
Require Import Rewriter.Language.Inversion. |
| 3 |
Require Import Crypto.Language.API. |
| 4 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 5 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 6 |
Require Import Crypto.Util.Option. |
| 7 |
Require Import Crypto.Util.Prod. |
| 8 |
Require Import Crypto.Util.Sigma. |
| 9 |
Require Import Crypto.Util.HProp. |
| 10 |
|
| 11 |
Module Compilers. |
| 12 |
Import Language.Compilers. |
| 13 |
Import Inversion.Compilers. |
| 14 |
Import Language.API.Compilers. |
| 15 |
Import Compilers.API. |
| 16 |
|
| 17 |
Module expr. |
| 18 |
Import Language.API.Compilers.invert_expr. |
| 19 |
|
| 20 |
Local Notation tZ := (base.type.type_base base.type.Z). |
| 21 |
Local Notation tzrange := (base.type.type_base base.type.zrange). |
| 22 |
|
| 23 |
Local Lemma diff_None_Some {T v P} : @None T = Some v -> P. |
| 24 |
Proof. discriminate. Qed. |
| 25 |
|
| 26 |
Lemma invert_Z_cast_Z_cast {var} (r : ZRange.zrange) |
| 27 |
: invert_Z_cast (var:=var) (#ident.Z_cast @ ##r) = Some r. |
| 28 |
Proof. destruct_head'_prod; reflexivity. Qed. |
| 29 |
|
| 30 |
Lemma invert_Z_cast_Some_match_type {var t} (e : @expr var t) r |
| 31 |
: invert_Z_cast e = Some r |
| 32 |
-> match t return expr t -> Prop with |
| 33 |
| (type.base tZ -> type.base tZ) |
| 34 |
=> fun e => e = (#ident.Z_cast @ ##r)%expr |
| 35 |
| _ => fun _ => False |
| 36 |
end%etype e. |
| 37 |
Proof. |
| 38 |
cbv [invert_Z_cast Option.bind]. |
| 39 |
edestruct invert_AppIdent eqn:?; expr.invert_subst; try exact diff_None_Some; []. |
| 40 |
destruct_head'_sigT; destruct_head'_prod; cbn [fst snd projT1 projT2] in *. |
| 41 |
repeat (break_innermost_match_step; try exact diff_None_Some; []). |
| 42 |
edestruct reflect_smart_Literal eqn:?; expr.invert_subst; try exact diff_None_Some; []. |
| 43 |
ident.invert_for ident; subst; inversion_type; cbn [f_equal2 eq_rect] in *; try exact diff_None_Some; []. |
| 44 |
now intro; inversion_option; subst; inversion_prod; subst. |
| 45 |
Qed. |
| 46 |
|
| 47 |
Lemma invert_Z_cast_Some {var t} (e : @expr var t) r |
| 48 |
: invert_Z_cast e = Some r -> existT (@expr var) t e = existT (@expr var) _ (#ident.Z_cast @ ##r)%expr. |
| 49 |
Proof. |
| 50 |
intro H; apply invert_Z_cast_Some_match_type in H; |
| 51 |
now break_innermost_match_hyps; subst. |
| 52 |
Qed. |
| 53 |
|
| 54 |
Lemma invert_Z_cast_Some_Z {var} (e : @expr var (tZ -> tZ)) r |
| 55 |
: invert_Z_cast e = Some r -> e = (#ident.Z_cast @ ##r)%expr. |
| 56 |
Proof. apply (@invert_Z_cast_Some_match_type var _ e). Qed. |
| 57 |
|
| 58 |
Lemma invert_Z_cast2_Z_cast2 {var} (r : ZRange.zrange * ZRange.zrange) |
| 59 |
: invert_Z_cast2 (var:=var) (#ident.Z_cast2 @ ##r) = Some r. |
| 60 |
Proof. destruct_head'_prod; reflexivity. Qed. |
| 61 |
|
| 62 |
Lemma invert_Z_cast2_Some_match_type {var t} (e : @expr var t) r |
| 63 |
: invert_Z_cast2 e = Some r |
| 64 |
-> match t return expr t -> Prop with |
| 65 |
| (type.base (tZ * tZ) -> type.base (tZ * tZ)) |
| 66 |
=> fun e => e = (#ident.Z_cast2 @ ##r)%expr |
| 67 |
| _ => fun _ => False |
| 68 |
end%etype e. |
| 69 |
Proof. |
| 70 |
cbv [invert_Z_cast2 Option.bind]. |
| 71 |
edestruct invert_AppIdent eqn:?; expr.invert_subst; try exact diff_None_Some; []. |
| 72 |
destruct_head'_sigT; destruct_head'_prod; cbn [fst snd projT1 projT2] in *. |
| 73 |
repeat (break_innermost_match_step; try exact diff_None_Some; []). |
| 74 |
edestruct reflect_smart_Literal eqn:?; expr.invert_subst; try exact diff_None_Some; []. |
| 75 |
ident.invert_for ident; subst; inversion_type; cbn [f_equal2 eq_rect] in *; try exact diff_None_Some; []. |
| 76 |
now intro; inversion_option; subst; inversion_prod; subst. |
| 77 |
Qed. |
| 78 |
|
| 79 |
Lemma invert_Z_cast2_Some {var t} (e : @expr var t) r |
| 80 |
: invert_Z_cast2 e = Some r -> existT (@expr var) t e = existT (@expr var) _ (#ident.Z_cast2 @ ##r)%expr. |
| 81 |
Proof. |
| 82 |
intro H; apply invert_Z_cast2_Some_match_type in H; |
| 83 |
now break_innermost_match_hyps; subst. |
| 84 |
Qed. |
| 85 |
|
| 86 |
Lemma invert_Z_cast2_Some_ZZ {var} (e : @expr var (tZ * tZ -> tZ * tZ)) r |
| 87 |
: invert_Z_cast2 e = Some r -> e = (#ident.Z_cast2 @ ##r)%expr. |
| 88 |
Proof. apply (@invert_Z_cast2_Some_match_type var _ e). Qed. |
| 89 |
End expr. |
| 90 |
End Compilers. |