forked from formalize/coq-vyper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MapSig.v
341 lines (302 loc) · 9.37 KB
/
MapSig.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
From Coq Require Import Eqdep_dec List.
Require Map.
(** MapSig implements a Map interface for [sig] types (that is, dependent pairs [{x: A| P x}]). *)
Section MapSig.
Context {Key: Type}
{KeyEqDec: forall x y: Key, {x = y} + {x <> y}}
{KeyIsGood: Key -> bool}
{Value: Type}
{M: Type}
(MInst: Map.class KeyEqDec Value M).
Definition t := { m: M | forall key: Key, match Map.lookup m key with
| Some _ => KeyIsGood key = true
| None => True
end }.
Definition key_sig := { key: Key | KeyIsGood key = true }.
Lemma key_sig_eq {a b: key_sig}
(H: proj1_sig a = proj1_sig b):
a = b.
Proof.
destruct a as (x, p).
destruct b as (y, q).
cbn in H. subst y.
f_equal.
apply eq_proofs_unicity.
decide equality.
Qed.
Lemma key_sig_eq_dec_helper {a b: key_sig} (NE: proj1_sig a <> proj1_sig b):
a <> b.
Proof.
intro H. subst b. contradiction.
Qed.
Definition key_sig_eq_dec (a b: key_sig)
: {a = b} + {a <> b}
:= match KeyEqDec (proj1_sig a) (proj1_sig b) as k return _ = k -> _ with
| left e =>fun E => left (key_sig_eq e)
| right n => fun E => right (key_sig_eq_dec_helper n)
end eq_refl.
Definition lookup (m: t) (k: key_sig)
: option Value
:= Map.lookup (proj1_sig m) (proj1_sig k).
Definition is_empty (m: t)
: bool
:= Map.is_empty (proj1_sig m).
Lemma is_empty_ok (m: t):
is_empty m = true
<->
forall key: key_sig,
lookup m key = None.
Proof.
unfold is_empty. unfold lookup.
rewrite Map.is_empty_ok.
assert (MOk := proj2_sig m). cbn in MOk.
split. { intros H key. apply H. }
intros H key.
assert (MOkKey := MOk key).
remember (Map.lookup (proj1_sig m) key) as v.
destruct v. 2:trivial.
assert (A := (H (exist _ key MOkKey))). cbn in A.
rewrite Heqv. rewrite A. trivial.
Qed.
Local Lemma empty_spec (key: Key):
match Map.lookup Map.empty key with
| Some _ => KeyIsGood key = true
| None => True
end.
Proof.
now rewrite Map.empty_lookup.
Qed.
Definition empty
: t
:= exist _ Map.empty empty_spec.
Lemma empty_ok: is_empty empty = true.
Proof.
unfold is_empty. cbn. now rewrite Map.empty_ok.
Qed.
Local Lemma insert_spec (m: t) (key: key_sig) (value: Value) (query: Key):
match Map.lookup (Map.insert (proj1_sig m) (proj1_sig key) value) query with
| Some _ => KeyIsGood query = true
| None => True
end.
Proof.
rewrite Map.insert_ok.
destruct (KeyEqDec (proj1_sig key) query).
{ subst query. apply (proj2_sig key). }
exact (proj2_sig m query).
Qed.
Definition insert (m: t) (key: key_sig) (value: Value)
: t
:= exist _ (Map.insert (proj1_sig m) (proj1_sig key) value) (insert_spec m key value).
Lemma insert_ok (m: t) (key: key_sig) (value: Value) (x: key_sig):
lookup (insert m key value) x
=
if key_sig_eq_dec key x
then Some value
else lookup m x.
Proof.
unfold lookup. unfold insert. cbn. rewrite Map.insert_ok.
unfold key_sig_eq_dec.
remember (KeyEqDec (proj1_sig key) (proj1_sig x)) as e.
now destruct e.
Qed.
Local Lemma remove_spec (m: t) (key: key_sig) (query: Key):
match Map.lookup (Map.remove (proj1_sig m) (proj1_sig key)) query with
| Some _ => KeyIsGood query = true
| None => True
end.
Proof.
rewrite Map.remove_ok.
destruct (KeyEqDec (proj1_sig key) query). { trivial. }
exact (proj2_sig m query).
Qed.
Definition remove (m: t) (key: key_sig)
: t
:= exist _ (Map.remove (proj1_sig m) (proj1_sig key)) (remove_spec m key).
Lemma remove_ok (m: t) (key: key_sig) (x: key_sig):
lookup (remove m key) x
=
if key_sig_eq_dec key x
then None
else lookup m x.
Proof.
unfold lookup. unfold remove. cbn. rewrite Map.remove_ok.
unfold key_sig_eq_dec.
remember (KeyEqDec (proj1_sig key) (proj1_sig x)) as e.
now destruct e.
Qed.
Local Lemma mark_helper_head {l : list (Key * Value)}
{h: Key * Value}
{t: list (Key * Value)}
(Ok : Forall (fun kv : Key * Value => KeyIsGood (fst kv) = true) l)
(E: l = h :: t):
KeyIsGood (fst h) = true.
Proof.
subst l. inversion Ok. assumption.
Qed.
Local Lemma mark_helper_tail {l : list (Key * Value)}
{h: Key * Value}
{t: list (Key * Value)}
(Ok : Forall (fun kv : Key * Value => KeyIsGood (fst kv) = true) l)
(E: l = h :: t):
Forall (fun kv : Key * Value => KeyIsGood (fst kv) = true) t.
Proof.
subst l. inversion Ok. assumption.
Qed.
Fixpoint mark (l: list (Key * Value))
(Ok: Forall (fun kv: Key * Value => KeyIsGood (fst kv) = true) l)
: list (key_sig * Value)
:= match l as l' return l = l' -> _ with
| nil => fun _ => nil
| cons h t => fun E => cons (exist _ (fst h) (mark_helper_head Ok E), snd h)
(mark t (mark_helper_tail Ok E))
end eq_refl.
Lemma mark_map_proj1_sig (l: list (Key * Value))
(Ok: Forall (fun kv: Key * Value => KeyIsGood (fst kv) = true) l):
map (@proj1_sig _ _) (map fst (mark l Ok)) = map fst l.
Proof.
induction l. { easy. }
cbn. f_equal. apply IHl.
Qed.
Local Lemma items_helper (m: t):
Forall (fun kv : Key * Value => KeyIsGood (fst kv) = true)
(Map.items (proj1_sig m)).
Proof.
assert (MOk := proj2_sig m).
assert (Ok: forall key: Key,
match Map.alist_lookup KeyEqDec (Map.items (proj1_sig m)) key with
| Some value => Map.lookup (proj1_sig m) key = Some value
| None => True
end).
{
intro key.
rewrite<- Map.items_ok.
now destruct Map.lookup.
}
assert (ND := Map.items_nodup (proj1_sig m)).
induction (Map.items (proj1_sig m)); constructor.
{ (* goal: KeyIsGood (fst a) = true *)
assert (MOkA := MOk (fst a)).
assert (OkA := Ok (fst a)).
destruct a as (key, value). cbn in *.
destruct (KeyEqDec key key). 2:contradiction.
now destruct (Map.lookup (proj1_sig m) key).
}
apply IHl. 2:{ now inversion ND. }
intro query. cbn in Ok.
assert (Q := Ok query).
destruct a as (key, value).
destruct (KeyEqDec query key). 2:{ exact Q. }
subst query.
cbn in ND.
inversion ND.
rewrite (Map.alist_lookup_not_in KeyEqDec l key) by assumption.
trivial.
Qed.
Definition items (m: t) := mark (Map.items (proj1_sig m)) (items_helper m).
Lemma items_ok (m: t) (key: key_sig):
lookup m key = Map.alist_lookup key_sig_eq_dec (items m) key.
Proof.
unfold lookup. rewrite Map.items_ok. unfold items.
remember (items_helper m) as F. clear HeqF. revert F.
induction (Map.items (proj1_sig m)); intro F. { easy. }
cbn. destruct a as (k, v). destruct key as (query, QueryIsGood).
unfold key_sig_eq_dec. cbn in *.
remember (KeyEqDec query k) as e. destruct e. { trivial. }
apply IHl.
Qed.
Lemma key_sig_list_not_in {l: list key_sig} {key: key_sig}
(H: ~ In (proj1_sig key) (map (@proj1_sig _ _) l)):
~ In key l.
Proof.
induction l. { easy. }
intro I. inversion I; subst; cbn in *; tauto.
Qed.
Lemma key_sig_list_nodup {l: list key_sig}
(H: NoDup (map (@proj1_sig _ _) l)):
NoDup l.
Proof.
induction l. { constructor. }
constructor.
{
inversion H. subst.
induction l. { easy. }
apply key_sig_list_not_in.
assumption.
}
apply IHl. now inversion H.
Qed.
Lemma items_nodup (m: t):
NoDup (map fst (items m)).
Proof.
apply key_sig_list_nodup.
unfold items.
rewrite mark_map_proj1_sig.
apply Map.items_nodup.
Qed.
Local Lemma merge_spec (newer older: t) (key: Key):
match Map.lookup (Map.merge (proj1_sig newer) (proj1_sig older)) key with
| Some _ => KeyIsGood key = true
| None => True
end.
Proof.
rewrite Map.merge_ok.
assert (OkN := proj2_sig newer key).
assert (OkO := proj2_sig older key).
now destruct (Map.lookup (proj1_sig newer) key).
Qed.
Definition merge (newer older: t)
: t
:= exist _ (Map.merge (proj1_sig newer) (proj1_sig older)) (merge_spec newer older).
Lemma merge_ok (newer older: t) (key: key_sig):
lookup (merge newer older) key
=
match lookup newer key with
| Some value => Some value
| None => lookup older key
end.
Proof.
unfold lookup. cbn.
rewrite Map.merge_ok.
trivial.
Qed.
Local Lemma map_endo_spec (m: t) (f: Value -> Value) (key: Key):
match Map.lookup (Map.map_endo (proj1_sig m) f) key with
| Some _ => KeyIsGood key = true
| None => True
end.
Proof.
rewrite Map.map_endo_ok.
assert (C := proj2_sig m key).
now destruct Map.lookup.
Qed.
Definition map_endo (m: t) (f: Value -> Value)
: t
:= exist _ (Map.map_endo (proj1_sig m) f) (map_endo_spec m f).
Lemma map_endo_ok (m: t) (f: Value -> Value) (key: key_sig):
lookup (map_endo m f) key = match lookup m key with
| Some value => Some (f value)
| None => None
end.
Proof.
unfold map_endo. unfold lookup. cbn.
apply Map.map_endo_ok.
Qed.
Instance instance: Map.class key_sig_eq_dec Value t
:= {| Map.lookup := lookup
; Map.is_empty := is_empty
; Map.is_empty_ok := is_empty_ok
; Map.empty := empty
; Map.empty_ok := empty_ok
; Map.insert := insert
; Map.insert_ok := insert_ok
; Map.remove := remove
; Map.remove_ok := remove_ok
; Map.items := items
; Map.items_ok := items_ok
; Map.items_nodup := items_nodup
; Map.merge := merge
; Map.merge_ok := merge_ok
; Map.map_endo := map_endo
; Map.map_endo_ok := map_endo_ok
|}.
End MapSig.