-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtoStateSemantics.v
More file actions
472 lines (376 loc) · 17 KB
/
Copy pathProtoStateSemantics.v
File metadata and controls
472 lines (376 loc) · 17 KB
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
Require Import Crypto.
Require Import ProtoRep.
Require Import CpdtTactics.
Require Import Eqdep_dec.
Require Import Program.
Require Import SfLib.
Require Import Coq.Program.Equality.
Require Import LibTactics.
Definition ob (t:type) := list ((message t) * (list (message Key))).
Record State : Type := mkState
{keys : list (message Key);
basics : list (message Basic);
kOb : ob Key;
bOb : ob Basic
}.
Definition emptyState := mkState nil nil nil nil.
Definition Assertion := State -> Prop.
Definition addKey : forall mt (m:message mt) (pf: mt = Key),
(list (message Key)) -> (list (message Key)).
Proof.
intros. subst. exact (m :: X).
Defined.
Definition addMKey t (m:message t) : (list (message Key)) -> (list (message Key)) := fun l => (
match t as t' return (t = t') -> (list (message Key)) with
| Key => fun pf => (addKey t m pf l)
| _ => fun _ => l
end (eq_refl t)).
Definition addBasic : forall mt (m:message mt) (pf: mt = Basic),
(list (message Basic)) -> (list (message Basic)).
Proof.
intros. subst. exact (m :: X).
Defined.
Definition addMBasic t (m:message t) : (list (message Basic)) -> (list (message Basic)) := fun l => (
match t as t' return (t = t') -> (list (message Basic)) with
| Basic => fun pf => (addBasic t m pf l)
| _ => fun _ => l
end (eq_refl t)).
Definition addP (t:type) : forall mt (pf:mt = t), ((message mt) * (list (message Key))) -> (ob t) -> (ob t).
Proof.
intros. subst. exact (X :: X0 ).
Defined.
Definition addMp mt (p:((message mt)*(list (message Key)))) : (ob Basic) -> (ob Basic) := fun l => (
match mt as t' return (mt = t') -> (ob Basic) with
| Basic => fun pf => (addP _ _ pf p l)
| _ => fun _ => l
end (eq_refl mt)).
Definition addMpK mt (p:((message mt)*(list (message Key)))) : (ob Key) -> (ob Key) := fun l => (
match mt as t' return (mt = t') -> (ob Key) with
| Key => fun pf => (addP _ _ pf p l)
| _ => fun _ => l
end (eq_refl mt)).
Fixpoint obligations'{mt:type} (m:message mt) (kl: list (message Key))
(l:ob Basic) : ob Basic :=
match m with
| encrypt mt' m' k => match mt' with
| Basic => let new := (m', (kl ++ [(key k)])) in
addMp mt' new l
| _ => obligations' m' (kl ++ [(key k)]) l
end
| pair _ _ m1 m2 => (obligations' m1 kl l) ++ (obligations' m2 kl l)
| basic n => addMp _ ((basic n), kl) l
| _ => addMp _ (m, nil) l
end.
Definition obligations{mt:type} (m:message mt) : ob Basic :=
obligations' m nil nil.
Fixpoint obligationsK'{mt:type} (m:message mt) (kl: list (message Key))
(l:ob Key) : ob Key :=
match m with
| encrypt mt' m' k => match mt' with
| Key => let new := (m', (kl ++ [(key k)])) in
addMpK _ new l
| _ => obligationsK' m' (kl ++ [(key k)]) l
end
| pair _ _ m1 m2 => (obligationsK' m1 kl l) ++ (obligationsK' m2 kl l)
| key k => addMpK _ ((key k), kl) l
| _ => addMpK _ (m, nil) l
end.
Definition obligationsK{mt:type} (m:message mt) : ob Key :=
obligationsK' m nil nil.
Definition updateState{t:type} (m:message t) (sIn: State) : State :=
match t with
| Basic => mkState sIn.(keys) (addMBasic t m sIn.(basics)) sIn.(kOb) sIn.(bOb)
| Key => mkState (addMKey t m sIn.(keys)) sIn.(basics) sIn.(kOb) sIn.(bOb)
| Encrypt _ => mkState sIn.(keys) sIn.(basics)
((obligationsK m) ++ sIn.(kOb))
((obligations m) ++ sIn.(bOb))
| _ => sIn
end.
Inductive step : forall (s:State) (t r t':protoType),
(protoExp t) -> (protoExp r) -> (protoExp t') -> State -> Prop :=
| ST_Send_Rec : forall x y mt
(m:message mt) (p1':protoExp x)
(f:(message mt) -> protoExp y) (s:State),
step s _ _ _ (SendC m p1') (ReceiveC f) p1' s
| ST_Rec_Send : forall x y mt (m:message mt) (p1':protoExp x)
(f:(message mt) -> protoExp y) s,
step s _ _ _ (ReceiveC f) (SendC m p1') (f m) (updateState m s)
| ST_Choice_true : forall rt rt' st st'
(r:protoExp rt) (r0:protoExp rt')
(s:protoExp st) (s0:protoExp st') stt,
step stt _ _ _ (ChoiceC true r s) (OfferC r0 s0) r stt
| ST_Choice_false : forall rt rt' st st'
(r:protoExp rt) (r0:protoExp rt')
(s:protoExp st) (s0:protoExp st') stt,
step stt _ _ _ (ChoiceC false r s) (OfferC r0 s0) s stt
| ST_Offer_true : forall rt rt' st st'
(r:protoExp rt) (r0:protoExp rt')
(s:protoExp st) (s0:protoExp st') stt,
step stt _ _ _ (OfferC r0 s0) (ChoiceC true r s) r0 stt
| ST_Offer_false : forall rt rt' st st'
(r:protoExp rt) (r0:protoExp rt') (s:protoExp st) (s0:protoExp st') stt,
step stt _ _ _ (OfferC r0 s0) (ChoiceC false r s) s0 stt.
(*Notation "'stepe' st st'" := (step st _ _ _ st') (at level 50).*)
Inductive multi : forall s (t r t':protoType),
(protoExp t) -> (protoExp r) -> (protoExp t') -> State -> Prop :=
| multi_refl : forall (t r :protoType) (x:protoExp t) (y:protoExp r) st,
multi st _ _ _ x y x st
| multi_step : forall (t t' r r2 s:protoType),
forall (x:protoExp t) (x':protoExp t')
(y:protoExp r) (y2:protoExp r2)
(z1:protoExp s) st st' st'' st2 st2',
step st _ _ _ x x' y st' ->
step st2 _ _ _ x' x y2 st2' ->
multi st' _ _ _ y y2 z1 st'' ->
multi st _ _ _ x x' z1 st''.
(*Notation "'multie' st st'" := (multi st _ _ _ st')
(at level 50).*)
Definition normal_form {p1t p2t:protoType}
(p1:protoExp p1t)(p2:protoExp p2t) : Prop :=
forall st st', ~ exists t' (x:protoExp t'), step st _ _ _ p1 p2 x st'.
Theorem nf_ex : normal_form (ReturnC (basic 0)) (ReturnC (basic 1)).
Proof.
unfold normal_form. unfold not. intros. destruct H. destruct H. inversion H.
Qed.
Axiom updateBoth : forall t (m:message t) x6 x7 p1t p2t p3t (p1:protoExp p1t) (p2:protoExp p2t) (p3: protoExp p3t),
multi x6 _ _ _ p1 p2 p3 x7 ->
multi (updateState m x6) _ _ _ p1 p2 p3 (updateState m x7).
Ltac bool_destruct :=
match goal with H: bool |- _ =>
destruct H
end.
Ltac steps_destruct :=
match goal with H: step _ _ _ _ _ _ _ _ /\ _ |- _ =>
destruct H
end.
Ltac step_destruct :=
match goal with H: step _ _ _ _ _ _ _ _ |- _ =>
dep_destruct H; clear H
end.
Parameter A B C : Type.
Parameter P : A -> B -> C -> Prop.
Parameter Q : Prop.
(* This will try to match an hypothesis named h with 'exists u: T, P'
and return the name of 'u' *)
Ltac extract_name h :=
match goal with
| [h : ?A |- _ ] =>
match A with
| @ex ?T ?P => match P with
| fun u => _ => u
end
end
end.
(* 'smart' destruct using the name we just computed *)
Ltac one_destruct h :=
let a := extract_name h in
destruct h as [a h].
Goal (exists (a:A) (b:B) (c:C), P a b c) -> Q.
intros H.
one_destruct H. one_destruct H. one_destruct H.
repeat (one_destruct H).
Abort.
(* the goal is now
1 subgoals
a : A
b : B
c : C
H : P a b c
______________________________________(1/1)
Q
*)
Ltac ih_dep_destruct :=
match goal with
| [ IHp1 : forall (p2t:protoType) (p2: protoExp p2t),
Dual _ _ -> exists _ _ _ _ _ _ _ _, _,
H : protoType,
H2 : message ?t0 -> (protoExp _),
H3 : message ?t0
|- _
] => dep_destruct (IHp1 H (H2 H3))
end.
Hint Constructors multi.
Hint Resolve updateBoth.
Theorem normalization {p1t p2t :protoType} :
forall (p1:protoExp p1t) (p2:protoExp p2t),
(Dual p1 p2) ->
exists p3t p4t (p3:protoExp p3t) (p4:protoExp p4t) st st' st2 st2',
(multi st _ _ _ p1 p2 p3 st') /\ (multi st2 _ _ _ p2 p1 p4 st2')
/\ normal_form p3 p4.
Proof.
intros.
generalize dependent p2. generalize dependent p2t.
induction p1; destruct p2;
try (intros H; inversion H; subst).
ih_dep_destruct. assumption.
Ltac ih_destruct :=
match goal with
| [ H: exists _, _
|- _
] => destruct H
end.
repeat ih_destruct.
Ltac multi_destruct :=
match goal with
| [ H: multi _ _ _ _ _ _ _ _ /\ _
|- _
] => destruct H
end.
repeat multi_destruct.
eexists. eexists. eexists. eexists.
eexists. eexists. eexists. eexists.
split. eapply multi_step. constructor. constructor. eassumption.
split. apply multi_step with (y:=(p m)) (y2:=p1) (st':=(updateState m x6)) (st2:=x4) (st2':=x4). constructor. constructor. apply updateBoth. eassumption. assumption.
intros. inversion H0; subst.
Ltac ih_dep_destruct' :=
match goal with
| [ IHp1 : forall m (p2t:protoType) (p2: protoExp p2t),
Dual _ _ -> exists _ _ _ _ _ _ _ _, _,
H : protoType,
H2 : (protoExp _),
H3: message _
|- _
] => dep_destruct (IHp1 H3 H H2)
end.
ih_dep_destruct'. assumption.
repeat ih_destruct.
repeat multi_destruct.
eexists. eexists. eexists. eexists.
eexists. eexists. eexists. eexists.
split. apply multi_step with (y:=(p m)) (y2:=p2) (st':=(updateState m x4)) (st2:=x4) (st2':=x4). constructor. constructor. apply updateBoth. eassumption.
split. apply multi_step with (y:=(p2)) (y2:=(p m)) (st':=x6) (st2:=x4) (st2':=(updateState m x4)). constructor. constructor. eassumption. assumption.
intros. inversion H0.
intros. inversion H0.
intros. inversion H0.
intros. inversion H0.
Abort.
(*
eapply multi_step; constructor. eassumption.
apply multi_step with (y:=p2) (st':=(updateState m x6)) (st2:=x4) (st2':=x4).
apply multi_step with (y:=p2) (y2:=(p m)) (st':=x4) (st2:=x6) (st2':=(updateState m x6)).
eapply multi_step. constructor. constructor. eassumption.
split. apply multi_step with (y:=(p m)) (st':=(updateState m x6)) (st2:=x4) (st2':=x4). constructor. constructor. apply updateBoth. eassumption. assumption.
constructor.
split. constructor
constructor
exists x2. exists x3.
exists x4. exists x5. exists x6. exists (updateState m x7).
try (ih_dep_destruct; inversion H; assumption).
dep_destruct (IHp1 p'0 (p m)).
inversion H. assumption.
dep_destruct
(p3t p4t : protoType) (p3 : protoExp p3t)
(p4 : protoExp p4t) (st st' st2 st2' : State),
(multie st p1) p2 p3 st' /\
(multie st2 p2) p1 p4 st2' /\ normal_form p3 p4
destruct H0. destruct H0. destruct H0. destruct H0. destruct H0. destruct H0. destruct H0. destruct H0. destruct H4. eexists. eexists. exists x2. exists x3.
exists x4. exists x5. exists x6. exists (updateState m x7).
split. apply multi_step with (y:=p1) (y2:=(p m)) (st':=x4) (st2:=x6) (st2':=(updateState m x6)). constructor. constructor. assumption.
split. apply multi_step with (y:=(p m)) (y2:=p1) (st':=(updateState m x6)) (st2:=x4) (st2':=x4). constructor. constructor.
apply updateBoth. assumption. assumption.
intros. inversion H0. subst.
dep_destruct (H m _ p2).
inversion H0. assumption.
destruct H0. destruct H1. destruct H1. destruct H1. destruct H1. destruct H1. destruct H1. destruct H1. destruct H1. destruct H4.
eexists. eexists. exists x2. exists x3. exists x4. exists (updateState m x5). exists x6. exists x7.
split. apply multi_step with (y:=(p m)) (y2:=p2) (st':=(updateState m x4)) (st2:=x6) (st2':=x6). constructor. constructor.
apply updateBoth. assumption.
split. apply multi_step with (y:=p2) (y2:=(p m)) (st':=x6) (st2:=x4) (st2':=(updateState m x4)). constructor. constructor. assumption. assumption.
intros. inversion H0.
intros. inversion H0.
intros. inversion H0.
intros. inversion H0.
destruct b. dep_destruct (IHp1_1 r0 p2_1). assumption. destruct x. assumption. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H4.
destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H6.
eexists. eexists. eexists. eexists.
exists x11. exists x12. exists x13. exists x14.
split. apply multi_step with (y:=p1_1) (y2:=p2_1) (st':=x11) (st2:=x13) (st2':=x13). constructor. constructor. apply H3.
split. apply multi_step with (y:=p2_1) (y2:=p1_1) (st':=x13) (st2:=x11) (st2':=x11). constructor. constructor. apply H6. assumption.
dep_destruct (IHp1_2 s0 p2_2). assumption. destruct x. assumption.
destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H4.
destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H6.
eexists. eexists. eexists. eexists.
exists x11. exists x12. exists x13. exists x14.
split. apply multi_step with (y:=p1_2) (y2:=p2_2) (st':=x11) (st2:=x13) (st2':=x13). constructor. constructor. apply H3.
split. apply multi_step with (y:=p2_2) (y2:=p1_2) (st':=x13) (st2:=x11) (st2':=x11). constructor. constructor. apply H6. assumption.
destruct b. dep_destruct (IHp1_1 r0 p2_1). assumption. destruct x. assumption. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H4.
destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H6.
eexists. eexists. eexists. eexists.
exists x11. exists x12. exists x13. exists x14.
split. apply multi_step with (y:=p1_1) (y2:=p2_1) (st':=x11) (st2:=x13) (st2':=x13). constructor. constructor. apply H3.
split. apply multi_step with (y:=p2_1) (y2:=p1_1) (st':=x13) (st2:=x11) (st2':=x11). constructor. constructor. apply H6. assumption.
dep_destruct (IHp1_2 s0 p2_2). assumption. destruct x. assumption.
destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H2. destruct H4.
destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H3. destruct H6.
eexists. eexists. eexists. eexists.
exists x11. exists x12. exists x13. exists x14.
split. apply multi_step with (y:=p1_2) (y2:=p2_2) (st':=x11) (st2:=x13) (st2':=x13). constructor. constructor. apply H3.
split. apply multi_step with (y:=p2_2) (y2:=p1_2) (st':=x13) (st2:=x11) (st2':=x11). constructor. constructor. apply H6. assumption.
eexists. eexists. eexists. eexists. exists emptyState. exists emptyState. exists emptyState. exists emptyState.
split. constructor. split. constructor.
unfold normal_form. intros. unfold not. intros. destruct H0. destruct H0. inversion H0.
Qed.
*)
Definition isValue {t:protoType} (p:protoExp t) : Prop :=
match p with
| ReturnC _ => True
| _ => False
end.
Theorem ex_isValue : isValue (ReturnC (basic 1)).
Proof.
simpl. trivial.
Qed.
Theorem progress {t t':protoType} :
forall (p1:protoExp t) (p2:protoExp t') st,
(Dual p1 p2) ->
isValue p1 \/ (exists t''(p3:protoExp t'') st', step st _ _ _ p1 p2 p3 st').
Proof.
intros p1 p2 st dualProof. destruct p1; destruct p2; inversion dualProof;
(* Case: p1 = SendC, p2 = ReceiveC *)
(* Case: p1 = ReceiveC, p2 = SendC *)
(* Case: p1 = ChoiceC, p2 = OfferC *)
(* Case: p1 = OfferC, p2 = ChoiceC *)
try (right;
try (bool_destruct);
subst; repeat (eexists); constructor).
(* Case: p1 = ReturnC, p2 = ReturnC *)
left. simpl. trivial.
Qed.
Theorem preservation {t t' p3t p4t : protoType} :
forall (p1:protoExp t) (p2:protoExp t'),
(Dual p1 p2) ->
forall (p3:protoExp p3t) (p4:protoExp p4t) st st' st2 st2',
step st _ _ _ p1 p2 p3 st' /\
step st _ _ _ p1 p2 p3 st' /\
step st2 _ _ _ p2 p1 p4 st2'
-> (Dual p3 p4).
Proof.
intros p1 p2 dualProof. destruct p1; destruct p2; inversion dualProof;
intros; try (repeat steps_destruct); (repeat step_destruct); assumption.
Qed.
Lemma value_is_nf {t t':protoType} (p1:protoExp t) (p2:protoExp t') :
(isValue p1) /\ (isValue p2) -> normal_form p1 p2.
Proof.
intros.
destruct p1; destruct p2;
(cbv; intros; solve by inversion 3).
Qed.
Lemma nf_is_value {t t':protoType} (p1:protoExp t) (p2:protoExp t') : (Dual p1 p2) ->
normal_form p1 p2 -> (isValue p1) /\ (isValue p2).
Proof.
unfold normal_form.
intros D H.
destruct p1; destruct p2; try (inversion D);
try (try bool_destruct; destruct H with (st:=emptyState) (st':=emptyState);
repeat eexists; subst; constructor).
destruct H with (st:=emptyState) (st':=updateState m emptyState);
eexists; subst; eexists; constructor.
jauto.
Qed.
Corollary nf_same_as_value {t t':protoType} (p1:protoExp t) (p2:protoExp t')
: (Dual p1 p2) -> normal_form p1 p2 <-> (isValue p1) /\ (isValue p2).
Proof.
intros. split.
intros. apply nf_is_value in H0. assumption. assumption.
intros. apply value_is_nf in H0. assumption.
Qed.