-
Notifications
You must be signed in to change notification settings - Fork 1
/
semantics.v
433 lines (398 loc) · 18.8 KB
/
semantics.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
Require Export heap.
Require Export Omega.
Require Export hetList.
Require Export Coq.Program.Equality.
(*evaluation context decomposition*)
Inductive decompose : term -> ctxt -> term -> Prop :=
|decompApp : forall e1 e2 E e, decompose e1 E e ->
decompose (app e1 e2) (appCtxt e2 E) e
|decompAppVal : forall v e2 E e (prf:value v),
decompose e2 E e -> decompose (app v e2) (appValCtxt v prf E) e
|appHole : forall e v, value v -> decompose (app (lambda e) v) hole (app (lambda e) v)
|decompGet : forall e E e', decompose e E e' ->
decompose (get e) (getCtxt E) e'
|decompGetHole : forall l, decompose (get (loc l)) hole (get (loc l))
|decompPut : forall e1 e2 E e, decompose e1 E e ->
decompose (put e1 e2) (putCtxt e2 E) e
|decompPutVal : forall v e2 E e (prf:value v),
decompose e2 E e ->
decompose (put v e2) (putValCtxt v prf E) e
|decompPutHole : forall n v, value v -> decompose (put (loc n) v) hole (put (loc n) v)
|decompAlloc : forall e E e', decompose e E e' ->
decompose (alloc e) (allocCtxt E) e'
|decompAllocHole : forall v, value v -> decompose (alloc v) hole (alloc v)
|decompAtomicHole : forall e, decompose (atomic e) hole (atomic e)
|decompFork : forall e, decompose (fork e) hole (fork e)
|decompInAtomic : forall e e' E, decompose e E e' ->
decompose (inatomic e) (inatomicCtxt E) e'
|decompInAtomicHole : forall v, value v -> decompose (inatomic v) hole (inatomic v).
(*values cannot be decomposed*)
Theorem decomposeValFalse : forall t E e, value t -> decompose t E e -> False.
Proof.
intros t E e v d. inv d; try solve[inv v].
Qed.
(*proves that values cannot be decomposed*)
Ltac decompVal :=
match goal with
|H:value ?t, H':decompose ?t ?E ?e |- _ =>
apply decomposeValFalse in H'; auto; inv H'
|H:decompose (lambda ?e) ?E ?e' |- _ => inv H
|H:decompose (loc ?l) ?E ?e |- _ => inv H
end.
(*inverts a decomposition nested inside another*)
Ltac invertDecomp :=
match goal with
|H:decompose (app ?e1 ?e2) ?E ?e |- _ => inv H
|H:decompose (get ?e) ?E ?e' |- _ => inv H
|H:decompose (put ?e1 ?e2) ?E ?e |- _ => inv H
|H:decompose (alloc ?e) ?E ?e' |- _ => inv H
|H:decompose (fork ?e) ?E ?e' |- _ => inv H
|H:decompose (inatomic ?e) ?E ?E' |- _ => inv H
end.
(*evaluation context decomposition is deterministic*)
Theorem decomposeDeterministic : forall t E E' e e',
decompose t E e -> decompose t E' e' ->
E = E' /\ e = e'.
Proof.
intros t E E' e e' HD1 HD2. genDeps {{ E'; e'}}.
induction HD1; intros; try(invertDecomp; try decompVal); try solve[auto |
match goal with
|H:forall e'0 E', decompose ?e E' e'0 -> ?E = E' /\ ?e' = e'0,
H':decompose ?e ?E'' ?e'' |- _ => apply H in H'; invertHyp; eauto; try (proofsEq; eauto)
end].
inv HD2. eauto.
Qed.
(*proves that the results of the same decomposition are the same*)
Ltac decompSame :=
match goal with
|H:decompose ?t ?E ?e,H':decompose ?t ?E' ?e' |- _ =>
eapply decomposeDeterministic in H; eauto; invertHyp
end.
(*fill an evaluation context*)
Fixpoint fill (E:ctxt) (e:term) :=
match E with
|appCtxt e' E => app (fill E e) e'
|appValCtxt v _ E => app v (fill E e)
|getCtxt E => get (fill E e)
|putCtxt e' E => put (fill E e) e'
|putValCtxt v _ E => put v (fill E e)
|allocCtxt E => alloc (fill E e)
|inatomicCtxt E => inatomic (fill E e)
|hole => e
end.
(*
commit H ==> indicates the log is valid and H contains the new writes from the log
abort e L ==> log was invalid, resume execution at term e with log L
*)
Inductive validateRes : Type :=
|commit : heap -> validateRes
|abort : term -> log -> validateRes.
(*Transactional log validation*)
Inductive validate : stamp -> log -> heap -> stamp -> validateRes -> Prop :=
|validateNil : forall S S' H, validate S nil H S' (commit H)
|validateCommitRead : forall S S' S'' l v E H H' L,
lookup H l = Some(v, S') -> S > S' ->
validate S L H S'' (commit H') ->
validate S (readItem l E v::L) H S'' (commit H')
|validateAbortPropogate : forall S S' L H x L' e,
validate S L H S' (abort e L') ->
validate S (x::L) H S' (abort e L')
|validateAbortRead : forall S S' S'' H L E H' l v v',
validate S L H S'' (commit H') -> lookup H l = Some(v, S') ->
S' > S -> validate S (readItem l E v'::L) H S''
(abort (fill E(get(loc l))) L)
|validateWrite : forall S S' L H H' l v,
validate S L H S' (commit H') ->
validate S (writeItem l v::L) H S' (commit ((l, v, S')::H'))
.
(*lookup a term in a thread's write set*)
Fixpoint logLookup (L:log) (l:location) :=
match L with
|readItem _ _ _::L' => logLookup L' l
|writeItem l' v::L' => if eq_nat_dec l l'
then Some v
else logLookup L' l
|nil => None
end.
Fixpoint open (e:term) (k:nat) (e':term) :=
match e with
|lambda e => lambda (open e (S k) e')
|loc l => loc l
|unit => unit
|var k' => if eq_nat_dec k k'
then e'
else var k'
|app e1 e2 => app (open e1 k e') (open e2 k e')
|get e => get (open e k e')
|put e1 e2 => put (open e1 k e') (open e2 k e')
|alloc e => alloc (open e k e')
|fork e => fork (open e k e')
|atomic e => atomic (open e k e')
|inatomic e => inatomic (open e k e')
end.
(*transactional step (used by both p_step and f_step)*)
Inductive trans_step (H:heap) : thread -> thread -> Prop :=
|t_readStep : forall S L E l t v e0 S',
decompose t E (get (loc l)) -> logLookup L l = None ->
lookup H l = Some(v, S') -> S > S' ->
trans_step H (Some(S, e0), L, t)
(Some(S, e0), readItem l E v::L, fill E v)
|t_readInDomainStep : forall S l v L E t e0,
decompose t E (get (loc l)) -> logLookup L l = Some v ->
trans_step H (Some(S, e0), L, t) (Some(S, e0), L, fill E v)
|t_writeStep : forall S L E l v t,
decompose t E (put (loc l) v) -> S <> None ->
trans_step H (S, L, t) (S, writeItem l v::L, fill E unit)
|t_atomicIdemStep : forall E e t L S,
decompose t E (atomic e) -> S <> None ->
trans_step H (S, L, t) (S, L, fill E e)
|t_betaStep : forall L E e t v S,
decompose t E (app (lambda e) v) -> S <> None ->
trans_step H (S, L, t) (S, L, fill E (open e 0 v))
.
(*same as trans_step with addition of r_readStepInvalid*)
Inductive replay_step H : thread -> thread -> Prop :=
|r_readStepValid : forall S L E l t v e0 S',
lookup H l = Some(v, S') -> S > S' ->
decompose t E (get (loc l)) -> logLookup L l = None ->
replay_step H (Some(S, e0), L, t) (Some(S, e0), readItem l E v::L, fill E v)
|r_readStepInvalid : forall S L E v' l t v e0 S',
lookup H l = Some(v',S') -> S' >= S ->
decompose t E (get (loc l)) -> logLookup L l = None ->
replay_step H (Some(S, e0), L, t) (Some(S, e0), readItem l E v::L, fill E v)
|r_readInDomainStep : forall S l v L E t e0,
decompose t E (get (loc l)) -> logLookup L l = Some v ->
replay_step H (Some(S, e0), L, t) (Some(S, e0), L, fill E v)
|r_writeStep : forall S L E l v t,
decompose t E (put (loc l) v) -> S <> None ->
replay_step H (S, L, t) (S, writeItem l v::L, fill E unit)
|r_atomicIdemStep : forall E e t L S,
decompose t E (atomic e) -> S <> None ->
replay_step H (S, L, t) (S, L, fill E e)
|r_betaStep : forall L E e t v S,
decompose t E (app (lambda e) v) -> S <> None ->
replay_step H (S, L, t) (S, L, fill E (open e 0 v))
.
(*reflexive transitive closure of replay_step*)
Inductive replay H : thread -> thread -> Prop :=
|replayRefl : forall t, replay H t t
|replayStep : forall t t' t'',
replay_step H t t' -> replay H t' t'' ->
replay H t t''.
(*left recursive version of replay*)
Inductive rewind H : thread -> thread -> Prop :=
|rewindRefl : forall t, rewind H t t
|rewindStep : forall t t' t'',
rewind H t t' -> replay_step H t' t'' ->
rewind H t t''.
(*parital abort STM semantics (single step)*)
Inductive p_step : nat -> heap -> pool -> nat -> heap -> pool -> Prop :=
|p_transStep : forall C H t t', trans_step H t t' ->
p_step C H (Single t) C H (Single t')
|p_parLStep : forall C H T1 T2 C' H' T1',
p_step C H T1 C' H' T1' -> p_step C H (Par T1 T2) C' H' (Par T1' T2)
|p_parRStep : forall C H T1 T2 C' H' T2',
p_step C H T2 C' H' T2' -> p_step C H (Par T1 T2) C' H' (Par T1 T2')
|p_forkStep : forall C H E e t,
decompose t E (fork e) ->
p_step C H (Single(None, nil, t)) C H
(Par (Single(None, nil, fill E unit)) (Single(None, nil, e)))
|p_eagerAbort : forall L S H L' C e0 e' t v E l S',
lookup H l = Some(v, S') -> S < S' ->
validate S (readItem l E v::L) H C (abort e' L') ->
decompose t E (get (loc l)) -> logLookup L l = None ->
p_step C H (Single (Some(S, e0), L, t))
(C+1) H (Single(Some(C, e0), L', e'))
|p_abortStep : forall L S H L' C e e0 e' S',
validate S L H S' (abort e' L') ->
p_step C H (Single(Some(S, e0), L, e))
(plus 1 C) H (Single(Some(C, e0), L', e'))
|p_allocStep : forall C H v E t l,
lookup H l = None -> decompose t E (alloc v) ->
p_step C H (Single(None, nil, t)) (plus 1 C) ((l, v, C)::H)
(Single(None, nil, fill E (loc l)))
|p_commitStep : forall C H S L v t E H' e0,
validate S L H C (commit H') -> decompose t E (inatomic v) ->
p_step C H (Single(Some(S, e0), L, t)) (plus 1 C) H' (Single(None, nil, fill E v))
|p_atomicStep : forall C H E e t,
decompose t E (atomic e) ->
p_step C H (Single(None, nil, t)) (plus 1 C) H
(Single(Some(C, fill E(inatomic e)),[],fill E (inatomic e)))
|p_betaStep : forall C H E e t v,
decompose t E (app (lambda e) v) ->
p_step C H (Single(None, nil, t)) C H
(Single(None, nil, fill E (open e 0 v))).
(*reflexive transitive closure of partial multistep*)
Inductive p_multistep : nat -> heap -> pool -> nat -> heap -> pool -> Prop :=
|p_multi_refl : forall C H T, p_multistep C H T C H T
|p_multi_step : forall C H T C' H' T' C'' H'' T'',
p_step C H T C' H' T' -> p_multistep C' H' T' C'' H'' T'' ->
p_multistep C H T C'' H'' T''.
(*full abort STM semantics (single step)*)
Inductive f_step : nat -> heap -> pool -> nat -> heap -> pool -> Prop :=
|f_transStep : forall C H t t', trans_step H t t' ->
f_step C H (Single t) C H (Single t')
|f_parLStep : forall C H T1 T2 C' H' T1',
f_step C H T1 C' H' T1' -> f_step C H (Par T1 T2) C' H' (Par T1' T2)
|f_parRStep : forall C H T1 T2 C' H' T2',
f_step C H T2 C' H' T2' -> f_step C H (Par T1 T2) C' H' (Par T1 T2')
|f_forkStep : forall C H E e t,
decompose t E (fork e) ->
f_step C H (Single(None, nil, t)) C H
(Par (Single(None, nil, fill E unit)) (Single(None, nil, e)))
|f_eagerAbort : forall L S H L' C e0 e' t v E l S',
lookup H l = Some(v, S') -> S < S' ->
validate S (readItem l E v::L) H C (abort e' L') ->
decompose t E (get (loc l)) -> logLookup L l = None ->
f_step C H (Single (Some(S, e0), L, t))
(C+1) H (Single(Some(C, e0), nil, e0))
|f_abortStep : forall L S H L' C e e0 e' S',
validate S L H S' (abort e' L') ->
f_step C H (Single(Some(S, e0), L, e)) (plus 1 C) H
(Single(Some(C, e0), nil, e0))
|f_allocStep : forall C H v E t l,
lookup H l = None -> decompose t E (alloc v) ->
f_step C H (Single(None, nil, t)) (plus 1 C) ((l, v, C)::H)
(Single(None, nil, fill E (loc l)))
|f_commitStep : forall C H S L v t E H' e0,
validate S L H C (commit H') -> decompose t E (inatomic v) ->
f_step C H (Single(Some(S, e0), L, t)) (plus 1 C) H' (Single(None, nil, fill E v))
|f_atomicStep : forall C H E e t,
decompose t E (atomic e) ->
f_step C H (Single(None, nil, t)) (plus 1 C) H
(Single(Some(C, fill E (inatomic e)), nil, fill E (inatomic e)))
|f_betaStep : forall C H E e t v,
decompose t E (app (lambda e) v) ->
f_step C H (Single(None, nil, t)) C H
(Single(None, nil, fill E (open e 0 v))).
(*reflexivie transitive closure of full multistep*)
Inductive f_multistep : nat -> heap -> pool -> nat -> heap -> pool -> Prop :=
|f_multi_refl : forall C H T, f_multistep C H T C H T
|f_multi_step : forall C H T C' H' T' C'' H'' T'',
f_step C H T C' H' T' -> f_multistep C' H' T' C'' H'' T'' ->
f_multistep C H T C'' H'' T''.
(*reflexivit transitive closure of trans_step*)
Inductive trans_multistep H : thread -> thread -> Prop :=
|trans_refl : forall t, trans_multistep H t t
|trans_multi_step : forall t t' t'',
trans_step H t t' -> trans_multistep H t' t'' ->
trans_multistep H t t''.
(*indicates that L1 is a postfix of L2*)
Definition postfix {A:Type} (L1 L2 : list A) := exists diff, L2 = diff ++ L1.
(*all threads can rewind to their initial term of a transaction and have
**a stamp number less than the global clock. The stamp number
**constraint probably doesn't belong here, but its handy to have*)
Inductive poolRewind C H : pool -> Prop :=
|rewindSingleNoTX : forall e, poolRewind C H (Single(None,nil,e))
|rewindSingleInTX : forall S e0 L e,
rewind H (Some(S,e0),nil,e0) (Some(S,e0),L,e) ->
S < C -> poolRewind C H (Single(Some(S,e0),L,e))
|rewindPar : forall T1 T2, poolRewind C H T1 -> poolRewind C H T2 -> poolRewind C H (Par T1 T2).
(*inject every step in a multistep derivation into a Par*)
Theorem f_multi_L : forall C H T1 T2 T1' C' H',
f_multistep C H T1 C' H' T1' ->
f_multistep C H (Par T1 T2) C' H' (Par T1' T2).
Proof.
intros C H T1 T2 T1' C' H' HYP. induction HYP.
{constructor. }
{econstructor. eapply f_parLStep. eassumption. eassumption. }
Qed.
(*inject every step in a multistep derivation into a Par*)
Theorem f_multi_R : forall C H T1 T2 T2' C' H',
f_multistep C H T2 C' H' T2' ->
f_multistep C H (Par T1 T2) C' H' (Par T1 T2').
Proof.
intros C H T1 T2 T2' C' H' HYP. induction HYP.
{constructor. }
{econstructor. eapply f_parRStep. eassumption. eassumption. }
Qed.
(*validation is idempotent*)
Theorem validateValidate : forall S L H S' L' e,
validate S L H S' (abort e L') ->
exists H'', validate S L' H S' (commit H'').
Proof.
intros S L H S' L' e HYP. dependent induction HYP; eauto.
Qed.
(*the log returned in an abort is a postfix of the initial log*)
Theorem abortLogPostfix : forall S L H S' L' e,
validate S L H S' (abort e L') ->
postfix L' L.
Proof.
intros S L H S' L' e HYP. remember (abort e L'). induction HYP; try solveByInv.
{apply IHHYP in Heqv. unfold postfix in *. invertHyp. exists (x::x0). auto. }
{inv Heqv. unfold postfix. exists [readItem l E v']. auto. }
Qed.
(*filling an evaluation context with the result of a decomposition
**yields the initial term that was decomposed *)
Theorem decomposeEq : forall E t e, decompose t E e -> t = fill E e.
Proof.
induction E; intros; try solve[inv H; simpl;erewrite <- IHE; eauto].
{inv H; auto. }
Qed.
Theorem lengthsEq : forall (A:Type) (x y : list A), x = y -> length x = length y.
Proof.
induction x; intros.
{destruct y. auto. inv H. }
{destruct y. inv H. inv H. simpl. apply f_equal. auto. }
Qed.
(*replay relation is transitive*)
Theorem trans_replay: forall H t t' t'',
replay H t' t'' -> replay H t t' ->
replay H t t''.
Proof.
intros H t t' t'' HYP1 HYP2. induction HYP2. auto. econstructor. eauto. auto.
Qed.
(*rewind relation is transitive*)
Theorem rewindTrans : forall H t t' t'',
rewind H t t' -> rewind H t' t'' ->
rewind H t t''.
Proof.
intros H t t' t'' HYP1 HYP2. generalize dependent t. induction HYP2; intros; auto.
econstructor. eapply IHHYP2. assumption. assumption.
Qed.
(*replay and rewind are equivalent*)
Theorem rewindIFFReplay : forall H t t',
rewind H t t' <-> replay H t t'.
Proof.
intros H t t'. split; intros HYP.
{induction HYP. constructor. eapply trans_replay; eauto.
econstructor. eauto. constructor. }
{induction HYP. constructor. eapply rewindTrans; eauto.
econstructor; eauto. constructor. }
Qed.
(*partial multistep relation is transitive*)
Theorem p_multi_trans : forall C H T C' H' T' C'' H'' T'',
p_multistep C H T C' H' T' ->
p_multistep C' H' T' C'' H'' T'' ->
p_multistep C H T C'' H'' T''.
Proof.
intros C H T C' H' T' C'' H'' T'' HYP1 HYP2.
induction HYP1; auto. econstructor; eauto.
Qed.
(*full multistep relation is transitive*)
Theorem f_multi_trans : forall C H T C' H' T' C'' H'' T'',
f_multistep C H T C' H' T' ->
f_multistep C' H' T' C'' H'' T'' ->
f_multistep C H T C'' H'' T''.
Proof.
intros C H T C' H' T' C'' H'' T'' HYP1 HYP2.
induction HYP1; auto. econstructor; eauto.
Qed.
(*inject left multistep derivation into a Par*)
Theorem p_multi_L : forall C H T1 T2 T1' C' H',
p_multistep C H T1 C' H' T1' ->
p_multistep C H (Par T1 T2) C' H' (Par T1' T2).
Proof.
intros. induction H0.
{constructor. }
{econstructor. eapply p_parLStep. eassumption. eassumption. }
Qed.
(*inject right multistep derivation into a Par*)
Theorem p_multi_R : forall C H T1 T2 T2' C' H',
p_multistep C H T2 C' H' T2' ->
p_multistep C H (Par T1 T2) C' H' (Par T1 T2').
Proof.
intros. induction H0.
{constructor. }
{econstructor. eapply p_parRStep. eassumption. eassumption. }
Qed.