| 1 |
From Coq Require Import String. |
| 2 |
From Coq Require Import PeanoNat. |
| 3 |
From Coq Require Import List. |
| 4 |
From Coq Require Import Lia. |
| 5 |
Require Import bedrock2.Syntax. |
| 6 |
Require Import bedrock2.Map.Separation. |
| 7 |
Require Import bedrock2.Map.SeparationLogic. |
| 8 |
Require Import coqutil.Word.Interface coqutil.Word.Properties. |
| 9 |
Require Import coqutil.Map.Interface coqutil.Map.Properties. |
| 10 |
Require Import coqutil.Datatypes.List. |
| 11 |
Require Import coqutil.Datatypes.PropSet. |
| 12 |
Require Import Crypto.Bedrock.Field.Common.Tactics. |
| 13 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 14 |
Require Import Crypto.Language.API. |
| 15 |
Require Import Crypto.Bedrock.Field.Common.Util. |
| 16 |
Require Import Crypto.Bedrock.Field.Translation.Flatten. |
| 17 |
Require Import Crypto.Bedrock.Field.Translation.LoadStoreList. |
| 18 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.Equivalence. |
| 19 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.EquivalenceProperties. |
| 20 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.UsedVarnames. |
| 21 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.VarnameSet. |
| 22 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 23 |
Import ListNotations. Local Open Scope Z_scope. |
| 24 |
|
| 25 |
Import API.Compilers. |
| 26 |
Import Types.Notations. |
| 27 |
|
| 28 |
(* For argument and return variable names, fiat-crypto expects a nested |
| 29 |
structure while bedrock2 expects flat lists; this file contains mechanical |
| 30 |
conversions between the two. *) |
| 31 |
|
| 32 |
Section Flatten. |
| 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 |
(* these conversions should happen before loading arguments and after |
| 38 |
storing return values, so they use in-memory lists *) |
| 39 |
Local Existing Instance rep.listZ_mem. |
| 40 |
Local Existing Instance rep.Z. |
| 41 |
|
| 42 |
Lemma flatten_base_samelength {t} |
| 43 |
(names : base_ltype t) |
| 44 |
(value : base.interp t) : |
| 45 |
forall (words : list word) s R mem, |
| 46 |
sep (equivalent_flat_base value words s) R mem -> |
| 47 |
length words = length (flatten_base_ltype names). |
| 48 |
Proof. |
| 49 |
induction t; cbn [flatten_base_ltype equivalent_flat_base]; |
| 50 |
break_match; |
| 51 |
repeat match goal with |
| 52 |
| _ => progress (intros; subst) |
| 53 |
| _ => progress sepsimpl |
| 54 |
| _ => rewrite app_length |
| 55 |
| _ => solve [eauto] |
| 56 |
end. |
| 57 |
erewrite <-IHt1, <-IHt2 by ecancel_assumption. |
| 58 |
rewrite skipn_length, firstn_length. |
| 59 |
apply Nat.min_case_strong; intros; lia. |
| 60 |
Qed. |
| 61 |
|
| 62 |
(* given these structures have the same type, they'll have the same size in |
| 63 |
flattened form *) |
| 64 |
Lemma flatten_args_samelength {t} |
| 65 |
(argnames : type.for_each_lhs_of_arrow ltype t) |
| 66 |
(args : type.for_each_lhs_of_arrow API.interp_type t) : |
| 67 |
forall (flat_args : list word) s mem, |
| 68 |
equivalent_flat_args args flat_args s mem -> |
| 69 |
length flat_args = length (flatten_argnames argnames). |
| 70 |
Proof. |
| 71 |
induction t; |
| 72 |
repeat match goal with |
| 73 |
| _ => progress (intros; subst) |
| 74 |
| _ => progress cbn [equivalent_flat_args |
| 75 |
equivalent_flat_base |
| 76 |
flatten_argnames] in * |
| 77 |
| _ => progress break_match |
| 78 |
| _ => progress sepsimpl |
| 79 |
| _ => rewrite app_length |
| 80 |
| _ => solve [eauto] |
| 81 |
end. |
| 82 |
erewrite <-IHt2, <-flatten_base_samelength by ecancel_assumption. |
| 83 |
rewrite skipn_length, firstn_length. |
| 84 |
apply Nat.min_case_strong; intros; lia. |
| 85 |
Qed. |
| 86 |
|
| 87 |
Lemma of_list_zip_flatten_argnames {t} |
| 88 |
(argnames : type.for_each_lhs_of_arrow ltype t) |
| 89 |
(args : type.for_each_lhs_of_arrow API.interp_type t) |
| 90 |
(flat_args : list word) s : forall (mem : mem), |
| 91 |
equivalent_flat_args args flat_args s mem -> |
| 92 |
(exists l, |
| 93 |
map.of_list_zip (flatten_argnames argnames) flat_args = Some l). |
| 94 |
Proof. |
| 95 |
intros. apply map.sameLength_putmany_of_list. |
| 96 |
erewrite flatten_args_samelength; eauto. |
| 97 |
Qed. |
| 98 |
|
| 99 |
Lemma varname_set_flatten {t} (names : base_ltype t) : |
| 100 |
PropSet.sameset (varname_set_base names) |
| 101 |
(PropSet.of_list (flatten_base_ltype names)). |
| 102 |
Proof. |
| 103 |
apply sameset_iff. |
| 104 |
induction t; |
| 105 |
cbn [varname_set_base |
| 106 |
flatten_base_ltype rep.varname_set rep.Z rep.listZ_mem]; |
| 107 |
break_match; |
| 108 |
cbv [PropSet.singleton_set |
| 109 |
PropSet.of_list PropSet.union PropSet.elem_of]; |
| 110 |
cbn [In]; try tauto; [ ]. |
| 111 |
intros. rewrite in_app_iff, IHt1, IHt2. |
| 112 |
cbv [PropSet.of_list]. reflexivity. |
| 113 |
Qed. |
| 114 |
|
| 115 |
Lemma varname_set_args_flatten {t} |
| 116 |
(argnames : type.for_each_lhs_of_arrow ltype t) : |
| 117 |
PropSet.sameset (varname_set_args argnames) |
| 118 |
(PropSet.of_list (flatten_argnames argnames)). |
| 119 |
Proof. |
| 120 |
revert argnames; induction t; intros; |
| 121 |
cbn [varname_set_args flatten_argnames]; |
| 122 |
break_match; rewrite ?of_list_nil; try reflexivity; [ ]. |
| 123 |
rewrite PropSet.of_list_app. |
| 124 |
rewrite varname_set_flatten, IHt2. |
| 125 |
reflexivity. |
| 126 |
Qed. |
| 127 |
|
| 128 |
Lemma flatten_listonly_subset {t} names : |
| 129 |
PropSet.subset |
| 130 |
(PropSet.of_list (flatten_listonly_base_ltype (t:=t) |
| 131 |
(fst (extract_listnames names)))) |
| 132 |
(varname_set_base names). |
| 133 |
Proof. |
| 134 |
induction t; |
| 135 |
cbn [fst snd varname_set extract_listnames |
| 136 |
flatten_listonly_base_ltype]; |
| 137 |
break_match; intros; cbn [fst snd]; |
| 138 |
eauto using subset_empty_l; [ | ]. |
| 139 |
{ rewrite PropSet.of_list_app. |
| 140 |
eauto using subset_union_l, subset_union_rr, subset_union_rl. } |
| 141 |
{ rewrite of_list_singleton. reflexivity. } |
| 142 |
Qed. |
| 143 |
|
| 144 |
Lemma flatten_listonly_sublist {t} names : |
| 145 |
forall x, |
| 146 |
In x (flatten_listonly_base_ltype (fst (extract_listnames names))) -> |
| 147 |
In x (flatten_base_ltype (t:=t) names). |
| 148 |
Proof. |
| 149 |
intros. |
| 150 |
pose proof (flatten_listonly_subset names) as Hsubset. |
| 151 |
rewrite varname_set_flatten in Hsubset. |
| 152 |
apply Hsubset. assumption. |
| 153 |
Qed. |
| 154 |
|
| 155 |
Lemma flatten_listonly_NoDup {t} names : |
| 156 |
NoDup (flatten_base_ltype (t:=t) names) -> |
| 157 |
NoDup (flatten_listonly_base_ltype (fst (extract_listnames names))). |
| 158 |
Proof. |
| 159 |
induction t; |
| 160 |
cbn [extract_listnames fst snd flatten_base_ltype |
| 161 |
flatten_listonly_base_ltype]; |
| 162 |
intros; break_match; |
| 163 |
repeat match goal with |
| 164 |
| |- NoDup [] => constructor |
| 165 |
| |- NoDup (_ :: _) => constructor |
| 166 |
| H : NoDup (_ :: _) |- _ => inversion H; subst; clear H |
| 167 |
| H : NoDup (_ ++ _) |- _ => |
| 168 |
apply NoDup_app_iff in H; cleanup |
| 169 |
| |- ~ (In _ []) => cbv [In]; tauto |
| 170 |
end; [ ]. |
| 171 |
apply NoDup_app_iff; repeat split; eauto; repeat intro; |
| 172 |
repeat match goal with |
| 173 |
| H : In _ (flatten_listonly_base_ltype _) |- _ => |
| 174 |
apply flatten_listonly_sublist in H |
| 175 |
| H : context [~ In _ _] |- _ => |
| 176 |
eapply H; solve [eauto] |
| 177 |
end. |
| 178 |
Qed. |
| 179 |
|
| 180 |
Lemma varname_set_flatten_listonly {t} (names : _ t) : |
| 181 |
PropSet.sameset |
| 182 |
(PropSet.of_list |
| 183 |
(flatten_listonly_base_ltype (fst (extract_listnames names)))) |
| 184 |
(varname_set_listonly names). |
| 185 |
Proof. |
| 186 |
induction t; |
| 187 |
cbn [extract_listnames fst snd varname_set_listonly |
| 188 |
flatten_listonly_base_ltype]; |
| 189 |
break_match; intros; rewrite ?of_list_nil; try reflexivity; |
| 190 |
[ | ]. |
| 191 |
{ rewrite PropSet.of_list_app. |
| 192 |
rewrite IHt1, IHt2. reflexivity. } |
| 193 |
{ rewrite of_list_singleton. reflexivity. } |
| 194 |
Qed. |
| 195 |
|
| 196 |
Lemma varname_set_flatten_listexcl {t} (names : _ t) : |
| 197 |
PropSet.sameset |
| 198 |
(PropSet.of_list |
| 199 |
(flatten_listexcl_base_ltype (snd (extract_listnames names)))) |
| 200 |
(varname_set_listexcl names). |
| 201 |
Proof. |
| 202 |
induction t; |
| 203 |
cbn [extract_listnames fst snd varname_set_listexcl |
| 204 |
flatten_listexcl_base_ltype]; |
| 205 |
break_match; intros; cbn [fst snd]; |
| 206 |
rewrite ?of_list_singleton; try reflexivity; [ ]. |
| 207 |
rewrite PropSet.of_list_app. |
| 208 |
rewrite IHt1, IHt2. |
| 209 |
reflexivity. |
| 210 |
Qed. |
| 211 |
|
| 212 |
Lemma flatten_listonly_flatten_listexcl_disjoint {t} (names : _ t) : |
| 213 |
NoDup (flatten_base_ltype names) -> |
| 214 |
PropSet.disjoint |
| 215 |
(PropSet.of_list |
| 216 |
(flatten_listonly_base_ltype (fst (extract_listnames names)))) |
| 217 |
(PropSet.of_list |
| 218 |
(flatten_listexcl_base_ltype (snd (extract_listnames names)))). |
| 219 |
Proof. |
| 220 |
induction t; |
| 221 |
cbn [extract_listnames fst snd flatten_base_ltype |
| 222 |
flatten_listexcl_base_ltype |
| 223 |
flatten_listonly_base_ltype]; |
| 224 |
intros; break_match; |
| 225 |
try match goal with |
| 226 |
| H : NoDup (_ ++ _) |- _ => |
| 227 |
let H' := fresh in |
| 228 |
pose proof H as H'; apply NoDup_disjoint in H |
| 229 |
end; |
| 230 |
repeat match goal with |
| 231 |
| H : NoDup (_ ++ _) |- _ => |
| 232 |
apply NoDup_app_iff in H; cleanup |
| 233 |
| _ => rewrite of_list_nil |
| 234 |
| _ => rewrite PropSet.of_list_app |
| 235 |
| _ => solve [eauto using disjoint_empty_l, disjoint_empty_r] |
| 236 |
end; [ ]. |
| 237 |
apply disjoint_union_l_iff; split; |
| 238 |
(apply disjoint_union_r_iff; split; eauto; [ ]); |
| 239 |
repeat match goal with |
| 240 |
| H : _ |- _ => erewrite <-!varname_set_flatten in H |
| 241 |
| H : _ |- _ => erewrite varname_set_listonly_listexcl in H |
| 242 |
| H : PropSet.disjoint (PropSet.union _ _) _ |- _ => |
| 243 |
apply disjoint_union_l_iff in H; cleanup |
| 244 |
| H : PropSet.disjoint _ (PropSet.union _ _) |- _ => |
| 245 |
apply disjoint_union_r_iff in H; cleanup |
| 246 |
| _ => rewrite varname_set_flatten_listonly |
| 247 |
| _ => rewrite varname_set_flatten_listexcl |
| 248 |
| _ => solve [eauto] |
| 249 |
| _ => symmetry; solve [eauto] |
| 250 |
end. |
| 251 |
Qed. |
| 252 |
|
| 253 |
Lemma flatten_listonly_disjoint {t} (names : _ t) : |
| 254 |
NoDup (flatten_base_ltype names) -> |
| 255 |
PropSet.disjoint |
| 256 |
(PropSet.of_list |
| 257 |
(flatten_listonly_base_ltype (fst (extract_listnames names)))) |
| 258 |
(varname_set_listexcl names). |
| 259 |
Proof. |
| 260 |
intros. rewrite <-varname_set_flatten_listexcl. |
| 261 |
eauto using flatten_listonly_flatten_listexcl_disjoint. |
| 262 |
Qed. |
| 263 |
|
| 264 |
Lemma flatten_listonly_samelength {t} |
| 265 |
(names : listonly_base_ltype t) (value : base.interp t) : |
| 266 |
forall (words : list word) s R mem, |
| 267 |
sep (equivalent_listonly_flat_base value words s) R mem -> |
| 268 |
length words = length (flatten_listonly_base_ltype names). |
| 269 |
Proof. |
| 270 |
induction t; |
| 271 |
cbn [flatten_listonly_base_ltype equivalent_listonly_flat_base |
| 272 |
equivalent_flat_base]; |
| 273 |
break_match; |
| 274 |
repeat match goal with |
| 275 |
| _ => progress (intros; subst) |
| 276 |
| _ => progress sepsimpl |
| 277 |
| _ => rewrite app_length |
| 278 |
| _ => solve [eauto] |
| 279 |
end. |
| 280 |
erewrite <-IHt1, <-IHt2 by ecancel_assumption. |
| 281 |
rewrite skipn_length, firstn_length. |
| 282 |
apply Nat.min_case_strong; intros; lia. |
| 283 |
Qed. |
| 284 |
End Flatten. |