-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasyncpi.go
276 lines (232 loc) · 5.91 KB
/
asyncpi.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
// +build go1.8
package asyncpi
import (
"bytes"
"fmt"
"sort"
)
// Name is channel or value.
type Name interface {
Ident() string
String() string
}
type names []Name
func (n names) Less(i, j int) bool { return n[i].Ident() < n[j].Ident() }
// remDup removes duplicate Names from sorted []Name.
func remDup(names []Name) []Name {
m := make(map[string]bool)
for _, name := range names {
if _, seen := m[name.Ident()]; !seen {
names[len(m)] = name
m[name.Ident()] = true
}
}
return names[:len(m)]
}
// Process is process prefixed with action.
type Process interface {
FreeNames() []Name
FreeVars() []Name
// Calculi returns the calculi representation.
Calculi() string
String() string
}
// NilProcess is the inaction process.
type NilProcess struct{}
// NewNilProcess creates a new inaction process.
func NewNilProcess() *NilProcess {
return new(NilProcess)
}
// FreeNames of NilProcess is defined to be empty.
func (n *NilProcess) FreeNames() []Name {
return []Name{}
}
// FreeVars of NilProcess is defined to be empty.
func (n *NilProcess) FreeVars() []Name {
return []Name{}
}
func (n *NilProcess) String() string {
return "inact"
}
// Par is parallel composition of P and Q.
type Par struct {
Procs []Process
}
// NewPar creates a new parallel composition.
func NewPar(P, Q Process) *Par { return &Par{Procs: []Process{P, Q}} }
// FreeNames of Par is the free names of composed processes.
func (p *Par) FreeNames() []Name {
var fn []Name
for _, proc := range p.Procs {
fn = append(fn, proc.FreeNames()...)
}
sort.Slice(fn, names(fn).Less)
return remDup(fn)
}
// FreeVars of Par is the free names of composed processes.
func (p *Par) FreeVars() []Name {
var fv []Name
for _, proc := range p.Procs {
fv = append(fv, proc.FreeVars()...)
}
sort.Slice(fv, names(fv).Less)
return remDup(fv)
}
func (p *Par) String() string {
var buf bytes.Buffer
buf.WriteString("par[ ")
for i, proc := range p.Procs {
if i != 0 {
buf.WriteString(" | ")
}
buf.WriteString(proc.String())
}
buf.WriteString(" ]")
return buf.String()
}
// Recv is input of Vars on channel Chan, with continuation Cont.
type Recv struct {
Chan Name // Channel to receive from.
Vars []Name // Variable expressions.
Cont Process // Continuation.
}
// NewRecv creates a new Recv with given channel.
func NewRecv(u Name, P Process) *Recv {
return &Recv{Chan: u, Cont: P}
}
// SetVars give name to what is received.
func (r *Recv) SetVars(vars []Name) {
r.Vars = vars
}
// FreeNames of Recv is the channel and FreeNames of the continuation.
func (r *Recv) FreeNames() []Name {
var fn []Name
fn = append(fn, FreeNames(r.Chan)...)
fn = append(fn, r.Cont.FreeNames()...)
sort.Slice(fn, names(fn).Less)
for _, v := range r.Vars {
for i, n := range fn {
if IsSameName(v, n) {
fn = append(fn[:i], fn[i+1:]...)
break // next v
}
}
}
return remDup(fn)
}
// FreeVars of Recv is the channel and FreeVars in continuation minus received variables.
func (r *Recv) FreeVars() []Name {
var fv []Name
fv = append(fv, r.Cont.FreeVars()...)
sort.Slice(fv, names(fv).Less)
ffv := fv[:0] // filtered
for i, j := 0, 0; i < len(fv); i++ {
for j < len(r.Vars) && r.Vars[j].Ident() < fv[i].Ident() {
j++
}
if j < len(r.Vars) && r.Vars[j].Ident() != fv[i].Ident() { // overshoot
ffv = append(ffv, fv[i])
} else if i >= len(r.Vars) { // remaining
ffv = append(ffv, fv[i])
}
}
ffv = append(ffv, FreeVars(r.Chan)...)
sort.Slice(ffv, names(ffv).Less)
return remDup(ffv)
}
func (r *Recv) String() string {
return fmt.Sprintf("recv(%s,%s).%s", r.Chan.Ident(), r.Vars, r.Cont)
}
// Repeat is a replicated Process.
type Repeat struct {
Proc Process
}
// NewRepeat creates a new replicated process.
func NewRepeat(P Process) *Repeat {
return &Repeat{Proc: P}
}
// FreeNames of Repeat are FreeNames in Proc.
func (r *Repeat) FreeNames() []Name {
return r.Proc.FreeNames()
}
// FreeVars of Repeat are FreeVars in Proc.
func (r *Repeat) FreeVars() []Name {
return r.Proc.FreeVars()
}
func (r *Repeat) String() string {
return fmt.Sprintf("repeat(%s)", r.Proc)
}
// Restrict is scope of Process.
type Restrict struct {
Name Name
Proc Process
}
// NewRestricts creates consecutive restrictions from a slice of Names.
func NewRestricts(a []Name, p Process) *Restrict {
cont := p
for i := len(a) - 1; i >= 0; i-- {
cont = &Restrict{Name: a[i], Proc: cont}
}
return cont.(*Restrict)
}
// NewRestrict creates a new restriction.
func NewRestrict(a Name, P Process) *Restrict {
return &Restrict{Name: a, Proc: P}
}
// FreeNames of Restrict are FreeNames in Proc excluding Name.
func (r *Restrict) FreeNames() []Name {
var fn []Name
fn = append(fn, r.Proc.FreeNames()...)
sort.Slice(fn, names(fn).Less)
fn = remDup(fn)
for i, n := range fn {
if IsSameName(n, r.Name) {
fn = append(fn[:i], fn[i+1:]...)
break
}
}
return fn
}
// FreeVars of Restrict are FreeVars in Proc.
func (r *Restrict) FreeVars() []Name {
return r.Proc.FreeVars()
}
func (r *Restrict) String() string {
return fmt.Sprintf("restrict(%s,%s)", r.Name, r.Proc)
}
// Send is output of Vals on channel Chan.
type Send struct {
Chan Name // Channel to send to.
Vals []Name // Values to send.
}
// NewSend creates a new Send with given channel.
func NewSend(u Name) *Send {
return &Send{Chan: u}
}
// SetVals determine what to send.
func (s *Send) SetVals(vals []Name) {
s.Vals = vals
}
// FreeNames of Send is the channel and the Vals.
func (s *Send) FreeNames() []Name {
var fn []Name
fn = append(fn, FreeNames(s.Chan)...)
for _, v := range s.Vals {
fn = append(fn, FreeNames(v)...)
}
sort.Slice(fn, names(fn).Less)
return remDup(fn)
}
// FreeVars of Send is the Vals.
func (s *Send) FreeVars() []Name {
var fv []Name
fv = append(fv, FreeVars(s.Chan)...)
for _, v := range s.Vals {
fv = append(fv, FreeVars(v)...)
}
sort.Slice(fv, names(fv).Less)
return remDup(fv)
}
func (s *Send) String() string {
return fmt.Sprintf("send(%s,%s)", s.Chan.Ident(), s.Vals)
}