| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import String. |
| 3 |
From Coq Require Import Lia. |
| 4 |
Require Import bedrock2.Syntax. |
| 5 |
Require Import bedrock2.Map.Separation. |
| 6 |
Require Import bedrock2.Map.SeparationLogic. |
| 7 |
Require Import coqutil.Map.Interface coqutil.Map.Properties. |
| 8 |
Require Import coqutil.Word.Interface coqutil.Word.Properties. |
| 9 |
Require Import coqutil.Datatypes.PropSet. |
| 10 |
From Coq Require Import List. (* after SeparationLogic *) |
| 11 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 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 |
(* for [eauto with lia] *) |
| 20 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 21 |
|
| 22 |
Import API.Compilers. |
| 23 |
Import ListNotations Types.Notations. |
| 24 |
|
| 25 |
Section UsedVarnames. |
| 26 |
Context |
| 27 |
{width BW word mem locals ext_spec varname_gen error} |
| 28 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}. |
| 29 |
Context {ok : ok}. |
| 30 |
Local Existing Instance Types.rep.Z. |
| 31 |
Local Instance varname_eqb_spec x y : BoolSpec _ _ _ |
| 32 |
:= Decidable.String.eqb_spec x y. |
| 33 |
Local Notation varname := String.string. |
| 34 |
|
| 35 |
Definition used_varnames nextn nvars : set varname := |
| 36 |
of_list (map varname_gen (seq nextn nvars)). |
| 37 |
|
| 38 |
Lemma used_varnames_iff nextn nvars v : |
| 39 |
used_varnames nextn nvars v <-> |
| 40 |
(exists n, |
| 41 |
v = varname_gen n /\ nextn <= n < nextn + nvars)%nat. |
| 42 |
Proof. |
| 43 |
cbv [used_varnames of_list]. revert nextn v. |
| 44 |
induction nvars; intros; cbn [seq map In]; |
| 45 |
[ split; try tauto; intros; cleanup; lia | ]. |
| 46 |
rewrite IHnvars. |
| 47 |
split; intros; |
| 48 |
repeat match goal with |
| 49 |
| _ => progress cleanup |
| 50 |
| _ => progress subst |
| 51 |
| H : _ \/ _ |- _ => destruct H |
| 52 |
| |- exists _, _ => eexists; solve [eauto with lia] |
| 53 |
end; [ ]. |
| 54 |
match goal with H : _ <= _ |- _ => |
| 55 |
apply Nat.lt_eq_cases in H; destruct H; [right | left] |
| 56 |
end. |
| 57 |
{ eexists; eauto with lia. } |
| 58 |
{ congruence. } |
| 59 |
Qed. |
| 60 |
|
| 61 |
Lemma used_varnames_disjoint n1 n2 l1 l2 : |
| 62 |
n1 + l1 <= n2 -> |
| 63 |
disjoint (used_varnames n1 l1) (used_varnames n2 l2). |
| 64 |
Proof. |
| 65 |
cbv [used_varnames]. |
| 66 |
revert n1 n2 l2; induction l1; cbn [map seq]; intros; |
| 67 |
rewrite ?of_list_nil, ?of_list_cons; |
| 68 |
eauto using disjoint_empty_l. |
| 69 |
rewrite add_union_singleton. |
| 70 |
apply disjoint_union_l_iff; split; eauto with lia; [ ]. |
| 71 |
apply disjoint_not_in. intro. |
| 72 |
repeat match goal with |
| 73 |
| _ => progress cleanup |
| 74 |
| H : In _ (map _ _) |- _ => rewrite in_map_iff in H |
| 75 |
| H : In _ (seq _ _) |- _ => rewrite in_seq in H |
| 76 |
| H : varname_gen _ = varname_gen _ |- _ => |
| 77 |
apply varname_gen_unique in H |
| 78 |
| _ => lia |
| 79 |
end. |
| 80 |
Qed. |
| 81 |
|
| 82 |
Lemma used_varnames_succ_high n l : |
| 83 |
sameset (used_varnames n (S l)) |
| 84 |
(add (used_varnames n l) (varname_gen (n + l))). |
| 85 |
Proof. |
| 86 |
intros. cbv [used_varnames]. |
| 87 |
rewrite seq_snoc, map_app. cbn [map]. |
| 88 |
rewrite of_list_app, of_list_cons. |
| 89 |
rewrite !add_union_singleton, of_list_nil, union_empty_r. |
| 90 |
apply union_comm. |
| 91 |
Qed. |
| 92 |
|
| 93 |
Lemma used_varnames_succ_low n m : |
| 94 |
PropSet.sameset (used_varnames n (S m)) |
| 95 |
(PropSet.add (used_varnames (S n) m) |
| 96 |
(varname_gen n)). |
| 97 |
Proof. |
| 98 |
apply sameset_iff. cbn. firstorder idtac. |
| 99 |
Qed. |
| 100 |
|
| 101 |
Lemma used_varnames_1 n : |
| 102 |
PropSet.sameset (used_varnames n 1) |
| 103 |
(PropSet.singleton_set (varname_gen n)). |
| 104 |
Proof. |
| 105 |
apply sameset_iff. cbn. firstorder idtac. |
| 106 |
Qed. |
| 107 |
|
| 108 |
Lemma used_varnames_subset n1 n2 l1 l2 : |
| 109 |
(n2 <= n1)%nat -> |
| 110 |
(n1 + l1 <= n2 + l2)%nat -> |
| 111 |
PropSet.subset (used_varnames n1 l1) |
| 112 |
(used_varnames n2 l2). |
| 113 |
Proof. |
| 114 |
cbv [PropSet.subset PropSet.elem_of]; |
| 115 |
intros; rewrite !used_varnames_iff in *. |
| 116 |
cleanup; subst. |
| 117 |
eexists; split; [ reflexivity | lia ]. |
| 118 |
Qed. |
| 119 |
|
| 120 |
Lemma used_varnames_union n m l : |
| 121 |
sameset (used_varnames n (m + l)) |
| 122 |
(union (used_varnames n m) (used_varnames (n + m) l)). |
| 123 |
Proof. |
| 124 |
cbv [used_varnames]. |
| 125 |
revert n m; induction l; cbn [map seq]; intros; |
| 126 |
rewrite ?Nat.add_0_r, ?of_list_nil, ?union_empty_r; |
| 127 |
try reflexivity; [ ]. |
| 128 |
rewrite of_list_cons, add_union_singleton, union_assoc. |
| 129 |
rewrite <-Nat.add_succ_comm. rewrite IHl. |
| 130 |
rewrite seq_snoc, map_app. cbn [map]. |
| 131 |
rewrite of_list_app, of_list_cons. |
| 132 |
rewrite !add_union_singleton, of_list_nil, union_empty_r. |
| 133 |
rewrite Nat.add_succ_r. |
| 134 |
reflexivity. |
| 135 |
Qed. |
| 136 |
|
| 137 |
Lemma used_varnames_shift n m l : |
| 138 |
subset (used_varnames (n + m) l) |
| 139 |
(used_varnames n (m + l)). |
| 140 |
Proof. |
| 141 |
cbv [subset]. intros. |
| 142 |
match goal with H : _ |- _ => |
| 143 |
apply used_varnames_iff in H end. |
| 144 |
apply used_varnames_iff. |
| 145 |
cleanup; subst. eexists; split; eauto. |
| 146 |
lia. |
| 147 |
Qed. |
| 148 |
|
| 149 |
Lemma used_varnames_subset_singleton n m l : |
| 150 |
n <= m < n + l -> |
| 151 |
subset (singleton_set (varname_gen m)) |
| 152 |
(used_varnames n l). |
| 153 |
Proof. |
| 154 |
cbv [subset singleton_set elem_of]. intros. |
| 155 |
apply used_varnames_iff; subst. |
| 156 |
eexists; split; eauto; lia. |
| 157 |
Qed. |
| 158 |
|
| 159 |
Implicit Types l : locals. |
| 160 |
|
| 161 |
Lemma only_differ_zero nextn l : |
| 162 |
map.only_differ l (used_varnames nextn 0) l. |
| 163 |
Proof. |
| 164 |
cbv [map.only_differ used_varnames of_list elem_of]. |
| 165 |
tauto. |
| 166 |
Qed. |
| 167 |
|
| 168 |
Lemma only_differ_succ nextn nvars l1 l2 v : |
| 169 |
map.only_differ (map.put l1 (varname_gen nextn) v) |
| 170 |
(used_varnames (S nextn) nvars) l2 -> |
| 171 |
map.only_differ l1 (used_varnames nextn (S nvars)) l2. |
| 172 |
Proof. |
| 173 |
intros. |
| 174 |
eapply Proper_only_differ; |
| 175 |
[ reflexivity | | reflexivity | eapply only_differ_trans; |
| 176 |
solve [eauto using @only_differ_sym, @only_differ_put with typeclass_instances ] ]. |
| 177 |
eapply sameset_iff; intros. |
| 178 |
cbv [used_varnames of_list elem_of union singleton_set]. |
| 179 |
cbn [seq map In]. |
| 180 |
tauto. |
| 181 |
Qed. |
| 182 |
|
| 183 |
Lemma only_differ_step nvars nvars' nextn l1 l2 l3 : |
| 184 |
map.only_differ l1 (used_varnames nextn nvars) l2 -> |
| 185 |
map.only_differ l2 (used_varnames (nextn + nvars) nvars') l3 -> |
| 186 |
map.only_differ l1 (used_varnames nextn (nvars + nvars')) l3. |
| 187 |
Proof. |
| 188 |
cbv [map.only_differ used_varnames of_list |
| 189 |
elem_of]. |
| 190 |
let H1 := fresh in |
| 191 |
let H2 := fresh in |
| 192 |
let x := fresh "x" in |
| 193 |
intros H1 H2 x; specialize (H1 x); specialize (H2 x). |
| 194 |
repeat match goal with |
| 195 |
| _ => progress cleanup |
| 196 |
| _ => progress subst |
| 197 |
| H : _ \/ _ |- _ => destruct H |
| 198 |
| |- context [In _ (map _ _)] => rewrite in_map_iff |
| 199 |
| H : In _ (map _ _) |- _ => apply in_map_iff in H |
| 200 |
| H : In _ (seq _ _) |- _ => apply in_seq in H |
| 201 |
| H : varname_gen _ = varname_gen _ |- _ => |
| 202 |
apply varname_gen_unique in H |
| 203 |
| _ => solve [right; congruence] |
| 204 |
| _ => solve [left; eexists; |
| 205 |
rewrite in_seq, varname_gen_unique; split; |
| 206 |
eauto with lia ] |
| 207 |
end. |
| 208 |
Qed. |
| 209 |
|
| 210 |
Lemma disjoint_used_varnames_lt n nvars (vset : set varname) : |
| 211 |
(forall x, n <= x -> ~ vset (varname_gen x)) -> |
| 212 |
disjoint (used_varnames n nvars) vset. |
| 213 |
Proof. |
| 214 |
cbv [disjoint elem_of]; intros. |
| 215 |
apply Decidable.imp_simp. |
| 216 |
{ cbv [used_varnames Decidable.decidable of_list ]. |
| 217 |
match goal with |
| 218 |
|- In ?x ?l \/ ~ In ?x ?l => |
| 219 |
destruct (in_dec string_dec x l); [left|right] |
| 220 |
end; tauto. } |
| 221 |
rewrite used_varnames_iff. |
| 222 |
intros; cleanup; subst. |
| 223 |
eauto with lia. |
| 224 |
Qed. |
| 225 |
|
| 226 |
Lemma disjoint_used_varnames_singleton n nvars m : |
| 227 |
m < n -> |
| 228 |
disjoint (used_varnames n nvars) |
| 229 |
(singleton_set (varname_gen m)). |
| 230 |
Proof. |
| 231 |
cbv [singleton_set elem_of]; intros. |
| 232 |
eapply disjoint_used_varnames_lt. intros. |
| 233 |
rewrite varname_gen_unique. lia. |
| 234 |
Qed. |
| 235 |
End UsedVarnames. |