-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTadaModel.v
302 lines (245 loc) · 8.17 KB
/
TadaModel.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
Add LoadPath "c:\td202\GitHub\coq\views".
Require Import Heaps.
Require Import String.
Require Import SeparationAlgebras.
Require Import MSets.
Require Import SetoidClass.
Require Import Tactics.
Module Type RegionTypes.
Parameter rid : Type.
Parameter rtype : Type.
Parameter RT : rtype -> Type.
Parameter rbelong : rid -> rtype -> Prop.
Parameter rid_rtype : forall t r,
rbelong r t -> RT t.
Axiom rid_rtype_morphism : forall r t p1 p2, rid_rtype t r p1 = rid_rtype t r p2.
Parameter rbelong_dec : forall r t,
{rbelong r t} + {~rbelong r t}.
Axiom rid_rtype_injective : forall t r1 B1 r2 B2,
rid_rtype t r1 B1 = rid_rtype t r2 B2 ->
r1 = r2.
Parameter to_rid : forall t, RT t -> rid.
Axiom to_rid_belong : forall t (rr : RT t),
rbelong (to_rid t rr) t.
Axiom to_rid_inverse : forall t r B,
to_rid t (rid_rtype t r B) = r.
Axiom rid_rtype_inverse : forall t k,
k = rid_rtype t (to_rid t k) (to_rid_belong t k).
Parameter rfresh : rtype -> rid.
Axiom fresh_not_belong : forall t,
~ rbelong (rfresh t) t.
Parameter rtempty : rtype.
Axiom rtempty_not_belong : forall r,
~ rbelong r rtempty.
Parameter rtextend : forall t r, ~ rbelong r t -> rtype.
Axiom rtextend_extended : forall t r P r',
rbelong r' (rtextend t r P) <->
(r' = r \/ rbelong r' t).
Parameter RIDs : forall t, list (RT t).
Axiom RIDs_spec : forall t x, In x (RIDs t).
End RegionTypes.
(* I think it was premature to create a module type for
region types. Just use the concrete representation.
Otherwise, we have to export a notion of fold...
*)
Module RegionTypesNat <: RegionTypes.
Require Import ProofIrrelevance.
Module Nat_Sets := MSetList.Make Nat_as_OT. (*MSets.MSetAVL.Make Nat_as_OT.*)
Definition rid := nat.
Definition rtype := Nat_Sets.t.
Definition RT (t : rtype) := { r | Nat_Sets.In r t }.
Definition rbelong : rid -> rtype -> Prop := Nat_Sets.In.
Definition rid_rtype : forall t r,
rbelong r t -> RT t.
intros t r.
exists r.
trivial.
Defined.
Lemma rid_rtype_morphism : forall r t p1 p2, rid_rtype t r p1 = rid_rtype t r p2.
repeat intro.
generalize (proof_irrelevance _ p1 p2).
intro; subst; auto.
Qed.
Definition rbelong_dec : forall r t,
{rbelong r t} + {~rbelong r t}.
intros.
generalize (Nat_Sets.mem_spec t r).
destruct (Nat_Sets.mem r t); intuition.
Defined.
Proposition rid_rtype_injective : forall t r1 B1 r2 B2,
rid_rtype t r1 B1 = rid_rtype t r2 B2 ->
r1 = r2.
cbv; intuition.
inversion H; trivial.
Qed.
Definition to_rid t : RT t -> rid :=
proj1_sig (A:=rid) (P:=_).
Proposition to_rid_belong : forall t (rr : RT t),
rbelong (to_rid t rr) t.
cbv; intuition.
destruct rr; trivial.
Qed.
Proposition to_rid_inverse : forall t r B,
to_rid t (rid_rtype t r B) = r.
cbv; intuition.
Qed.
Proposition rid_rtype_inverse : forall t k,
k = rid_rtype t (to_rid t k) (to_rid_belong t k).
intros.
destruct k.
cbv.
f_equal.
apply proof_irrelevance.
Qed.
Definition rfresh (t : rtype) : rid :=
match (Nat_Sets.max_elt t) with
| Some r => S r
| None => 0
end.
Lemma fresh_not_belong : forall t,
~ rbelong (rfresh t) t.
cbv [rbelong rfresh rtype]; intuition.
remember (Nat_Sets.max_elt t) as x.
symmetry in Heqx.
destruct x.
generalize (Nat_Sets.max_elt_spec2 Heqx H).
auto.
eapply Nat_Sets.max_elt_spec3; eauto.
Qed.
Definition rtempty : rtype := Nat_Sets.empty.
Lemma rtempty_not_belong : forall r,
~ rbelong r rtempty.
apply Nat_Sets.empty_spec.
Qed.
Definition rtextend t r : ~ rbelong r t -> rtype :=
fun _ => Nat_Sets.add r t.
Proposition rtextend_extended : forall t r P r',
rbelong r' (rtextend t r P) <->
(r' = r \/ rbelong r' t).
cbv; intros.
generalize (Nat_Sets.add_spec t r r').
intuition.
Qed.
Lemma elements_belong (t : rtype) : forall x,
InA (@eq nat) x (Nat_Sets.elements t) ->
rbelong x t.
intros.
rewrite <- Nat_Sets.elements_spec1.
intuition.
Qed.
Definition RIDs0 (t : rtype) (l : list rid) : (forall x, In x l -> rbelong x t) -> list (RT t).
induction l; intros.
exact nil.
apply cons.
apply rid_rtype with a; intuition.
apply IHl; intuition.
Defined.
Lemma elements_belong' (t : rtype) : forall x,
In x (Nat_Sets.elements t) ->
rbelong x t.
intros.
rewrite <- Nat_Sets.elements_spec1.
intuition.
Qed.
Definition RIDs t := RIDs0 t (Nat_Sets.elements t) (elements_belong' t).
Lemma RIDs0_spec (t : rtype) (l : list rid) (H : forall x, In x l -> rbelong x t) :
forall k : RT t, In (to_rid _ k) l -> In k (RIDs0 t l H).
intros.
induction l; inversion H0; subst.
left.
rewrite rid_rtype_inverse.
f_equal.
apply proof_irrelevance.
right.
apply IHl; trivial.
Qed.
Lemma RIDs_spec (t : rtype) : forall x, In x (RIDs t).
intros.
apply RIDs0_spec.
generalize (Nat_Sets.elements_spec1 t (to_rid t x)); intro.
generalize (to_rid_belong t x).
intuition.
rewrite InA_alt in H1.
firstorder; subst; trivial.
Qed.
End RegionTypesNat.
Module TadaModel (ht : HeapTypes) .
Import RegionTypesNat.
Module Export TheHeap := MHeaps ht.
(* Action identifiers are pairs of string and lists of values *)
Definition AID := (string * (list Val))%type.
Record SA :=
{
sa_dom :> Type;
sa_setoid : Setoid sa_dom;
sa_op : partial_op sa_dom;
sa_sa : SepAlg sa_op
}.
Program Instance unit_Setoid : Setoid unit.
Instance unit_sa : SepAlg (fun _ _ => lift_val tt).
apply Build_SepAlg with (Setof.singleton tt); intuition.
cbv; auto.
exists tt; destruct m; cbv; intuition.
exists tt; intuition.
destruct m1; destruct m2; reflexivity.
Defined.
Definition unit_SA : SA := {| sa_dom := unit |}.
Definition Promises := (AID -> bool) -> nat.
Definition Witnesses := AID -> nat.
Definition RSA (t : rtype) := RT t -> SA.
Record LState (t : rtype) (rsat : RSA t) := {
ls_hp : store;
ls_gd : forall r : RT t, sa_dom (rsat r);
ls_pr : RT t -> Promises;
ls_wi : RT t -> Witnesses
}.
Definition SState (t : rtype) (rsat : RSA t) := RT t -> LState t rsat.
(* Type for interference specifications.
Choices: this does not need to be local (with respect to the guard) --
we will interpret it as the local closure.
*)
Definition IF_Spec (t : rtype) (rsat : RSA t) (r : RT t) :=
(sa_dom (rsat r)) -> AID -> relation (SState t rsat).
Record PreWorld (t : rtype) :=
{
world_rsa : RSA t;
world_local : LState t world_rsa;
world_shared : SState t world_rsa;
world_if_spec : forall r : RT t, IF_Spec t world_rsa r
}.
(* This should be factored out. *)
Definition partial_val_fmap {A} (f : A -> A) :
partial_val (T := A) -> partial_val (T:=A) :=
fun v => match v with
| {| defined := defined; val := val |} =>
{| defined := defined; val := f val |}
end.
Fixpoint sa_op_list {s : SA} (init : s) (l : list s)
: partial_val (T:=s) :=
match l with
| nil => lift_val init
| cons a l' => let (d, v) := sa_op_list init l' in
let (d', v') := sa_op _ a v in
{| defined := d /\ d'; val := v' |}
end.
Definition guard_total {t : rtype} (w : PreWorld t) (r : RT t)
: partial_val (T := world_rsa t w r) :=
let l :=
map (fun r0 : RT t => ls_gd t (world_rsa t w) (world_shared t w r0) r)
(RIDs t) in
sa_op_list (ls_gd t (world_rsa t w) (world_local t w) r) l.
Record World (t : rtype) :=
{
world_wrld :> PreWorld t;
world_well_guarded : forall (r : RT t), defined (guard_total world_wrld r)
}.
(* TODO:
1. Define Rely relation
2. Define Guarantee relation
3. Prove compatability.
*)
(* Guarantee relation.
An update should be permitted if it is permitted by all regions.
An update is permitted by a region if it is the identity on that region.
An update is permitted by
End TadaModel.