-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mem.v
255 lines (211 loc) · 5.64 KB
/
Mem.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
From Coq Require Export FunctionalExtensionality.
From Coq Require Import Setoid.
From Tactical Require Import
Propositional
SimplMatch.
Require Import SepLogic.Instances.
Set Implicit Arguments.
(* for compatibility with coq master *)
Set Warnings "-undeclared-scope".
Section Memory.
Context (A V:Type).
Record mem :=
mkMem { mem_read :> A -> option V }.
Implicit Types (a:A) (v:V) (m:mem).
Definition empty : mem :=
mkMem (fun _ => None).
Definition union m1 m2 : mem :=
mkMem (fun x => match m1 x with
| Some v => Some v
| None => m2 x
end).
Definition disjoint m1 m2 :=
forall x v, m1 x = Some v -> forall v', m2 x = Some v' -> False.
Infix "#" := disjoint (at level 70, no associativity).
Infix "+" := union.
Theorem disjoint_match1 m1 m2 :
m1 # m2 <->
(forall x, match m1 x with
| Some v => m2 x = None
| None => True
end).
Proof.
unfold disjoint; split; intros.
destruct_with_eqn (m1 x); destruct_with_eqn (m2 x); eauto.
exfalso; eauto.
specialize (H x).
replace (m1 x) in H.
congruence.
Qed.
Theorem disjoint_sym m1 m2 :
m1 # m2 <-> m2 # m1.
Proof.
firstorder.
Qed.
Theorem disjoint_sym1 m1 m2 :
m1 # m2 -> m2 # m1.
Proof.
firstorder.
Qed.
Global Instance disjoint_symmetric : Symmetric disjoint.
exact disjoint_sym1.
Qed.
Theorem disjoint_match2 m1 m2 :
m1 # m2 <->
(forall x, match m2 x with
| Some v => m1 x = None
| None => True
end).
Proof.
setoid_rewrite disjoint_sym.
apply disjoint_match1.
Qed.
Context {Aeq: EqDec A}.
Definition upd (m: mem) (a0:A) (v:V) : mem :=
mkMem (fun a => if a0 == a then Some v else m a).
Theorem mem_ext_eq m1 m2 :
(forall a, m1 a = m2 a) ->
m1 = m2.
Proof.
intros.
destruct m1, m2.
f_equal.
extensionality a; auto.
Qed.
Hint Unfold mem_read : core.
Hint Unfold upd empty disjoint union : mem.
Ltac t :=
autounfold with mem;
simpl;
repeat match goal with
| [ m: mem |- _ ] =>
destruct m as m
| |- context[mem_read] => progress simpl
| |- @eq mem _ _ => apply mem_ext_eq; intros
| _ => progress destruct matches
| _ => progress propositional
| _ => congruence
| _ => solve [ eauto ]
| _ => solve [ exfalso; eauto ]
end.
Theorem upd_eq m a v :
upd m a v a = Some v.
Proof. t. Qed.
Theorem upd_ne m a v a' :
a <> a' ->
upd m a v a' = m a'.
Proof. t. Qed.
Theorem upd_upd m a v v' :
upd (upd m a v) a v' = upd m a v'.
Proof. t. Qed.
Theorem upd_upd_ne m a v a' v' :
a <> a' ->
upd (upd m a v) a' v' = upd (upd m a' v') a v.
Proof. t. Qed.
Theorem empty_disjoint1 m :
m # empty.
Proof. t. Qed.
Theorem empty_disjoint2 m :
empty # m.
Proof. t. Qed.
Theorem empty_union1 m :
m + empty = m.
Proof. t. Qed.
Theorem empty_union2 m :
empty + m = m.
Proof. t. Qed.
Theorem disjoint_union_comm m1 m2 :
m1 # m2 ->
m1 + m2 = m2 + m1.
Proof. t. Qed.
Theorem union_disjoint1 m m1 m2 :
m # (m1 + m2) ->
m # m1.
Proof.
t.
specialize (H _ _ ltac:(eauto)).
replace (m1 x) in *; t.
Qed.
Theorem union_disjoint2 m m1 m2 :
m # (m1 + m2) ->
m # m2.
Proof.
t.
specialize (H _ _ ltac:(eauto)).
destruct_with_eqn (m1 x); t.
Qed.
Theorem union_disjoint_elim m m1 m2 :
m # (m1 + m2) ->
m # m1 /\ m # m2.
Proof.
split; eauto using union_disjoint1, union_disjoint2.
Qed.
Theorem union_disjoint_intro m m1 m2 :
m # m1 ->
m # m2 ->
m # (m1 + m2).
Proof.
t.
destruct_with_eqn (m1 x); t.
Qed.
Theorem union_assoc m1 m2 m3 :
m1 + m2 + m3 = m1 + (m2 + m3).
Proof. t. Qed.
Definition singleton a v : mem := upd empty a v.
Hint Unfold singleton : mem.
Theorem disjoint_different_singleton m a v v' :
m # singleton a v ->
m # singleton a v'.
Proof.
t.
destruct matches in *;
repeat match goal with
| [ H: Some _ = Some _ |- _ ] =>
inversion H; subst; clear H
end.
specialize (H _ _ ltac:(eauto)).
rewrite Heqs in *; eauto.
Qed.
Lemma disjoint_from_singleton a v m :
m a = None ->
disjoint (singleton a v) m.
Proof.
t.
destruct (a == x); try congruence.
Qed.
Lemma disjoint_singleton_oob a v m :
disjoint (singleton a v) m ->
m a = None.
Proof.
t.
destruct_with_eqn (m a); eauto.
specialize (H a v).
destruct (a == a); try congruence.
exfalso; eauto.
Qed.
Theorem singleton_eq a v :
singleton a v a = Some v.
Proof. t. Qed.
Theorem singleton_ne a v a' :
a <> a' ->
singleton a v a' = None.
Proof. t. Qed.
End Memory.
Opaque upd singleton.
Arguments empty A V : clear implicits.
Module MemNotations.
(* Declare Scope mem_scope. *)
Delimit Scope mem_scope with mem.
Infix "#" := disjoint (at level 70, no associativity) : mem_scope.
Infix "+" := union : mem_scope.
Notation "m [ a := v ]" := (upd m a v) (at level 12, left associativity) : mem_scope.
(* we need to enable printing of the coercion to use the notation (as opposed
to printing an application) *)
Add Printing Coercion mem_read.
Notation "m [ a ]" := (mem_read m a) (at level 13, no associativity) : mem_scope.
End MemNotations.
Global Hint Rewrite upd_eq : upd.
Global Hint Rewrite upd_ne using solve [ trivial || congruence ] : upd.
Global Hint Rewrite singleton_eq : upd.
Global Hint Rewrite singleton_ne using solve [ trivial || congruence ] : upd.
Global Hint Rewrite upd_upd : upd.