forked from achlipala/frap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SessionTypes.v
1555 lines (1327 loc) · 46.9 KB
/
SessionTypes.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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(** Formal Reasoning About Programs <http://adam.chlipala.net/frap/>
* Chapter 22: Session Types
* Author: Adam Chlipala
* License: https://creativecommons.org/licenses/by-nc-nd/4.0/ *)
Require Import Frap FunctionalExtensionality MessagesAndRefinement.
Set Implicit Arguments.
Set Asymmetric Patterns.
(* One natural view of process algebra is as a way of orchestrating multiple
* agents that communicate with each other through prearranged protocols.
* Session types are a way of doing static analysis, in the style of type
* checking as we saw in earlier chapters, to guarantee that agents play well
* together. Specifically, in this chapter, we'll confine our attention to
* avoiding stuckness: a set of agents should either reach a state where
* everyone is done or should continue stepping forever. A counterexample would
* be a configuration where each of two agents is blocked waiting for input from
* the other -- a classic deadlock. *)
(** * Basic Two-Party Session Types *)
(* We'll consider some gradations of fanciness in our session type systems.
* Even the final version will have some notable expressiveness weaknesses, but
* we'll still be able to handle a variety of nontrivial protocols. Each
* variant will be confined to its own module, allowing us to reuse names. *)
Module BasicTwoParty.
(** ** Defining the type system *)
Inductive type :=
| TSend (ch : channel) (A : Type) (t : type)
(* This type applies to a process that begins by sending a value of type [A]
* over channel [ch], then continuing according to type [t]. *)
| TRecv (ch : channel) (A : Type) (t : type)
(* This type is the dual of the last one: the process begins by receiving a
* value of type [A] from channel [ch]. *)
| TDone.
(* This type describes processes that are done. Notice that we make our lives
* easier by not supporting any of the other constructs (parallel composition,
* duplication, ...) from our process algebra! *)
(* The typing rules mostly just formalize the comments from above. *)
Inductive hasty : proc -> type -> Prop :=
| HtSend : forall ch (A : Type) (v : A) k t,
hasty k t
-> hasty (Send ch v k) (TSend ch A t)
| HtRecv : forall ch (A : Type) (k : A -> _) t,
(forall v, hasty (k v) t)
-> hasty (Recv ch k) (TRecv ch A t)
| HtDone :
hasty Done TDone.
(* Notice, though, that the premise of [HtRecv] does quantification over all
* possible values that might come down the channel [ch]. The follow-up type [t]
* must be independent of those values, though. *)
(* Some notations will let us write nicer-looking types. *)
Delimit Scope st_scope with st.
Bind Scope st_scope with type.
Notation "!!! ch ( A ) ; k" := (TSend ch A k%st) (right associativity, at level 45, ch at level 0) : st_scope.
Notation "??? ch ( A ) ; k" := (TRecv ch A k%st) (right associativity, at level 45, ch at level 0) : st_scope.
(* This tactic happens to be good for automating typing derivations. *)
Ltac hasty := simplify; repeat ((constructor; simplify)
|| match goal with
| [ |- hasty _ (match ?E with _ => _ end) ] => cases E
| [ |- hasty (match ?E with _ => _ end) _ ] => cases E
end).
(** * Examples of typed processes *)
(* Recall our first example from last chapter. *)
Definition addN (k : nat) (input output : channel) : proc :=
??input(n : nat);
!!output(n + k);
Done.
(* Let's prove it against a type, which looks a lot like the program itself. *)
Definition addN_type input output :=
(???input(nat); !!!output(nat); TDone)%st.
Theorem addN_typed : forall k input output,
hasty (addN k input output) (addN_type input output).
Proof.
hasty.
Qed.
(** * Complementing types *)
(* We will focus on pairs of interacting processes, where one process follows a
* session type, and the other process follows the *complement* of that type,
* guaranteeing that they agree on the protocol. *)
(* Complementation just flips all sends and receives. *)
Fixpoint complement (t : type) : type :=
match t with
| TSend ch A t1 => TRecv ch A (complement t1)
| TRecv ch A t1 => TSend ch A (complement t1)
| TDone => TDone
end.
(* Here's a simple client for our adder example. *)
Definition add2_client (input output : channel) : proc :=
!!input(42);
??output(_ : nat);
Done.
(* It checks out against the complement of the type from before. *)
Theorem add2_client_typed : forall input output,
hasty (add2_client input output) (complement (addN_type input output)).
Proof.
hasty.
Qed.
(** * Main theorem: deadlock freedom for complementary processes *)
Definition trsys_of pr := {|
Initial := {pr};
Step := lstepSilent
|}.
(* Note: here we force silent steps, so that all channel communication is
* internal. *)
Global Hint Constructors hasty : core.
(* The next two lemmas state some inversions that connect stepping and
* typing. *)
Lemma input_typed : forall pr ch A v pr',
lstep pr (Action (Input {| Channel := ch; TypeOf := A; Value := v |})) pr'
-> forall t, hasty pr t
-> exists k, pr = Recv ch k /\ pr' = k v.
Proof.
invert 1; invert 1; eauto.
Qed.
Lemma output_typed : forall pr ch A v pr',
lstep pr (Action (Output {| Channel := ch; TypeOf := A; Value := v |})) pr'
-> forall t, hasty pr t
-> exists k, pr = Send ch v k /\ pr' = k.
Proof.
invert 1; invert 1; eauto.
Qed.
(* A key strengthened invariant: when two processes begin life as complementary,
* they remain complementary forever after, though the shared type may
* change. *)
Lemma complementarity_forever : forall pr1 pr2 t,
hasty pr1 t
-> hasty pr2 (complement t)
-> invariantFor (trsys_of (pr1 || pr2))
(fun pr => exists pr1' pr2' t',
pr = pr1' || pr2'
/\ hasty pr1' t'
/\ hasty pr2' (complement t')).
Proof.
simplify.
apply invariant_induction; simplify.
propositional; subst.
eauto 6.
clear pr1 pr2 t H H0.
first_order; subst.
invert H2.
invert H6; invert H0.
invert H6; invert H1.
eapply input_typed in H4; eauto.
eapply output_typed in H5; eauto.
first_order; subst.
invert H0.
invert H1.
eauto 7.
eapply input_typed in H5; eauto.
eapply output_typed in H4; eauto.
first_order; subst.
invert H0.
invert H1.
eauto 10.
Qed.
(* The main theorem: it's an invariant that the system is done or can take a
* step. *)
Theorem no_deadlock : forall pr1 pr2 t,
hasty pr1 t
-> hasty pr2 (complement t)
-> invariantFor (trsys_of (pr1 || pr2))
(fun pr => pr = (Done || Done)
\/ exists pr', lstep pr Silent pr').
Proof.
simplify.
eapply invariant_weaken.
eapply complementarity_forever; eauto.
clear pr1 pr2 t H H0.
simplify; first_order; subst.
invert H0; invert H1; simplify; eauto.
Qed.
(* Applying the theorem to our earlier example is easy. *)
Example adding_no_deadlock : forall k input output,
input <> output
-> invariantFor (trsys_of (addN k input output
|| add2_client input output))
(fun pr => pr = (Done || Done)
\/ exists pr', lstep pr Silent pr').
Proof.
simplify.
eapply no_deadlock with (t := addN_type input output);
hasty.
Qed.
End BasicTwoParty.
(** * Two-Party Session Types *)
(* That last type system has a serious weakness: it doesn't allow communication
* patterns to vary, based on what was received on channels earlier in
* execution. Let's switch to a simple kind of *dependent* session types, where
* send and receive types bind message values for use in decision-making. *)
Module TwoParty.
(** ** Defining the type system *)
Inductive type :=
| TSend (ch : channel) (A : Type) (t : A -> type)
| TRecv (ch : channel) (A : Type) (t : A -> type)
| TDone.
(* Note the big change: each follow-up type [t] is parameterized on the value
* sent or received. As with our mixed-embedding programs, within these
* functions we may employ the full expressiveness of Gallina. *)
Inductive hasty : proc -> type -> Prop :=
| HtSend : forall ch (A : Type) (v : A) k t,
hasty k (t v)
-> hasty (Send ch v k) (TSend ch t)
| HtRecv : forall ch (A : Type) (k : A -> _) t,
(forall v, hasty (k v) (t v))
-> hasty (Recv ch k) (TRecv ch t)
| HtDone :
hasty Done TDone.
Delimit Scope st_scope with st.
Bind Scope st_scope with type.
Notation "!!! ch ( x : A ) ; k" := (TSend ch (fun x : A => k)%st) (right associativity, at level 45, ch at level 0, x at level 0) : st_scope.
Notation "??? ch ( x : A ) ; k" := (TRecv ch (fun x : A => k)%st) (right associativity, at level 45, ch at level 0, x at level 0) : st_scope.
Ltac hasty := simplify; repeat ((constructor; simplify)
|| match goal with
| [ |- hasty _ (match ?E with _ => _ end) ] => cases E
| [ |- hasty (match ?E with _ => _ end) _ ] => cases E
end).
(** * Complementing types *)
Fixpoint complement (t : type) : type :=
match t with
| TSend ch _ t1 => TRecv ch (fun v => complement (t1 v))
| TRecv ch _ t1 => TSend ch (fun v => complement (t1 v))
| TDone => TDone
end.
(** ** Example *)
(* Let's demonstrate the power of the strengthened type system. We'll model an
* online store communicating with a customer. *)
Section online_store.
Variables request_product in_stock_or_not send_payment_info payment_success add_review : channel.
Definition customer (product payment_info : string) :=
!!request_product(product);
??in_stock_or_not(worked : bool);
if worked then
!!send_payment_info(payment_info);
??payment_success(worked_again : bool);
if worked_again then
!!add_review((product, "awesome"));
Done
else
Done
else
Done.
Definition customer_type :=
(!!!request_product(_ : string);
???in_stock_or_not(worked : bool);
if worked then
!!!send_payment_info(_ : string);
???payment_success(worked_again : bool);
if worked_again then
!!!add_review(_ : (string * string)%type);
TDone
else
TDone
else
TDone)%st.
(* Yes, that type again looks a lot like the program! However, we abstract
* away the details of all non-[bool] messages. *)
Theorem customer_hasty : forall product payment_info,
hasty (customer product payment_info) customer_type.
Proof.
hasty.
Qed.
Definition merchant (in_stock payment_checker : string -> bool) :=
??request_product(product : string);
if in_stock product then
!!in_stock_or_not(true);
??send_payment_info(payment_info : string);
if payment_checker payment_info then
!!payment_success(true);
??add_review(_ : (string * string)%type);
Done
else
!!payment_success(false);
Done
else
!!in_stock_or_not(false);
Done.
Theorem merchant_hasty : forall in_stock payment_checker,
hasty (merchant in_stock payment_checker) (complement customer_type).
Proof.
hasty.
Qed.
End online_store.
(** * Main theorem: deadlock freedom for complementary processes *)
(* The proof is essentially identical to before, which is kind of neat, given
* the fundamental new capability that we added. *)
Definition trsys_of pr := {|
Initial := {pr};
Step := lstepSilent
|}.
Global Hint Constructors hasty : core.
Lemma input_typed : forall pr ch A v pr',
lstep pr (Action (Input {| Channel := ch; TypeOf := A; Value := v |})) pr'
-> forall t, hasty pr t
-> exists k, pr = Recv ch k /\ pr' = k v.
Proof.
invert 1; invert 1; eauto.
Qed.
Lemma output_typed : forall pr ch A v pr',
lstep pr (Action (Output {| Channel := ch; TypeOf := A; Value := v |})) pr'
-> forall t, hasty pr t
-> exists k, pr = Send ch v k /\ pr' = k.
Proof.
invert 1; invert 1; eauto.
Qed.
Lemma complementarity_forever : forall pr1 pr2 t,
hasty pr1 t
-> hasty pr2 (complement t)
-> invariantFor (trsys_of (pr1 || pr2))
(fun pr => exists pr1' pr2' t',
pr = pr1' || pr2'
/\ hasty pr1' t'
/\ hasty pr2' (complement t')).
Proof.
simplify.
apply invariant_induction; simplify.
propositional; subst.
eauto 6.
clear pr1 pr2 t H H0.
first_order; subst.
invert H2.
invert H6; invert H0.
invert H6; invert H1.
eapply input_typed in H4; eauto.
eapply output_typed in H5; eauto.
first_order; subst.
invert H0.
invert H1.
eauto 7.
eapply input_typed in H5; eauto.
eapply output_typed in H4; eauto.
first_order; subst.
invert H0.
invert H1.
eauto 10.
Qed.
Theorem no_deadlock : forall pr1 pr2 t,
hasty pr1 t
-> hasty pr2 (complement t)
-> invariantFor (trsys_of (pr1 || pr2))
(fun pr => pr = (Done || Done)
\/ exists pr', lstep pr Silent pr').
Proof.
simplify.
eapply invariant_weaken.
eapply complementarity_forever; eauto.
clear pr1 pr2 t H H0.
simplify; first_order; subst.
invert H0; invert H1; simplify; eauto.
Qed.
Example online_store_no_deadlock : forall request_product in_stock_or_not
send_payment_info payment_success add_review
product payment_info in_stock payment_checker,
invariantFor (trsys_of (customer request_product in_stock_or_not
send_payment_info payment_success add_review
product payment_info
|| merchant request_product in_stock_or_not
send_payment_info payment_success add_review
in_stock payment_checker))
(fun pr => pr = (Done || Done)
\/ exists pr', lstep pr Silent pr').
Proof.
simplify.
eapply no_deadlock with (t := customer_type request_product in_stock_or_not
send_payment_info payment_success add_review);
hasty.
Qed.
End TwoParty.
(** * Multiparty Session Types *)
(* Let's generalize to any number of agents participating in a protocol. We
* won't support all reasonable protocols, and it's an edifying exercise for the
* reader to think up examples that this type system rejects. *)
Module Multiparty.
(** ** Defining the type system *)
Inductive type :=
| Communicate (ch : channel) (A : Type) (t : A -> type)
| TDone.
(* Things are quite different now. We define one protocol with a series of
* communications, not specifying read vs. write polarity. Every agent will be
* checked against this type, referring to a mapping that tells us which agent
* controls the receive end and which the send end of each channel. Exactly one
* agent will have each role. *)
Delimit Scope st_scope with st.
Bind Scope st_scope with type.
Notation "!!! ch ( x : A ) ; k" := (Communicate ch (fun x : A => k)%st) (right associativity, at level 45, ch at level 0, x at level 0) : st_scope.
Section parties.
Variable party : Type.
(* We will formalize typing with respect to some (usually finite) set of
* parties/agents. *)
Record parties := {
Sender : party;
Receiver : party
}.
Variable channels : channel -> parties.
(* As promised, every channel is assigned a unique sender and receiver. *)
Inductive hasty (p : party) : bool -> proc -> type -> Prop :=
(* The first two rules look up the next channel and confirm that the current
* process is in the right role to perform a send or receive. *)
| HtSend : forall ch rr (A : Type) (v : A) k t,
channels ch = {| Sender := p; Receiver := rr |}
-> rr <> p
-> hasty p false k (t v)
-> hasty p false (Send ch v k) (Communicate ch t)
| HtRecv : forall mayNotSend ch sr (A : Type) (k : A -> _) t (witness : A),
channels ch = {| Sender := sr; Receiver := p |}
-> sr <> p
-> (forall v, hasty p false (k v) (t v))
-> hasty p mayNotSend (Recv ch k) (Communicate ch t)
(* Not all parties participate in all communications. Uninvolved parties may
* (or, rather, must!) skip protocol steps. *)
| HtSkip : forall mayNotSend ch sr rr (A : Type) pr (t : A -> _) (witness : A),
channels ch = {| Sender := sr; Receiver := rr |}
-> sr <> p
-> rr <> p
-> (forall v, hasty p true pr (t v))
-> hasty p mayNotSend pr (Communicate ch t)
| HtDone : forall mayNotSend,
hasty p mayNotSend Done TDone.
(* What was that peculiar [bool] parameter? If [true], it prohibits the
* process from running a [Send] as its next action. The idea is that, when a
* process sits out one step of a protocol, its next action (if any) had
* better be a receive, so that it gets some signal to wake up and resume
* participating. Otherwise, the deadlock-freedom analysis is more
* complicated. *)
End parties.
(** * Main theorem: deadlock freedom for complementary processes *)
Definition trsys_of pr := {|
Initial := {pr};
Step := lstepSilent
|}.
Global Hint Constructors hasty : core.
(* We prove that the type system rules out fancier constructs. *)
Lemma hasty_not_Block : forall party (channels: _ -> parties party) p mns ch pr t,
hasty channels p mns (BlockChannel ch pr) t
-> False.
Proof.
induct 1; auto.
Unshelve.
assumption.
Qed.
Lemma hasty_not_Dup : forall party (channels: _ -> parties party) p mns pr t,
hasty channels p mns (Dup pr) t
-> False.
Proof.
induct 1; auto.
Unshelve.
assumption.
Qed.
Lemma hasty_not_Par : forall party (channels: _ -> parties party) p mns pr1 pr2 t,
hasty channels p mns (pr1 || pr2) t
-> False.
Proof.
induct 1; auto.
Unshelve.
assumption.
Qed.
Global Hint Immediate hasty_not_Block hasty_not_Dup hasty_not_Par : core.
(* Next, we characterize how channels must be mapped, given typing of a
* process. *)
Lemma input_typed' : forall party (channels : _ -> parties party) p mns ch (A : Type) (k : A -> _) t,
hasty channels p mns (Recv ch k) t
-> exists sr (witness : A), channels ch = {| Sender := sr; Receiver := p |}
/\ sr <> p.
Proof.
induct 1; eauto.
Unshelve.
assumption.
Qed.
Lemma input_typed : forall party (channels: _ -> parties party) pr ch A v pr',
lstep pr (Action (Input {| Channel := ch; TypeOf := A; Value := v |})) pr'
-> forall p mns t, hasty channels p mns pr t
-> exists sr k, pr = Recv ch k /\ pr' = k v
/\ channels ch = {| Sender := sr; Receiver := p |}
/\ sr <> p.
Proof.
induct 1; simplify; try solve [ exfalso; eauto ].
eapply input_typed' in H.
first_order.
eauto 6.
Qed.
Lemma output_typed' : forall party (channels : _ -> parties party) p mns ch (A : Type) (v : A) k t,
hasty channels p mns (Send ch v k) t
-> exists rr, channels ch = {| Sender := p; Receiver := rr |}
/\ rr <> p.
Proof.
induct 1; eauto.
Unshelve.
assumption.
Qed.
Lemma output_typed : forall party (channels: _ -> parties party) pr ch A v pr',
lstep pr (Action (Output {| Channel := ch; TypeOf := A; Value := v |})) pr'
-> forall p mns t, hasty channels p mns pr t
-> exists k, pr = Send ch v k /\ pr' = k.
Proof.
induct 1; simplify; try solve [ exfalso; eauto ].
eapply output_typed' in H.
first_order.
eauto.
Qed.
(* Here is a crucial additional typing judgment, applying to lists of parties.
* The parties' code is lined up with lopsided trees of parallel composition. *)
Inductive typed_multistate party (channels : channel -> parties party) (t : type)
: list party -> proc -> Prop :=
| TmsNil : typed_multistate channels t [] Done
| TmsCons : forall p ps pr1 pr2,
hasty channels p false pr1 t
-> typed_multistate channels t ps pr2
-> typed_multistate channels t (p :: ps) (pr1 || pr2).
Global Hint Constructors typed_multistate : core.
(* This fancier typing judgment gets a fancier tactic for type-checking. *)
Ltac side :=
match goal with
| [ |- ?E = {| Sender := _; Receiver := _ |} ] =>
let E' := eval hnf in E in change E with E';
repeat match goal with
| [ |- context[if ?E then _ else _] ] => cases E; try (exfalso; equality)
end;
try (exfalso; equality);
repeat match goal with
| [ H : NoDup _ |- _ ] => invert H
end; simplify; try (exfalso; equality); equality
| [ |- _ <> _ ] => equality
end.
Ltac hasty := simplify; repeat match goal with
| [ |- typed_multistate _ _ _ _ ] => econstructor; simplify
| [ |- hasty _ _ _ _ _ ] =>
apply HtDone
|| (eapply HtSend; [ side | side | ])
|| (eapply HtRecv; [ constructor | side | side | simplify ])
|| (eapply HtSkip; [ constructor | side | side | side | simplify ])
| [ |- hasty _ _ _ _ (match ?E with _ => _ end) ] => cases E
| [ |- hasty _ _ _ (match ?E with _ => _ end) _ ] => cases E
end.
(* Now follow quite a few fiddly lemmas. Commentary resumes at a crucial
* lemma. *)
Lemma no_silent_steps : forall party (channels : _ -> parties party) p mns pr t,
hasty channels p mns pr t
-> forall pr', lstep pr Silent pr'
-> False.
Proof.
induct 1; invert 1; try solve [ exfalso; eauto ].
Unshelve.
assumption.
assumption.
assumption.
assumption.
assumption.
assumption.
Qed.
Global Hint Immediate no_silent_steps : core.
Lemma complementarity_forever_done : forall party (channels : _ -> parties party) pr pr',
lstep pr Silent pr'
-> forall all_parties, typed_multistate channels TDone all_parties pr
-> False.
Proof.
induct 1; invert 1; eauto.
invert H5.
invert H.
invert H5.
invert H.
Qed.
Lemma mayNotSend_really : forall party (channels : _ -> parties party) p pr t,
hasty channels p true pr t
-> forall m pr', lstep pr (Action (Output m)) pr'
-> False.
Proof.
induct 1; eauto; invert 1.
Unshelve.
assumption.
Qed.
Global Hint Immediate mayNotSend_really : core.
Lemma may_not_output : forall (party : Type) pr pr' ch (A : Type) (v : A),
lstep pr (Action (Output {| Channel := ch; Value := v |})) pr'
-> forall (channels : _ -> parties party) p t,
hasty channels p true pr t
-> False.
Proof.
induct 1; invert 1; simplify; try solve [ exfalso; eauto ].
Unshelve.
assumption.
assumption.
assumption.
assumption.
Qed.
Global Hint Immediate may_not_output : core.
Lemma output_is_legit : forall (party : Type) pr pr' ch (A : Type) (v : A),
lstep pr (Action (Output {| Channel := ch; Value := v |})) pr'
-> forall (channels : _ -> parties party) all_parties ch' (A' : Type) (k : A' -> _),
typed_multistate channels (Communicate ch' k) all_parties pr
-> In (Sender (channels ch')) all_parties.
Proof.
induct 1; invert 1; simplify; try solve [ exfalso; eauto ].
invert H4.
rewrite H3 in *; simplify; eauto.
invert H.
exfalso; eauto.
invert H4.
rewrite H3 in *; simplify; eauto.
eauto.
eauto.
Unshelve.
assumption.
Qed.
Lemma output_is_first : forall (party : Type) pr pr' ch (A : Type) (v : A),
lstep pr (Action (Output {| Channel := ch; Value := v |})) pr'
-> forall (channels : _ -> parties party) all_parties ch' (A' : Type) (k : A' -> _),
typed_multistate channels (Communicate ch' k) all_parties pr
-> ch' = ch.
Proof.
induct 1; invert 1; simplify; try solve [ exfalso; eauto ].
invert H4.
invert H; auto.
invert H.
exfalso; eauto.
eauto.
Unshelve.
assumption.
Qed.
Lemma input_is_legit' : forall (party : Type) pr ch (A : Type) (v : A)
(channels : _ -> parties party) p mns t,
hasty channels p mns pr t
-> forall pr', lstep pr (Action (Input {| Channel := ch; Value := v |})) pr'
-> p = Receiver (channels ch).
Proof.
induct 1; eauto; invert 1.
rewrite H; auto.
Qed.
Lemma input_is_legit : forall (party : Type) pr pr' ch (A : Type) (v : A),
lstep pr (Action (Input {| Channel := ch; Value := v |})) pr'
-> forall (channels : _ -> parties party) all_parties t,
typed_multistate channels t all_parties pr
-> In (Receiver (channels ch)) all_parties.
Proof.
induct 1; invert 1; simplify; try solve [ exfalso; eauto ].
invert H4.
invert H.
invert H.
rewrite H0 in *; simplify; eauto.
eapply input_is_legit' in H; eauto.
invert H.
eauto.
Unshelve.
assumption.
Qed.
Lemma comm_stuck : forall (party : Type) pr pr',
lstep pr Silent pr'
-> forall (channels : _ -> parties party) all_parties ch (A : Type) (k : A -> _),
typed_multistate channels (Communicate ch k) all_parties pr
-> (In (Sender (channels ch)) all_parties
-> In (Receiver (channels ch)) all_parties
-> False)
-> False.
Proof.
induct 1; invert 1; simplify; try solve [ exfalso; eauto ].
invert H5.
invert H.
invert H.
eapply output_is_legit in H0; eauto.
rewrite H9 in *; simplify; eauto.
rewrite H7 in *; simplify.
eapply output_is_first in H0; eauto.
subst.
eapply input_is_legit' in H; eauto.
subst.
rewrite H7 in *.
simplify.
eauto.
invert H5.
invert H.
rewrite H7 in *; simplify.
eapply input_is_legit in H0; eauto.
rewrite H7 in *; simplify.
eauto.
invert H.
eauto.
Unshelve.
assumption.
assumption.
Qed.
Lemma hasty_relax : forall party (channels : _ -> parties party) p mns pr t,
hasty channels p mns pr t
-> hasty channels p false pr t.
Proof.
induct 1; eauto.
Qed.
Global Hint Immediate hasty_relax : core.
Lemma complementarity_preserve_unused : forall party (channels : _ -> parties party)
pr ch (A : Type) (t : A -> _) all_parties,
typed_multistate channels (Communicate ch t) all_parties pr
-> ~In (Sender (channels ch)) all_parties
-> ~In (Receiver (channels ch)) all_parties
-> forall v, typed_multistate channels (t v) all_parties pr.
Proof.
induct 1; simplify; eauto.
invert H.
rewrite H6 in *; simplify.
equality.
rewrite H8 in *; simplify.
propositional.
rewrite H6 in *; simplify.
propositional.
eauto.
Qed.
Lemma hasty_output : forall pr party (channels : _ -> parties party) p mns t,
hasty channels p mns pr t
-> forall ch (A : Type) (v : A) pr', lstep pr (Action (Output {| Channel := ch; Value := v |})) pr'
-> Sender (channels ch) = p.
Proof.
induct 1; invert 1.
rewrite H; auto.
eauto.
exfalso; eauto.
exfalso; eauto.
exfalso; eauto.
Unshelve.
assumption.
assumption.
assumption.
Qed.
Lemma complementarity_find_sender : forall party (channels : _ -> parties party) pr ch (A : Type) (v : A) pr',
lstep pr (Action (Output {| Channel := ch; Value := v |})) pr'
-> forall (t : A -> _) all_parties,
typed_multistate channels (Communicate ch t) all_parties pr
-> NoDup all_parties
-> In (Sender (channels ch)) all_parties
-> ~In (Receiver (channels ch)) all_parties
-> typed_multistate channels (t v) all_parties pr'.
Proof.
induct 1; invert 1; simplify; try solve [ exfalso; eauto ].
invert H0.
generalize dependent H.
invert H4.
invert 1.
econstructor.
eauto.
eapply complementarity_preserve_unused; eauto.
rewrite H6; assumption.
invert 1.
rewrite H6 in *; simplify.
eapply hasty_output in H; eauto.
rewrite H6 in *; simplify.
equality.
invert H0.
invert H4.
rewrite H9 in *; simplify.
eapply output_is_legit in H5; try eassumption.
rewrite H9 in *; simplify.
propositional.
rewrite H11 in *; simplify.
propositional.
rewrite H9 in *; simplify.
eapply IHlstep in H5; try (eassumption || reflexivity).
2: rewrite H9; simplify; equality.
2: rewrite H9; simplify; equality.
eauto.
Unshelve.
assumption.
Qed.
Lemma complementarity_find_receiver : forall party (channels : _ -> parties party) pr ch (A : Type) (v : A) pr',
lstep pr (Action (Input {| Channel := ch; Value := v |})) pr'
-> forall (t : A -> _) all_parties,
typed_multistate channels (Communicate ch t) all_parties pr
-> NoDup all_parties
-> ~In (Sender (channels ch)) all_parties
-> In (Receiver (channels ch)) all_parties
-> typed_multistate channels (t v) all_parties pr'.
Proof.
induct 1; invert 1; simplify; try solve [ exfalso; eauto ].
invert H0.
generalize dependent H.
invert H4.
invert 1.
invert 1.
econstructor.
eauto.
eapply complementarity_preserve_unused; eauto.
rewrite H10; assumption.
rewrite H6 in *; simplify.
eapply input_is_legit' in H; eauto.
rewrite H6 in *; simplify; equality.
invert H0.
invert H4.
rewrite H9 in *; simplify.
eapply input_is_legit in H; try eassumption.
rewrite H9 in *; simplify.
propositional.
rewrite H11 in *; simplify.
propositional.
eapply input_is_legit in H; try eassumption.
rewrite H11 in *; simplify.
propositional.
eapply IHlstep in H5; try (eassumption || reflexivity).
2: rewrite H9 in *; simplify; equality.
2: rewrite H9 in *; simplify; equality.
eauto.
Unshelve.
assumption.
Qed.
Lemma output_is_legit' : forall party (channels : _ -> parties party) p mns pr t,
hasty channels p mns pr t
-> forall ch (A : Type) (v : A) pr', lstep pr (Action (Output {| Channel := ch; Value := v |})) pr'
-> p = Sender (channels ch).
Proof.
induct 1; invert 1; simplify; try solve [ exfalso; eauto ].
rewrite H; auto.
Unshelve.
assumption.
assumption.
assumption.
assumption.
Qed.
Lemma complementarity_forever' : forall party (channels : _ -> parties party) pr pr',
lstep pr Silent pr'
-> forall ch (A : Type) (t : A -> _) all_parties,
typed_multistate channels (Communicate ch t) all_parties pr
-> NoDup all_parties
-> In (Sender (channels ch)) all_parties
-> In (Receiver (channels ch)) all_parties
-> exists v, typed_multistate channels (t v) all_parties pr'.
Proof.
induct 1; invert 1; simplify; try solve [ exfalso; eauto ].
invert H0.
invert H4.
rewrite H9 in *; simplify.
propositional; try equality.
exfalso; eapply comm_stuck; try eassumption.
rewrite H9; simplify; eauto.
exfalso; eapply comm_stuck; try eassumption.
rewrite H11; simplify; eauto.
rewrite H9 in *; simplify.
apply IHlstep in H5; try assumption.
2: rewrite H9; simplify; equality.
2: rewrite H9; simplify; equality.
first_order; eauto.
invert H1.
generalize dependent H.
invert H5.
invert 1.
invert 1.
eexists.
econstructor.
eauto.
eapply complementarity_find_sender; try eassumption.
rewrite H11 in *; simplify; equality.
rewrite H11 in *; simplify; equality.
rewrite H7 in *; simplify.
eapply input_is_legit' in H; eauto.
eapply output_is_first in H6; try eassumption.