-
Notifications
You must be signed in to change notification settings - Fork 1
/
escapes.go
2400 lines (1961 loc) · 57.5 KB
/
escapes.go
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
package escapes
import (
"github.com/deckarep/golang-set"
"errors"
"fmt"
"time"
"math/rand"
)
// Escapes is an implementation of the Global Escape for Multiparty
// Sessions framework defined in Capecchi et al. (2014).
// Unfortunately, the paper is sometimes vague in its definitions and
// makes incomplete references to the POPL 2008 paper "Multiparty
// Asynchronous Session Types" by Honda, Yoshida and Carbon (which
// hereafter we will denominate as Honda08, Honda et al. 2008 and
// similar notations). Whenever there is a (required) definition that
// is not clear from the original Capecchi paper (most likely because
// they claim to follow a definition by Honda08, which is never
// extended to account for global escapes), we provide a definition
// which is original. We denote functions that implement new
// functionality with CONTRIBUTION. To reduce the amount of
// annotations, we sometimes annotate an interface definition with
// whether a method in the interface is a contribution.
// Listing of Comment Identifiers (Useful for Grep-ing)
// CONTRIBUTION SECTION
// References to figures or pages that do not include a clear
// reference must be considered as referring to Capecchi et al. (2014)
// Notes
// Restrictions wrt Capecchi et al.
// 1. We decided to allow only single expressions sent atomically
// through a channel. Why? Because the definition establishes sets
// (not lists!) and as such is hard to formally verify that binding
// works (that binding is order-preserving, for which it would suffice
// to have OrderedSets, but that is never explicit), and also imposes
// restrictions on sending multiple equal expressions atomically. A
// single value sent makes everything simpler. We consider that this
// is a behavior expected by programmers that does not reduce
// expressivity in the language but simplifies implementation
// considerably.
// SECTION Implementation Language Syntax.
type MultilevelQueue chan Message
type Program interface{
eval(exceptions []Throw, queues map[Channel]MultilevelQueue) Program
//step(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool)
unplugC() (CEC, Program)
unplugE() (EEC, Program)
stepC() (Program, bool)
stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool)
subst(x Id, e Exp) Program
typecheck(names SortingNames, vars SortingVariables, gts GlobalTypeEnv) (Typing, error)
}
type Channel struct{
name string // represents a channel identifier.
level int
}
type Participant int // represents an identifier for a participant.
type SessionIdentifier string // represents a in Request/Accept
type Label string
// conditions. Bear in mind that a SessionIdentifier should be
// matched to begin evaluation of a session.
type Request struct{
// \bar{a}[2..n](\~s).P
name SessionIdentifier //a
others []Participant//[2..n]
channels mapset.Set // a set of channels
P Program
}
type Accept struct{
// a[p](\~s).P
name SessionIdentifier//a
participant Participant
channels mapset.Set
P Program
}
type Output struct{
// r!<e> binds a set of expression
channel Channel
expression Exp //Only one, to simplify stuff.
}
type Input struct{
//r?(x).P
channel Channel
x Id
P Program
}
type Select struct{
// r <| l.P
channel Channel
label Label
P Program
}
type Branch struct{
// r |> {l_i: P_i} i \in I
channel Channel
branches map[Label] Program
}
type TryCatch struct{
channels mapset.Set // a set of channels
Try, Catch Program
}
type Throw struct{
// throw(\~s)
channels mapset.Set
}
type Conditional struct{
// if e then P else P
cond Exp
t, f Program
}
type Parallel struct{
P, Q Program
}
type Sequencing struct{
P, Q Program
}
type Inaction struct{}
type Hiding struct{
//(\eta n P)
channel Channel //name of the hidden channel
P Program
}
type ProcessId string
//declarations are used in Recursion calls.
type Declaration struct{
identifiers []Id
channels mapset.Set // set of channels
P Program
}
type Recursion struct{
declarations map[ProcessId] Declaration
P Program
}
type ProcessCall struct{
name ProcessId
expressions []Exp //to be bound to identifiers in a declaration
channels mapset.Set
}
// We avoid declaring named queues directly, our interpreter will
// have a global map of channels to Multilevel Queues, as required
// by implementation details. (Future improvement: There's a note
// in page 10 suggesting to use pairs (level, msg) instead of
// multilevel queues. While this is a notable implementation
// improvement that might reduce overhead, we do not implement it
// here to avoid making the code even more confusing.
// The following is commented out for the reasons in the previous comment.
// type NamedQueue struct{
// name Channel
// queue chan Message
// }
// SECTION Expressions Syntax.
// CONTRIBUTION The paper does not consider the expressions language
// as an interesting part of their work. The expressions section
// denotes local node computation, so it will most likely be the
// interesting part to extend in future work. Without much analysis,
// it seems that there would be not much problem in adding almost all
// the GO language here (likely exception: channels. Using channels
// for communication outside the Session protocol would allow to break
// all the invariants).
// We define a standard call-by-value interpreter for expressions.
type Exp interface{
eval() (Value, error)
subst(x Id, v Exp) Exp
isValue() bool
}
type Value interface{
isValue() bool
isMessage() bool
}
type Id string //Identifiers, to be used in Expressions.
func (i Id) isValue() bool{
return false
}
type And struct{
e1, e2 Exp
}
func (a And) isValue() bool{
return false
}
type Not struct{
e Exp
}
func (n Not) isValue() bool{
return false
}
// Values: As defined in Fig. 3. Only SessionIdentifiers (a) or
// booleans for now.
type Bool bool // You can't define extra methods on bool directly. Boo
// golang. Boo.
func (b Bool) isValue() bool{
return true
}
func (s SessionIdentifier) isValue() bool{
return true
}
// Messages are things that could be on a queue (on a channel). For
// now, they can only be either labels or values (we already
// established that sending sets of expressions was not implemented
// here)
type Message interface{
isMessage() bool
}
// Since golang is evil and does not permit general definition of
// methods on interface types, I need to define the method for all
// elements of the Value interface again. Boo golang. Boo.
func (v Bool) isMessage() bool{
return true
}
func (s SessionIdentifier) isMessage() bool{
return true
}
func (l Label) isMessage() bool{
return true
}
// SECTION Expression's eval. We try to encapsulate the full definition of the
// eval function for Expressions here.
func (s SessionIdentifier) eval() (Value, error){
return s, nil
}
func (s SessionIdentifier) subst(x Id, v Exp) Exp{
return s
}
func (b Bool) eval() (Value, error){
return b, nil
}
func (b Bool) subst(x Id, v Exp) Exp{
return b
}
func (x Id) eval() (Value, error){
// substitution failed. there is an unbound identifier. we could
// simplify this to have an environment, but we also need to carry
// the environment throughout evaluation of Programs so we simplify
// here for the prototype.
return nil, errors.New(fmt.Sprintf("Identifier %v is undefined", x))
}
func (x Id) subst(other Id, v Exp) Exp{
if x == other {
return v
}
return x
}
func (a And) eval() (Value, error){
e1, err := a.e1.eval()
if err != nil {
return nil, err
}
e2, err := a.e2.eval()
if err != nil {
return nil, err
}
b1, ok1 := e1.(Bool)
b2, ok2 := e2.(Bool)
if ok1 && ok2 && bool(b1) && bool(b2) {
return Bool(true), nil
}
return Bool(false), nil
}
func (a And) subst(x Id, v Exp) Exp{
return And{e1:a.e1.subst(x,v), e2:a.e2.subst(x,v)}
}
func (n Not) eval() (Value, error) {
e, err := n.e.eval()
if err != nil {
return nil, err
}
v, ok := e.(Bool)
if !ok{
return nil, errors.New("Trying to not a non boolean expression.")
}
return Bool(!bool(v)), nil
}
func (n Not) subst(x Id, v Exp) Exp{
return Not{e:n.e.subst(x,v)}
}
// SECTION Substitution defined for programs.
// Several rules make use of substitution to encompass for receiving messages.
// Even though it is never defined. (This is standard practice).
// We define it in the standard way.
func (r Request) subst(x Id, v Exp) Program{
return Request{name:r.name, others:r.others, channels:r.channels, P:r.P.subst(x,v)}
}
func (a Accept) subst (x Id, v Exp) Program{
return Accept{name:a.name, participant: a.participant, channels:a.channels, P:a.P.subst(x,v)}
}
func (o Output) subst (x Id, v Exp) Program{
return Output{channel:o.channel, expression:o.expression.subst(x,v)}
}
func (i Input) subst (x Id, v Exp) Program{
return Input{channel:i.channel, x:i.x, P:i.P.subst(x,v)}
}
func (s Select) subst (x Id, v Exp) Program{
return Select{channel:s.channel, label:s.label, P:s.P.subst(x,v)}
}
func (b Branch) subst(x Id, v Exp) Program{
branches := make(map[Label]Program)
for l, p := range b.branches{
branches[l] = p.subst(x,v)
}
return Branch{channel:b.channel, branches:branches}
}
func (tc TryCatch) subst(x Id, v Exp) Program{
return TryCatch{channels:tc.channels,
Try:tc.Try.subst(x,v),
Catch:tc.Try.subst(x,v)}
}
func (t Throw) subst(x Id, v Exp) Program{
return t
}
func (c Conditional) subst (x Id, v Exp) Program{
return Conditional{cond:c.cond.subst(x,v),
t:c.t.subst(x,v),
f:c.f.subst(x,v)}
}
func (p Parallel) subst (x Id, v Exp) Program{
return Parallel{P: p.P.subst(x,v), Q: p.Q.subst(x,v)}
}
func (s Sequencing) subst (x Id, v Exp) Program{
return Sequencing{P: s.P.subst(x,v), Q: s.Q.subst(x,v)}
}
func (i Inaction) subst (x Id, v Exp) Program{
return i
}
func (h Hiding) subst (x Id, v Exp) Program{
return Hiding{channel:h.channel, P:h.P.subst(x,v)}
}
func (d Declaration) subst(x Id, v Exp) Declaration{
return Declaration{
identifiers: d.identifiers,
channels: d.channels,
P: d.P.subst(x,v),
}
}
func (r Recursion) subst (x Id, v Exp) Program{
declarations := make(map[ProcessId] Declaration)
for pid, decl := range r.declarations {
declarations[pid] = decl.subst(x,v)
}
return Recursion{
declarations: declarations,
P : r.P.subst(x,v),
}
}
func (pc ProcessCall) subst ( x Id, v Exp) Program{
expressions := make([]Exp, len(pc.expressions), len(pc.expressions))
for i, e := range pc.expressions{
expressions[i] = e.subst(x,v)
}
return ProcessCall{
name:pc.name,
expressions:expressions,
channels:pc.channels,
}
}
// SECTION Runtime semantics for Global Escape.
// The following section maps the rules in Fig. 5 and Fig. 6 that were
// used to define a reduction semantics (in a standard "Evaluation
// Context" structure) into an evaluation strategy for the system. We
// try to keep track of each of the rules used. A key innovation with
// respect to the formal semantics is as follows: Instead of using
// Queues as a first-class process running in parallel (which
// introduces all sorts of congruence issues and unnecessary stuff),
// we simply keep a global map of channels to Multilevel Queues.
// Though as explained in the paper, and in previous comments, these
// Multilevel Queues could be implemented more efficiently, we have
// opted for increased clarity on the first implementation.
// The runtime semantics is expressed through the eval method in the Program interface.
// However, it is hard to make a direct map from the rules in a
// reduction semantics towards an actual implementation (they are
// useful for other reasons of semantic reasoning). We follow an
// approach to ease understanding of the transformation, combining an
// explicit notion of evaluation contexts (in both grammars defined in
// page 10) with a notion of "unplug" and "plug" (plug is the standard
// function to combine evaluation contexts and terms that fill the
// hole. "unplug" gets that pair back from a term.) A simple notion of
// both definitions is presented on the reduction.go package, which
// provides a mapping from a reduction semantics for the lambda
// calculus towards an evaluation machine. Future work is required to
// ensure efficiency and clarity on this code. We regard our
// implementation as a working prototype.
// SECTION Evaluation Context Definition
// two grammars: C and E (generating CEC ("C" Evaluation Contexts) and
// EEC, respectively)
type CEC interface{
plug(p Program) Program
}
type CHole struct{}
type CRec struct{
D map[ProcessId] Declaration
C CEC
}
type CSeq struct{
C CEC
P Program
}
func (h CHole) plug(p Program) Program{
return p
}
func (d CRec) plug(p Program) Program{
return Recursion{declarations: d.D, P:d.C.plug(p)}
}
func (s CSeq) plug (p Program) Program{
return Sequencing{P:s.C.plug(p), Q:s.P}
}
type EEC interface{
plug(p Program) Program
}
type EHole struct{}
type ERec struct{
D map[ProcessId] Declaration
E EEC
}
type ESeq struct{
E EEC
P Program
}
type EParallel struct{
E EEC
P Program
}
type EHiding struct{
channel Channel
E EEC
}
type ETryCatch struct{
channels mapset.Set
Try EEC
Catch Program
}
func (h EHole) plug(p Program) Program {
return p
}
func (d ERec) plug(p Program) Program {
return Recursion{declarations:d.D, P:d.E.plug(p)}
}
func (d ESeq) plug(p Program) Program {
return Sequencing{P:d.E.plug(p), Q:d.P}
}
func (ep EParallel) plug(p Program) Program {
return Parallel{P:ep.E.plug(p), Q:ep.P}
}
func (h EHiding) plug(p Program) Program {
return Hiding{channel:h.channel, P:h.E.plug(p)}
}
func (tc ETryCatch) plug(p Program) Program {
return TryCatch{channels: tc.channels, Try:tc.Try.plug(p), Catch:tc.Catch}
}
func (r Request) unplugC() (CEC, Program) {
return CHole{}, r
}
func (a Accept) unplugC() (CEC, Program) {
return CHole{}, a
}
func (o Output) unplugC() (CEC, Program) {
return CHole{}, o
}
func (i Input) unplugC() (CEC, Program) {
return CHole{}, i
}
func (s Select) unplugC() (CEC, Program) {
return CHole{}, s
}
func (b Branch) unplugC() (CEC, Program) {
return CHole{}, b
}
func (tc TryCatch) unplugC() (CEC, Program) {
return CHole{}, tc
}
func (t Throw) unplugC() (CEC, Program) {
return CHole{}, t
}
func (c Conditional) unplugC() (CEC, Program) {
return CHole{}, c
}
func (p Parallel) unplugC() (CEC, Program) {
return CHole{}, p
}
func (s Sequencing) unplugC() (CEC, Program) {
// Only Sequencing and Recursion are interesting
ec, p := s.P.unplugC()
return CSeq{C:ec, P:s.Q}, p
}
func (i Inaction) unplugC() (CEC, Program) {
return CHole{}, i
}
func (h Hiding) unplugC() (CEC, Program) {
return CHole{}, h
}
func (r Recursion) unplugC() (CEC, Program) {
// Only Sequencing and Recursion are interesting
ec, p := r.P.unplugC()
return CRec{D:r.declarations, C:ec}, p
}
func (pc ProcessCall) unplugC() (CEC, Program) {
return CHole{}, pc
}
func (r Request) unplugE() (EEC, Program) {
return EHole{}, r
}
func (a Accept) unplugE() (EEC, Program) {
return EHole{}, a
}
func (o Output) unplugE() (EEC, Program) {
return EHole{}, o
}
func (i Input) unplugE() (EEC, Program) {
return EHole{}, i
}
func (s Select) unplugE() (EEC, Program) {
return EHole{}, s
}
func (b Branch) unplugE() (EEC, Program) {
return EHole{}, b
}
func (tc TryCatch) unplugE() (EEC, Program) {
ec, p := tc.Try.unplugE()
return ETryCatch{channels: tc.channels, Try:ec, Catch:tc.Catch}, p
}
func (t Throw) unplugE() (EEC, Program) {
return EHole{}, t
}
func (c Conditional) unplugE() (EEC, Program) {
return EHole{}, c
}
func (p Parallel) unplugE() (EEC, Program) {
ec, pr := p.P.unplugE()
return EParallel{E:ec, P:p.Q}, pr
}
func (s Sequencing) unplugE() (EEC, Program) {
ec, p := s.P.unplugE()
return ESeq{E:ec, P:s.Q}, p
}
func (i Inaction) unplugE() (EEC, Program) {
return EHole{}, i
}
func (h Hiding) unplugE() (EEC, Program) {
ec, p := h.P.unplugE()
return EHiding{channel: h.channel, E:ec}, p
}
func (r Recursion) unplugE() (EEC, Program) {
ec, p := r.P.unplugE()
return ERec{D:r.declarations, E:ec}, p
}
func (pc ProcessCall) unplugE() (EEC, Program) {
return EHole{}, pc
}
// SECTION step definitions.
// step is a one-step transition in the reduction semantics
// (well... IF the semantics was defined nicely)
func (r Request) stepC() (Program, bool){
return nil, false
}
func (a Accept) stepC() (Program, bool){
return nil, false
}
func (o Output) stepC() (Program, bool){
return nil, false
}
func (i Input) stepC() (Program, bool){
return nil, false
}
func (s Select) stepC() (Program, bool){
return nil, false
}
func (b Branch) stepC() (Program, bool){
return nil, false
}
func (t TryCatch) stepC() (Program, bool){
return nil, false
}
func (t Throw) stepC() (Program, bool){
return nil, false
}
func (c Conditional) stepC() (Program, bool){
return nil, false
}
func (p Parallel) stepC() (Program, bool){
// this is the only interesting stepping. Tries to match a session
// We try to match structurally for the body of rule [Link]
req, isRequest := p.P.(Request)
if isRequest && len(req.others)>0{
// Then find out how many accepting participants i require and chech that they are defined as a parallel IN ORDER :)
programs := make([]Program,0,len(req.others))
accept := p.Q
for i :=0; i<(len(req.others)-1); i++{
par , isParallel := accept.(Parallel)
if !isParallel{
return nil, false
}
programs = append(programs, par.P)
accept = par.Q
}
accepts := make([]Accept, len(programs), len(programs))
for i, candidate := range programs {
if accept, isAccept := candidate.(Accept); isAccept {
accepts[i] = accept
} else {
return nil, false
}
}
channels := accepts[0].channels
var program Program
program = Inaction{}
for _, accept := range accepts {
if !channels.Equal(accept.channels) {
return nil, false
}
program = Parallel{Q: program, P: accept.P}
}
// At this point, it is structurally as required. We can return the binding.
for _, channel := range channels.ToSlice(){
program = Hiding{channel: channel.(Channel), P: program}
}
return program, true
}
return nil, false
}
func (s Sequencing) stepC() (Program, bool){
return nil, false
}
func (i Inaction) stepC() (Program, bool){
return nil, false
}
func (h Hiding) stepC() (Program, bool){
return nil, false
}
func (r Recursion) stepC() (Program, bool){
return nil, false
}
func (p ProcessCall) stepC() (Program, bool){
return nil, false
}
func (r Request) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
return nil, false
}
func (a Accept) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
return nil, false
}
func (o Output) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
value, error := o.expression.eval()
if error == nil {
queue, exists := queues[o.channel]
if !exists{
//rule [Send2]
queues[o.channel] = make(chan Message,256)
queue = queues[o.channel]
}
// rule [Send1]
queue <- value
return Inaction{}, true
}
return nil, false
}
func (i Input) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
// [Recv] Rule
select {
case value := <- queues[i.channel]:
return i.P.subst(i.x,value.(Exp)), true
case <-time.After(time.Second):
}
return nil, false
}
func (s Select) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
if _ , exists := queues[s.channel]; !exists{
queues[s.channel]=make(chan Message, 256)
}
queues[s.channel]<-s.label
return s.P, true
}
func (b Branch) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
pick := rand.Intn(len(b.branches))
i := 0
for l, P := range b.branches{
if i == pick {
queues[b.channel] <- l
return P, true
}
i++
}
return nil, false
}
func (t TryCatch) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
return nil, false
}
func (t Throw) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
return nil, false
}
func (c Conditional) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
return nil, false
}
func (p Parallel) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
return nil, false
}
func (s Sequencing) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
return nil, false
}
func (i Inaction) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
return nil, false
}
func (h Hiding) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
return nil, false
}
func (r Recursion) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
if PR, ok := r.P.(Parallel); ok{
P := PR.P
Q := PR.Q
if pc, ok := P.(ProcessCall); ok{
values := make([]Exp, len(pc.expressions), len(pc.expressions))
for i, exp := range pc.expressions{
v, err := exp.eval()
if err != nil{
return nil, false
}
values[i] = v.(Exp)
}
if D, ok := r.declarations[pc.name]; ok{
P = D.P
if D.channels.Equal(pc.channels){
if len(D.identifiers) == len(values){
for i := range values {
P = P.subst(D.identifiers[i], values[i])
}
return Recursion{
declarations: r.declarations,
P: Parallel{P:P, Q:Q},
}, true
}
}
}
}
}
return nil, false
}
func (p ProcessCall) stepE(exceptions []Throw, queues map[Channel]MultilevelQueue) (Program, bool){
return nil, false
}
func (r Request) eval (exceptions []Throw, queues map[Channel]MultilevelQueue) Program{
// request and accepts don't do anything on their own.
// only when in parallel and under a CEC they reduce through link
return r
}
func (a Accept) eval (exceptions []Throw, queues map[Channel]MultilevelQueue) Program{
return a
}
func (o Output) eval (exceptions []Throw, queues map[Channel]MultilevelQueue) Program {
if step, ok := o.stepE(exceptions, queues); ok {
return step
}
return o
}
func (i Input) eval (exceptions []Throw, queues map[Channel]MultilevelQueue) Program {
if step, ok := i.stepE(exceptions, queues); ok {
return step
}
return i
}
func (s Select) eval (exceptions []Throw, queues map[Channel]MultilevelQueue) Program {
if step, ok := s.stepE(exceptions, queues); ok {
return step
}
return s
}
func (b Branch) eval (exceptions []Throw, queues map[Channel]MultilevelQueue) Program {
if step, ok := b.stepE(exceptions, queues); ok {
return step
}
return b
}
func THRexceptionsPrecondition(exceptions []Throw, channels mapset.Set) bool{
for _, thr := range exceptions{
for _, throwncandidate := range thr.channels.ToSlice(){
if thrown, ok := throwncandidate.(Channel); ok{
for _, precond := range channels.ToSlice(){
if precondition, ok := precond.(Channel); ok{
if precondition.name == thrown.name && thrown.level > precondition.level{
// precondition doesn't hold.
return false
}
} else {
//there was a non channel on the set.
return false
}
}
} else {
// There was a non channel on the set. boo golang. boo.
return false
}
}
}
return true
}
func HasThrown( exceptions []Throw, channels mapset.Set) bool{