Debian Codegraph @

src/Bedrock/Field/Common/Names/MakeNames.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import List.
3 From Coq Require Import Lia.
4 From Coq Require Import String.
5 Require Import coqutil.Datatypes.List.
6 Require Import coqutil.Datatypes.PropSet.
7 Require Import Crypto.Bedrock.Field.Common.Tactics.
8 Require Import Crypto.Bedrock.Field.Common.Types.
9 Require Import Crypto.Bedrock.Field.Translation.Proofs.Flatten.
10 Require Import Crypto.Bedrock.Field.Translation.Proofs.VarnameSet.
11 Require Import Crypto.Bedrock.Field.Translation.Flatten.
12 Require Import Crypto.Language.API.
13 Require Import Crypto.Util.Tactics.BreakMatch.
14
15 Import Language.API.Compilers.
16 Import Types.Notations.
17 Import ListNotations.
18 #[global]
19 Existing Instances rep.Z rep.listZ_mem.
20
21 Section with_parameters.
22 Context
23 {width BW word mem locals ext_spec varname_gen error}
24 `{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}.
25 Context {inname_gen outname_gen : nat -> string}.
26
27 Fixpoint make_names
28 (name_gen : nat -> string) (nextn : nat) (t : base.type)
29 : nat * base_ltype t :=
30 match t as t0 return nat * base_ltype t0 with
31 | base.type.prod a b =>
32 let resa := make_names name_gen nextn a in
33 let resb := make_names name_gen (fst resa) b in
34 (fst resb, (snd resa, snd resb))
35 | base_listZ =>
36 (S nextn, name_gen nextn)
37 | _ =>
38 (S nextn, name_gen nextn)
39 end.
40 Fixpoint make_innames' (nextn : nat) (t : API.type)
41 : nat * type.for_each_lhs_of_arrow ltype t :=
42 match t as t0 return
43 nat * type.for_each_lhs_of_arrow ltype t0 with
44 | type.base _ => (nextn, tt)
45 | type.arrow (type.base s) d =>
46 let ress := make_names inname_gen nextn s in
47 let resd := make_innames' (fst ress) d in
48 (fst resd, (snd ress, snd resd))
49 | type.arrow _ d =>
50 let resd := make_innames' nextn d in
51 (fst resd, (tt, snd resd))
52 end.
53 Definition make_innames t : type.for_each_lhs_of_arrow ltype t :=
54 snd (make_innames' 0 t).
55 Definition make_outnames t : base_ltype t :=
56 snd (make_names outname_gen 0 t).
57
58 Section proofs.
59 Context (inname_gen_varname_gen_ok :
60 forall n m, inname_gen n <> varname_gen m)
61 (outname_gen_varname_gen_ok :
62 forall n m, outname_gen n <> varname_gen m)
63 (outname_gen_inname_gen_ok :
64 forall n m, outname_gen n <> inname_gen m).
65 Context (inname_gen_unique :
66 forall i j : nat, inname_gen i = inname_gen j <-> i = j)
67 (outname_gen_unique :
68 forall i j : nat, outname_gen i = outname_gen j <-> i = j).
69
70 Lemma make_names_no_collision name_gen1 name_gen2 t :
71 (forall n m : nat, name_gen1 n <> name_gen2 m) ->
72 forall nextn n,
73 ~ varname_set_base (snd (make_names name_gen1 nextn t))
74 (name_gen2 n).
75 Proof.
76 intro Hdisjoint; induction t; intros;
77 cbn [varname_set_base rep.varname_set rep.Z rep.listZ_mem
78 make_names fst snd ];
79 repeat match goal with
80 | _ => progress cbn [fst snd]
81 | |- ~ PropSet.singleton_set _ _ =>
82 apply disjoint_singleton_r_iff, disjoint_singleton_singleton; auto
83 | |- ~ PropSet.union _ _ _ =>
84 apply not_union_iff; split; solve [auto]
85 | _ => progress break_innermost_match
86 end.
87 Qed.
88
89 Lemma make_innames'_varname_gen_disjoint t :
90 forall nextn n,
91 ~ varname_set_args
92 (snd (make_innames' nextn t)) (varname_gen n).
93 Proof.
94 induction t; intros;
95 repeat match goal with
96 | _ => progress
97 cbn [fst snd make_innames'
98 varname_set_args] in *
99 | |- ~ PropSet.empty_set _ => cbv [PropSet.empty_set]; tauto
100 | |- ~ PropSet.union _ _ _ =>
101 apply not_union_iff; split
102 | _ => progress break_innermost_match
103 | _ => solve [auto using make_names_no_collision]
104 end.
105 Qed.
106
107 Lemma make_innames_varname_gen_disjoint t :
108 forall n,
109 ~ varname_set_args (make_innames t)
110 (varname_gen n).
111 Proof. apply make_innames'_varname_gen_disjoint. Qed.
112
113 Lemma make_outnames_varname_gen_disjoint t :
114 forall n,
115 ~ varname_set_base (make_outnames t) (varname_gen n).
116 Proof. apply make_names_no_collision; auto. Qed.
117
118 Lemma fst_make_names_lower_bound name_gen t :
119 forall nextn, (nextn <= fst (make_names name_gen nextn t))%nat.
120 Proof.
121 induction t; intros;
122 repeat match goal with
123 | _ => progress intros
124 | _ => progress cbn [fst snd make_names] in *
125 | _ => progress break_innermost_match
126 | _ => lia
127 end; [ ].
128 eapply Nat.le_trans; [ | solve [eauto] ]. eauto.
129 Qed.
130
131 Lemma flatten_make_names_range name_gen t :
132 forall x nextn,
133 In x (flatten_base_ltype
134 (snd (make_names name_gen nextn t))) ->
135 exists n, x = name_gen n
136 /\ (nextn <= n < fst (make_names name_gen nextn t))%nat.
137 Proof.
138 induction t; intros;
139 repeat match goal with
140 | _ => progress intros
141 | _ => progress cbn [fst snd make_names
142 flatten_base_ltype] in *
143 | H : _ /\ _ |- _ => destruct H
144 | H : In _ [_] |- _ => inversion H; clear H
145 | H : In _ [] |- _ => solve [inversion H]
146 | H : In _ (_ ++ _) |- _ =>
147 apply in_app_iff in H; destruct H
148 | H : name_gen ?x = ?y |- exists n, ?y = name_gen n /\ _ =>
149 exists x; split; [ congruence | ]
150 | H : ?y = name_gen ?x |- exists n, ?y = name_gen n /\ _ =>
151 exists x; split; [ assumption | ]
152 | _ => specialize (IHt1 _ _ ltac:(eassumption));
153 destruct IHt1
154 | _ => specialize (IHt2 _ _ ltac:(eassumption));
155 destruct IHt2
156 | _ => progress break_innermost_match
157 | _ => lia
158 end; [ | ];
159 pose proof fst_make_names_lower_bound name_gen t1 nextn;
160 pose proof fst_make_names_lower_bound
161 name_gen t2 (fst (make_names name_gen nextn t1));
162 lia.
163 Qed.
164
165 Lemma flatten_make_names_NoDup name_gen t :
166 (forall i j, name_gen i = name_gen j <-> i = j) ->
167 forall nextn,
168 NoDup (flatten_base_ltype
169 (snd (make_names name_gen nextn t))).
170 Proof.
171 intro name_gen_unique.
172 induction t; intros;
173 repeat match goal with
174 | _ => progress intros
175 | _ => progress cbn [fst snd make_names
176 flatten_base_ltype]
177 | H : In _ _ |- _ => apply flatten_make_names_range in H;
178 destruct H
179 | H : _ /\ _ |- _ => destruct H
180 | H1 : ?x = name_gen ?n1, H2 : ?x = name_gen ?n2 |- _ =>
181 pose proof (proj1 (name_gen_unique n1 n2)
182 ltac:(congruence)); clear H1 H2
183 | _ => apply NoDup_cons
184 | |- NoDup (_ ++ _) => apply NoDup_app_iff
185 | |- _ /\ _ => split
186 | |- ~ In _ _ => try apply in_nil; intro
187 | _ => progress break_innermost_match
188 | _ => solve [auto using in_nil, NoDup_nil]
189 | _ => lia
190 end.
191 Qed.
192
193 Lemma fst_make_innames'_lower_bound t :
194 forall nextn,
195 (nextn <= fst (make_innames' nextn t))%nat.
196 Proof.
197 induction t; intros;
198 repeat match goal with
199 | _ => progress intros
200 | _ => progress cbn [fst snd make_innames'] in *
201 | _ => progress break_innermost_match
202 | _ => progress break_innermost_match_hyps
203 | _ => lia
204 | _ => solve [eauto]
205 end; [ ].
206 eapply Nat.le_trans; [ apply fst_make_names_lower_bound | ].
207 eauto.
208 Qed.
209
210 Lemma flatten_make_innames'_range t :
211 forall x nextn,
212 In x (flatten_argnames (snd (make_innames' nextn t))) ->
213 exists n,
214 x = inname_gen n
215 /\ (nextn <= n < fst (make_innames' nextn t))%nat.
216 Proof.
217 induction t; intros;
218 repeat match goal with
219 | _ => progress intros
220 | _ => progress cbn [fst snd make_innames'
221 flatten_argnames] in *
222 | H : _ /\ _ |- _ => destruct H
223 | H : In _ [] |- _ => solve [inversion H]
224 | H : In _ (_ ++ _) |- _ =>
225 apply in_app_iff in H; destruct H
226 | H : In _ _ |- _ => apply flatten_make_names_range in H;
227 destruct H
228 | H : ?y = inname_gen ?x
229 |- exists n, ?y = inname_gen n /\ _ =>
230 exists x; split; [ assumption | ]
231 | _ => specialize (IHt2 _ _ ltac:(eassumption));
232 destruct IHt2
233 | _ => progress break_innermost_match
234 | _ => lia
235 end; [ | ].
236 { pose proof fst_make_innames'_lower_bound t2 (fst (make_names inname_gen nextn t)).
237 lia. }
238 { pose proof fst_make_names_lower_bound inname_gen t nextn.
239 lia. }
240 Qed.
241
242 Lemma flatten_make_innames'_NoDup t :
243 forall nextn,
244 NoDup (flatten_argnames (snd (make_innames' nextn t))).
245 Proof.
246 induction t; intros;
247 repeat match goal with
248 | _ => progress intros
249 | _ => progress cbn [fst snd make_innames' flatten_argnames]
250 | _ => apply NoDup_nil
251 | H : _ /\ _ |- _ => destruct H
252 | H : In _ _ |- _ => apply flatten_make_names_range in H;
253 destruct H
254 | H : In _ _ |- _ => apply flatten_make_innames'_range in H;
255 destruct H
256 | H1 : ?x = inname_gen ?n1, H2 : ?x = inname_gen ?n2 |- _ =>
257 pose proof (proj1 (inname_gen_unique n1 n2)
258 ltac:(congruence)); clear H1 H2
259 | |- NoDup (_ ++ _) => apply NoDup_app_iff
260 | |- _ /\ _ => split
261 | |- ~ In _ _ => intro
262 | _ => progress break_innermost_match
263 | _ => solve [ auto using flatten_make_names_NoDup]
264 | _ => lia
265 end.
266 Qed.
267
268 Lemma flatten_make_innames_NoDup t :
269 NoDup
270 (flatten_argnames (make_innames t)).
271 Proof. apply flatten_make_innames'_NoDup. Qed.
272
273 Lemma flatten_make_innames_exists t x :
274 In x (flatten_argnames (make_innames t)) ->
275 exists n : nat, x = inname_gen n.
276 Proof.
277 cbv [make_innames]; intros.
278 destruct (flatten_make_innames'_range t _ _ ltac:(eassumption)).
279 cleanup. eexists; eauto.
280 Qed.
281
282 Lemma flatten_make_outnames_exists t x :
283 In x (flatten_base_ltype (make_outnames t)) ->
284 exists n : nat, x = outname_gen n.
285 Proof.
286 cbv [make_outnames]; intros.
287 destruct (flatten_make_names_range
288 outname_gen t _ _ ltac:(eassumption)).
289 cleanup. eexists; eauto.
290 Qed.
291
292 Lemma flatten_make_outnames_NoDup t :
293 NoDup (flatten_base_ltype (make_outnames t)).
294 Proof. apply flatten_make_names_NoDup; auto. Qed.
295
296 Lemma make_innames_make_outnames_disjoint t1 t2 :
297 PropSet.disjoint
298 (varname_set_args (make_innames t1))
299 (varname_set_base (make_outnames t2)).
300 Proof.
301 rewrite varname_set_args_flatten.
302 rewrite varname_set_flatten.
303 apply NoDup_disjoint, NoDup_app_iff.
304 repeat match goal with
305 | _ => progress intros
306 | H : In _ _ |- _ => apply flatten_make_innames_exists in H;
307 destruct H
308 | H : In _ _ |- _ => apply flatten_make_outnames_exists in H;
309 destruct H
310 | H1 : ?x = inname_gen ?n1, H2 : ?x = outname_gen ?n2 |- _ =>
311 specialize (outname_gen_inname_gen_ok n2 n1); congruence
312 | |- ~ In _ _ => intro
313 | |- _ /\ _ => split
314 | _ => solve [eauto using flatten_make_innames_NoDup,
315 flatten_make_names_NoDup]
316 end.
317 Qed.
318 End proofs.
319 End with_parameters.