-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathAssoc.v
278 lines (247 loc) · 6.57 KB
/
Assoc.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
Require Import List.
Import ListNotations.
Require Import StructTact.StructTactics.
Set Implicit Arguments.
Section assoc.
Variable K V : Type.
Variable K_eq_dec : forall k k' : K, {k = k'} + {k <> k'}.
Fixpoint assoc (l : list (K * V)) (k : K) : option V :=
match l with
| [] => None
| (k', v) :: l' =>
if K_eq_dec k k' then
Some v
else
assoc l' k
end.
Definition assoc_default (l : list (K * V)) (k : K) (default : V) : V :=
match (assoc l k) with
| Some x => x
| None => default
end.
Fixpoint assoc_set (l : list (K * V)) (k : K) (v : V) : list (K * V) :=
match l with
| [] => [(k, v)]
| (k', v') :: l' =>
if K_eq_dec k k' then
(k, v) :: l'
else
(k', v') :: (assoc_set l' k v)
end.
Fixpoint assoc_del (l : list (K * V)) (k : K) : list (K * V) :=
match l with
| [] => []
| (k', v') :: l' =>
if K_eq_dec k k' then
assoc_del l' k
else
(k', v') :: (assoc_del l' k)
end.
Lemma get_set_same :
forall k v l,
assoc (assoc_set l k v) k = Some v.
Proof using.
induction l; intros; simpl; repeat (break_match; simpl); subst; congruence.
Qed.
Lemma get_set_same' :
forall k k' v l,
k = k' ->
assoc (assoc_set l k v) k' = Some v.
Proof using.
intros. subst. auto using get_set_same.
Qed.
Lemma get_set_diff :
forall k k' v l,
k <> k' ->
assoc (assoc_set l k v) k' = assoc l k'.
Proof using.
induction l; intros; simpl; repeat (break_match; simpl); subst; try congruence; auto.
Qed.
Lemma not_in_assoc :
forall k l,
~ In k (map (@fst _ _) l) ->
assoc l k = None.
Proof using.
intros.
induction l.
- auto.
- simpl in *. repeat break_match; intuition.
subst. simpl in *. congruence.
Qed.
Lemma get_del_same :
forall k l,
assoc (assoc_del l k) k = None.
Proof using.
induction l; intros; simpl in *.
- auto.
- repeat break_match; subst; simpl in *; auto.
break_if; try congruence.
Qed.
Lemma get_del_diff :
forall k k' l,
k <> k' ->
assoc (assoc_del l k') k = assoc l k.
Proof using.
induction l; intros; simpl in *.
- auto.
- repeat (break_match; simpl); subst; try congruence; auto.
Qed.
Lemma get_set_diff_default :
forall (k k' : K) (v : V) l d,
k <> k' ->
assoc_default (assoc_set l k v) k' d = assoc_default l k' d.
Proof using.
unfold assoc_default.
intros.
repeat break_match; auto;
rewrite get_set_diff in * by auto; congruence.
Qed.
Lemma get_set_same_default :
forall (k : K) (v : V) l d,
assoc_default (assoc_set l k v) k d = v.
Proof using.
unfold assoc_default.
intros.
repeat break_match; auto;
rewrite get_set_same in *; congruence.
Qed.
Lemma assoc_assoc_default:
forall l k (v : V) d,
assoc l k = Some v ->
assoc_default l k d = v.
Proof using.
intros. unfold assoc_default.
break_match; congruence.
Qed.
Lemma assoc_assoc_default_missing:
forall (l : list (K * V)) k d,
assoc l k = None ->
assoc_default l k d = d.
Proof using.
intros. unfold assoc_default.
break_match; congruence.
Qed.
Lemma assoc_set_same :
forall (l : list (K * V)) k v,
assoc l k = Some v ->
assoc_set l k v = l.
Proof using.
intros. induction l; simpl in *; auto; try congruence.
repeat break_match; simpl in *; intuition.
- subst. find_inversion. auto.
- repeat find_rewrite. auto.
Qed.
Lemma assoc_default_assoc_set :
forall l (k : K) (v : V) d,
assoc_default (assoc_set l k v) k d = v.
Proof using.
intros. unfold assoc_default.
rewrite get_set_same. auto.
Qed.
Lemma assoc_set_assoc_set_same :
forall l (k : K) (v : V) v',
assoc_set (assoc_set l k v) k v' = assoc_set l k v'.
Proof using.
induction l; intros; simpl in *; repeat break_match; simpl in *; subst; try congruence; eauto;
break_if; congruence.
Qed.
Definition a_equiv (l1 : list (K * V)) l2 :=
forall k,
assoc l1 k = assoc l2 k.
Lemma a_equiv_refl :
forall l,
a_equiv l l.
Proof using.
intros. unfold a_equiv. auto.
Qed.
Lemma a_equiv_sym :
forall l l',
a_equiv l l' ->
a_equiv l' l.
Proof using.
unfold a_equiv. intros. auto.
Qed.
Lemma a_equiv_trans :
forall l l' l'',
a_equiv l l' ->
a_equiv l' l'' ->
a_equiv l l''.
Proof using.
unfold a_equiv in *.
intros. repeat find_higher_order_rewrite.
auto.
Qed.
Ltac assoc_destruct :=
match goal with
| [ |- context [assoc (assoc_set _ ?k0' _) ?k0 ] ] =>
destruct (K_eq_dec k0 k0'); [subst k0'; rewrite get_set_same with (k := k0)|
rewrite get_set_diff with (k' := k0) by auto]
end.
Ltac assoc_rewrite :=
match goal with
| [ |- context [assoc (assoc_set _ ?k0' _) ?k0 ] ] =>
first [rewrite get_set_same with (k := k0) by auto |
rewrite get_set_diff with (k' := k0) by auto ]
end.
Lemma assoc_set_assoc_set_diff :
forall l (k : K) (v : V) k' v',
k <> k' ->
a_equiv (assoc_set (assoc_set l k v) k' v')
(assoc_set (assoc_set l k' v') k v).
Proof using.
unfold a_equiv.
intros.
assoc_destruct.
- now repeat assoc_rewrite.
- assoc_destruct.
+ now repeat assoc_rewrite.
+ now repeat assoc_rewrite.
Qed.
Lemma a_equiv_nil :
forall l,
a_equiv l [] ->
l = [].
Proof using.
intros.
destruct l; auto.
unfold a_equiv in *. simpl in *.
destruct p.
specialize (H k).
break_if; try congruence.
Qed.
Lemma assoc_set_a_equiv :
forall l l' (k : K) (v : V),
a_equiv l l' ->
a_equiv (assoc_set l k v) (assoc_set l' k v).
Proof using.
unfold a_equiv.
intros.
assoc_destruct; assoc_rewrite; auto.
Qed.
Lemma assoc_default_a_equiv :
forall l l' (k : K) (v : V),
a_equiv l l' ->
assoc_default l k v = assoc_default l' k v.
Proof using.
intros. unfold a_equiv, assoc_default in *.
find_higher_order_rewrite.
auto.
Qed.
Lemma assoc_a_equiv :
forall l l' (k : K),
a_equiv l l' ->
assoc l k = assoc l' k.
Proof using.
unfold a_equiv.
auto.
Qed.
Lemma assoc_default_assoc_set_diff :
forall (l : list (K * V)) k v k' d,
k <> k' ->
assoc_default (assoc_set l k' v) k d =
assoc_default l k d.
Proof using.
intros. unfold assoc_default. rewrite get_set_diff; auto.
Qed.
End assoc.
Arguments a_equiv {_} {_} _ _ _.