| 1 |
From Coq Require Import String. |
| 2 |
Require Import bedrock2.Syntax. |
| 3 |
Require Import bedrock2.Map.Separation. |
| 4 |
Require Import bedrock2.Map.SeparationLogic. |
| 5 |
Require Import coqutil.Map.Interface coqutil.Map.Properties. |
| 6 |
Require Import coqutil.Word.Interface coqutil.Word.Properties. |
| 7 |
Require Import coqutil.Datatypes.PropSet. |
| 8 |
From Coq Require Import List. (* after SeparationLogic *) |
| 9 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 10 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.Equivalence. |
| 11 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.UsedVarnames. |
| 12 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.VarnameSet. |
| 13 |
Require Import Crypto.Bedrock.Field.Common.Tactics. |
| 14 |
Require Import Crypto.Bedrock.Field.Common.Util. |
| 15 |
Require Import Crypto.Language.API. |
| 16 |
Require Import Crypto.Util.ListUtil. |
| 17 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 18 |
|
| 19 |
Import API.Compilers. |
| 20 |
Import ListNotations Types.Notations. |
| 21 |
|
| 22 |
Section OnlyDiffer. |
| 23 |
Context |
| 24 |
{width BW word mem locals ext_spec varname_gen error} |
| 25 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}. |
| 26 |
Context {ok : ok}. |
| 27 |
Local Existing Instance Types.rep.Z. |
| 28 |
|
| 29 |
Lemma equiv_Z_only_differ_iff1 |
| 30 |
{listZ : rep.rep base_listZ} |
| 31 |
locals1 locals2 vset (varname : base_ltype base_Z) s x : |
| 32 |
map.only_differ locals1 vset locals2 -> |
| 33 |
disjoint vset (varname_set_base varname) -> |
| 34 |
Lift1Prop.iff1 |
| 35 |
(rep.equiv x (rep.rtype_of_ltype varname) s locals1) |
| 36 |
(rep.equiv x (rep.rtype_of_ltype varname) s locals2). |
| 37 |
Proof. |
| 38 |
cbn [varname_set_base |
| 39 |
rep.varname_set rep.equiv rep.rtype_of_ltype rep.Z]. |
| 40 |
cbv [WeakestPrecondition.dexpr]. |
| 41 |
rewrite <-disjoint_singleton_r_iff by eauto using string_dec. |
| 42 |
split; intros; sepsimpl; subst; eexists; sepsimpl; eauto; |
| 43 |
eapply expr_untouched; eauto using @only_differ_sym with typeclass_instances. |
| 44 |
Qed. |
| 45 |
|
| 46 |
Lemma equiv_Z_only_differ_undef {listZ:rep.rep base_listZ} : |
| 47 |
forall x y s locals locals' vset, |
| 48 |
map.only_differ locals vset locals' -> |
| 49 |
map.undef_on locals vset -> |
| 50 |
Lift1Prop.impl1 |
| 51 |
(equivalent_base (t:=base_Z) x y s locals) |
| 52 |
(equivalent_base x y s locals'). |
| 53 |
Proof. |
| 54 |
cbv [equivalent_base rep.equiv rep.Z WeakestPrecondition.dexpr]. |
| 55 |
repeat intro; sepsimpl; subst; eexists; sepsimpl; eauto. |
| 56 |
pose expr_only_differ_undef; eauto. |
| 57 |
Qed. |
| 58 |
|
| 59 |
Section Local. |
| 60 |
Local Existing Instance rep.listZ_local. |
| 61 |
|
| 62 |
Lemma equiv_listZ_only_differ_local : forall |
| 63 |
locals1 locals2 vset (varnames : base_ltype base_listZ) s x (mem : mem), |
| 64 |
map.only_differ locals1 vset locals2 -> |
| 65 |
disjoint vset (varname_set_base varnames) -> |
| 66 |
rep.equiv x (rep.rtype_of_ltype varnames) s locals1 mem -> |
| 67 |
rep.equiv x (rep.rtype_of_ltype varnames) s locals2 mem. |
| 68 |
Proof. |
| 69 |
intros *. |
| 70 |
cbn [rep.equiv rep.rtype_of_ltype rep.listZ_local |
| 71 |
varname_set rep.varname_set]. |
| 72 |
rewrite !Forall.Forall2_map_r_iff. |
| 73 |
revert x; induction varnames; intros; |
| 74 |
match goal with H : Forall2 _ _ _ |- _ => |
| 75 |
inversion H; subst; clear H end; |
| 76 |
[ solve [eauto] | ]. |
| 77 |
cbn [fold_right] in *; cbv [emp] in *. cleanup. |
| 78 |
constructor. |
| 79 |
{ eapply equiv_Z_only_differ_iff1; |
| 80 |
cbn [rep.equiv rep.Z]; cbv [emp]; |
| 81 |
destruct_head'_and; |
| 82 |
eauto using @only_differ_sym with typeclass_instances. |
| 83 |
cbv [varname_set rep.varname_set rep.Z] in *. |
| 84 |
match goal with H : disjoint _ _ |- _ => |
| 85 |
apply disjoint_union_r_iff in H; |
| 86 |
cleanup |
| 87 |
end. eauto. } |
| 88 |
{ apply IHvarnames; eauto. |
| 89 |
match goal with H : disjoint _ _ |- _ => |
| 90 |
apply disjoint_union_r_iff in H; |
| 91 |
cleanup |
| 92 |
end. eauto. } |
| 93 |
Qed. |
| 94 |
|
| 95 |
Lemma equiv_listZ_only_differ_local_iff1 |
| 96 |
locals1 locals2 vset (varnames : base_ltype base_listZ) s x : |
| 97 |
map.only_differ locals1 vset locals2 -> |
| 98 |
disjoint vset (varname_set_base varnames) -> |
| 99 |
Lift1Prop.iff1 |
| 100 |
(rep.equiv x (rep.rtype_of_ltype varnames) s locals1) |
| 101 |
(rep.equiv x (rep.rtype_of_ltype varnames) s locals2). |
| 102 |
Proof. |
| 103 |
cbv [Lift1Prop.iff1]; split; intros; |
| 104 |
eapply equiv_listZ_only_differ_local; |
| 105 |
eauto using @only_differ_sym with typeclass_instances. |
| 106 |
Qed. |
| 107 |
|
| 108 |
Lemma equiv_listZ_only_differ_undef_local : |
| 109 |
forall x y s locals locals' vset, |
| 110 |
map.only_differ locals vset locals' -> |
| 111 |
map.undef_on locals vset -> |
| 112 |
Lift1Prop.impl1 |
| 113 |
(equivalent_base (t:=base_listZ) x y s locals) |
| 114 |
(equivalent_base x y s locals'). |
| 115 |
Proof. |
| 116 |
cbn [equivalent_base rep.equiv rep.listZ_local]; intros; sepsimpl. |
| 117 |
repeat intro; sepsimpl. |
| 118 |
eapply Forall.Forall2_Proper_impl; |
| 119 |
try eassumption; try reflexivity; repeat intro. |
| 120 |
eapply (equiv_Z_only_differ_undef (listZ:=rep.listZ_local)); eauto. |
| 121 |
Qed. |
| 122 |
End Local. |
| 123 |
|
| 124 |
Section InMemory. |
| 125 |
Local Existing Instance rep.listZ_mem. |
| 126 |
|
| 127 |
Lemma equiv_listZ_only_differ_mem |
| 128 |
locals1 locals2 vset (varnames : base_ltype base_listZ) s x : |
| 129 |
forall (mem : mem), |
| 130 |
map.only_differ locals1 vset locals2 -> |
| 131 |
disjoint vset (varname_set_base varnames) -> |
| 132 |
rep.equiv x (rep.rtype_of_ltype varnames) s locals1 mem -> |
| 133 |
rep.equiv x (rep.rtype_of_ltype varnames) s locals2 mem. |
| 134 |
Proof. |
| 135 |
intros *. |
| 136 |
cbn [rep.equiv rep.rtype_of_ltype rep.listZ_mem |
| 137 |
varname_set rep.varname_set]. |
| 138 |
intros. |
| 139 |
repeat match goal with |
| 140 |
H : Lift1Prop.ex1 _ _ |- Lift1Prop.ex1 _ _ => |
| 141 |
let x := fresh in |
| 142 |
destruct H as [x ?]; exists x |
| 143 |
end. |
| 144 |
eapply Proper_sep_iff1; [ | reflexivity | eassumption ]. |
| 145 |
cancel. |
| 146 |
eapply equiv_Z_only_differ_iff1; eauto using @only_differ_sym with typeclass_instances. |
| 147 |
Qed. |
| 148 |
|
| 149 |
Lemma equiv_listZ_only_differ_mem_iff1 |
| 150 |
locals1 locals2 vset (varnames : base_ltype base_listZ) s x : |
| 151 |
map.only_differ locals1 vset locals2 -> |
| 152 |
disjoint vset (varname_set_base varnames) -> |
| 153 |
Lift1Prop.iff1 |
| 154 |
(rep.equiv x (rep.rtype_of_ltype varnames) s locals1) |
| 155 |
(rep.equiv x (rep.rtype_of_ltype varnames) s locals2). |
| 156 |
Proof. |
| 157 |
cbv [Lift1Prop.iff1]; split; intros; |
| 158 |
eapply equiv_listZ_only_differ_mem; |
| 159 |
eauto using @only_differ_sym with typeclass_instances. |
| 160 |
Qed. |
| 161 |
|
| 162 |
Lemma equiv_listZ_only_differ_undef_mem : |
| 163 |
forall x y s locals locals' vset, |
| 164 |
map.only_differ locals vset locals' -> |
| 165 |
map.undef_on locals vset -> |
| 166 |
Lift1Prop.impl1 |
| 167 |
(equivalent_base |
| 168 |
(t:=base_listZ) (listZ:=rep.listZ_mem) |
| 169 |
x y s locals) |
| 170 |
(equivalent_base x y s locals'). |
| 171 |
Proof. |
| 172 |
cbn [equivalent_base rep.equiv rep.listZ_mem]; intros; sepsimpl. |
| 173 |
repeat intro; sepsimpl. eexists. |
| 174 |
repeat intro; sepsimpl. eexists. |
| 175 |
eapply Proper_sep_impl1; [ | reflexivity | eassumption ]. |
| 176 |
repeat intro; sepsimpl; eauto. |
| 177 |
eapply (equiv_Z_only_differ_undef (listZ:=rep.listZ_mem)); eauto. |
| 178 |
Qed. |
| 179 |
|
| 180 |
Lemma equiv_nil_iff1 y s : forall (locals : locals), |
| 181 |
Lift1Prop.iff1 |
| 182 |
(rep.equiv (rep:=rep.listZ_mem) [] y s locals) |
| 183 |
(Lift1Prop.ex1 |
| 184 |
(fun x => rep.equiv (rep:=rep.Z) x y tt locals)). |
| 185 |
Proof. |
| 186 |
cbn [rep.equiv rep.listZ_mem rep.Z]. |
| 187 |
intro; split; intros; |
| 188 |
repeat match goal with |
| 189 |
| _ => progress subst |
| 190 |
| _ => progress cbn [Array.array map] in * |
| 191 |
| _ => progress sepsimpl |
| 192 |
| H : map _ _ = [] |- _ => |
| 193 |
apply map_eq_nil in H |
| 194 |
| _ => rewrite word.of_Z_unsigned in * |
| 195 |
| |- _ /\ _ => split |
| 196 |
| |- Lift1Prop.ex1 _ _ => eexists |
| 197 |
| |- emp _ _ => cbv [emp] |
| 198 |
| _ => solve [apply word.unsigned_range] |
| 199 |
| _ => solve [eauto using map_nil] |
| 200 |
end. |
| 201 |
Qed. |
| 202 |
|
| 203 |
Lemma varname_set_listonly_listexcl {t} (names : _ t) : |
| 204 |
sameset |
| 205 |
(varname_set_base names) |
| 206 |
(union (varname_set_listonly names) |
| 207 |
(varname_set_listexcl names)). |
| 208 |
Proof. |
| 209 |
induction t; |
| 210 |
cbn [fst snd varname_set_base varname_set_listexcl |
| 211 |
varname_set_listonly]; |
| 212 |
break_match; intros; |
| 213 |
rewrite ?union_empty_l, ?union_empty_r; |
| 214 |
try reflexivity; [ ]. |
| 215 |
rewrite IHt1, IHt2. |
| 216 |
clear. firstorder idtac. |
| 217 |
Qed. |
| 218 |
|
| 219 |
Lemma varname_set_listexcl_subset {t} (names : base_ltype t) : |
| 220 |
subset (varname_set_listexcl names) (varname_set_base names). |
| 221 |
Proof. |
| 222 |
rewrite varname_set_listonly_listexcl. |
| 223 |
clear. firstorder idtac. |
| 224 |
Qed. |
| 225 |
End InMemory. |
| 226 |
|
| 227 |
Section Generic. |
| 228 |
Context {listZ : rep.rep base_listZ} |
| 229 |
(equiv_listZ_only_differ_undef : |
| 230 |
forall x y s locals1 locals2 vset, |
| 231 |
map.only_differ locals1 vset locals2 -> |
| 232 |
map.undef_on locals1 vset -> |
| 233 |
Lift1Prop.impl1 |
| 234 |
(rep.equiv (rep:=listZ) x y s locals1) |
| 235 |
(rep.equiv x y s locals2)) |
| 236 |
(equiv_listZ_only_differ : |
| 237 |
forall |
| 238 |
locals1 locals2 vset |
| 239 |
(varnames : base_ltype base_listZ) s x mem, |
| 240 |
map.only_differ locals1 vset locals2 -> |
| 241 |
disjoint vset (varname_set_base varnames) -> |
| 242 |
rep.equiv x (rep.rtype_of_ltype varnames) s locals1 mem -> |
| 243 |
rep.equiv x (rep.rtype_of_ltype varnames) s locals2 mem). |
| 244 |
|
| 245 |
Lemma equivalent_only_differ {t} |
| 246 |
locals1 locals2 vset (varnames : base_ltype t) s x : |
| 247 |
map.only_differ locals1 vset locals2 -> |
| 248 |
disjoint vset (varname_set_base varnames) -> |
| 249 |
forall mem, |
| 250 |
equivalent_base x (base_rtype_of_ltype varnames) s locals1 mem -> |
| 251 |
equivalent_base x (base_rtype_of_ltype varnames) s locals2 mem. |
| 252 |
Proof. |
| 253 |
intros Hdiffer Hexcl. |
| 254 |
induction t; |
| 255 |
cbn [fst snd rtype_of_ltype varname_set_base equivalent_base] in *; |
| 256 |
intros; break_match; destruct_head'_and; try tauto. |
| 257 |
{ (* base case *) |
| 258 |
eapply equiv_Z_only_differ_iff1; eauto using @only_differ_sym with typeclass_instances. } |
| 259 |
{ (* prod case *) |
| 260 |
match goal with H : disjoint _ (union _ _) |- _ => |
| 261 |
apply disjoint_union_r_iff in H |
| 262 |
end. |
| 263 |
cleanup. |
| 264 |
eapply Proper_sep_impl1; [ | | eassumption]; repeat intro; eauto. |
| 265 |
{ apply IHt1; eauto. } |
| 266 |
{ apply IHt2; eauto. } } |
| 267 |
{ (* list case *) |
| 268 |
eapply equiv_listZ_only_differ; eauto. } |
| 269 |
Qed. |
| 270 |
|
| 271 |
Lemma equivalent_only_differ_iff1 {t} |
| 272 |
locals1 locals2 vset (varnames : base_ltype t) s x : |
| 273 |
map.only_differ locals1 vset locals2 -> |
| 274 |
disjoint vset (varname_set_base varnames) -> |
| 275 |
Lift1Prop.iff1 |
| 276 |
(equivalent_base x (base_rtype_of_ltype varnames) s locals1) |
| 277 |
(equivalent_base x (base_rtype_of_ltype varnames) s locals2). |
| 278 |
Proof. |
| 279 |
repeat intro. split; intros. |
| 280 |
all:eapply equivalent_only_differ; eauto using @only_differ_sym with typeclass_instances. |
| 281 |
Qed. |
| 282 |
|
| 283 |
Lemma equivalent_args_only_differ {t} |
| 284 |
locals1 locals2 vset |
| 285 |
(argnames : type.for_each_lhs_of_arrow ltype t) |
| 286 |
s x : |
| 287 |
map.only_differ locals1 vset locals2 -> |
| 288 |
disjoint vset (varname_set_args argnames) -> |
| 289 |
let argvalues := |
| 290 |
type.map_for_each_lhs_of_arrow |
| 291 |
rtype_of_ltype argnames in |
| 292 |
forall m, |
| 293 |
equivalent_args x argvalues s locals1 m -> |
| 294 |
equivalent_args x argvalues s locals2 m. |
| 295 |
Proof. |
| 296 |
induction t; |
| 297 |
cbv [Lift1Prop.iff1]; |
| 298 |
cbn [fst snd rtype_of_ltype varname_set_args |
| 299 |
type.map_for_each_lhs_of_arrow |
| 300 |
equivalent_args]; |
| 301 |
intros; break_match; cbn [fst snd] in *; |
| 302 |
try tauto; [ ]. |
| 303 |
intros; cleanup; subst. |
| 304 |
repeat match goal with |
| 305 |
| |- _ /\ _ => split |
| 306 |
| |- exists _, _ => eexists |
| 307 |
| _ => solve [eauto] |
| 308 |
end; |
| 309 |
[ eapply Proper_sep_iff1; |
| 310 |
[ eapply equivalent_only_differ_iff1 |
| 311 |
| reflexivity | eassumption ] | eapply IHt2 ]; |
| 312 |
eauto using @only_differ_sym with typeclass_instances. |
| 313 |
all:match goal with |
| 314 |
| H : disjoint _ (union _ _) |- _ => |
| 315 |
apply disjoint_union_r_iff in H; |
| 316 |
cleanup; eauto |
| 317 |
end. |
| 318 |
Qed. |
| 319 |
|
| 320 |
Lemma equivalent_only_differ_undef {t} : |
| 321 |
forall locals1 locals2 vset x y s, |
| 322 |
map.only_differ locals1 vset locals2 -> |
| 323 |
map.undef_on locals1 vset -> |
| 324 |
Lift1Prop.impl1 |
| 325 |
(equivalent_base (t:=t) x y s locals1) |
| 326 |
(equivalent_base x y s locals2). |
| 327 |
Proof. |
| 328 |
induction t; |
| 329 |
cbn [equivalent_base]; |
| 330 |
break_match; intros; try reflexivity; [ | | ]. |
| 331 |
{ eapply equiv_Z_only_differ_undef; eauto. } |
| 332 |
{ apply Proper_sep_impl1; eauto. } |
| 333 |
{ eapply equiv_listZ_only_differ_undef; eauto. } |
| 334 |
Qed. |
| 335 |
End Generic. |
| 336 |
End OnlyDiffer. |
| 337 |
Global Hint Resolve |
| 338 |
equiv_listZ_only_differ_undef_local |
| 339 |
equiv_listZ_only_differ_undef_mem |
| 340 |
equiv_listZ_only_differ_local |
| 341 |
equiv_listZ_only_differ_mem : equiv. |
| 342 |
|
| 343 |
Section ContextEquivalence. |
| 344 |
Context |
| 345 |
{width BW word mem locals ext_spec varname_gen error} |
| 346 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}. |
| 347 |
Context {ok : ok}. |
| 348 |
Local Existing Instance Types.rep.Z. |
| 349 |
|
| 350 |
Section Local. |
| 351 |
Local Existing Instance Types.rep.listZ_local. |
| 352 |
|
| 353 |
(* 3-way equivalence (for single elements of the context list G |
| 354 |
from wf3 preconditions) *) |
| 355 |
Definition equiv3 {var1} |
| 356 |
(locals : Interface.map.rep (map:=locals)) |
| 357 |
(x : {t : API.type |
| 358 |
& (var1 t * API.interp_type t * ltype t)%type}) |
| 359 |
: Prop := |
| 360 |
match x with |
| 361 |
| existT (type.base b) (w, x, y) => |
| 362 |
locally_equivalent x (base_rtype_of_ltype y) locals |
| 363 |
| existT (type.arrow _ _) _ => False (* no functions allowed *) |
| 364 |
end. |
| 365 |
|
| 366 |
Definition context_equiv {var1} G locals |
| 367 |
: Prop := Forall (equiv3 (var1:= var1) locals) G. |
| 368 |
|
| 369 |
Fixpoint context_varname_set {var1} |
| 370 |
(G : list {t : API.type & (var1 t * API.interp_type t * ltype t)%type}) |
| 371 |
: PropSet.set string := |
| 372 |
match G with |
| 373 |
| (existT (type.base b) (w, x, y)) :: G' => |
| 374 |
union (varname_set y) (context_varname_set G') |
| 375 |
| _ => PropSet.empty_set (* no functions allowed *) |
| 376 |
end. |
| 377 |
|
| 378 |
Lemma varname_set_local x : |
| 379 |
sameset |
| 380 |
(rep.varname_set (rep:=rep.listZ_local) x) |
| 381 |
(of_list x). |
| 382 |
Proof. |
| 383 |
apply sameset_iff. |
| 384 |
cbn [rep.varname_set rep.listZ_local rep.Z]. |
| 385 |
induction x; cbn [fold_right of_list In]; |
| 386 |
[ solve [firstorder idtac] | ]. |
| 387 |
intros. cbv [of_list] in *. |
| 388 |
rewrite <-IHx. firstorder idtac. |
| 389 |
Qed. |
| 390 |
End Local. |
| 391 |
|
| 392 |
Lemma equivalent_not_in_context {var1} locals1 locals2 vset x : |
| 393 |
map.only_differ locals1 vset locals2 -> |
| 394 |
disjoint vset (@context_varname_set var1 (x :: nil)) -> |
| 395 |
equiv3 locals1 x -> |
| 396 |
equiv3 locals2 x. |
| 397 |
Proof. |
| 398 |
intros; cbv [equiv3 context_varname_set locally_equivalent] in *. |
| 399 |
destruct x as [x [ [? ?] ?] ]; destruct x; [ | tauto ]. |
| 400 |
eapply equivalent_only_differ; eauto with equiv. |
| 401 |
match goal with H : _ |- _ => |
| 402 |
apply disjoint_union_r_iff in H end. |
| 403 |
cleanup; eauto. |
| 404 |
Qed. |
| 405 |
|
| 406 |
Lemma equivalent_not_in_context_forall {var1} locals1 locals2 vset G : |
| 407 |
map.only_differ locals1 vset locals2 -> |
| 408 |
disjoint vset (@context_varname_set var1 G) -> |
| 409 |
Forall (equiv3 locals1) G -> |
| 410 |
Forall (equiv3 locals2) G. |
| 411 |
Proof. |
| 412 |
induction G; intros; constructor; |
| 413 |
repeat match goal with |
| 414 |
| _ => progress cbn [context_varname_set equiv3] in * |
| 415 |
| _ => progress break_match_hyps |
| 416 |
| H : disjoint _ (union _ _) |- _ => |
| 417 |
apply disjoint_union_r_iff in H; cleanup |
| 418 |
| H : Forall _ (_ :: _) |- _ => |
| 419 |
inversion H; subst; clear H |
| 420 |
| _ => tauto |
| 421 |
| _ => eapply equivalent_only_differ; |
| 422 |
solve [eauto with equiv] |
| 423 |
end. |
| 424 |
Qed. |
| 425 |
End ContextEquivalence. |