-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy patharithmetic.rs
More file actions
170 lines (145 loc) · 4.5 KB
/
arithmetic.rs
File metadata and controls
170 lines (145 loc) · 4.5 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
// This is an implementation of the extended arithmetic server from
// Vasconcelos-Gay-Ravara (2006) with some additional functionality
#[macro_use]
extern crate session_types;
use session_types::*;
use std::thread::spawn;
// Offers: Add, Negate, Sqrt, Eval
type Srv =
Offer<(Eps,
Recv<i64, Recv<i64, Send<i64, Var<Z>>>>,
Recv<i64, Send<i64, Var<Z>>>,
Recv<f64, Choose<(Send<f64, Var<Z>>, Var<Z>)>>,
Recv<fn(i64) -> bool, Recv<i64, Send<bool, Var<Z>>>>)>;
fn server(c: Chan<(), Rec<Srv>>) {
let mut c = c.enter();
loop {
use session_types::Branch5::*;
c = match c.offer() {
B1(c) => {
c.close();
return
},
B2(c) => {
let (c, n) = c.recv();
let (c, m) = c.recv();
c.send(n + m).zero()
},
B3(c) => {
let (c, n) = c.recv();
c.send(-n).zero()
},
B4(c) => {
let (c, x) = c.recv();
if x >= 0.0 {
c.sel1().send(x.sqrt()).zero()
} else {
c.sel2().zero()
}
},
B5(c) => {
let (c, f) = c.recv();
let (c, n) = c.recv();
c.send(f(n)).zero()
}
}
}
}
// `add_client`, `neg_client` and `sqrt_client` are all pretty straightforward
// uses of session types, but they do showcase subtyping, recursion and how to
// work the types in general.
type AddCli<R, S, T> =
Choose<(Eps,
Send<i64, Send<i64, Recv<i64, Var<Z>>>>,
R, S, T)>;
fn add_client<R, S, T>(c: Chan<(), Rec<AddCli<R, S, T>>>) {
let (c, n) = c.enter().sel2().send(42).send(1).recv();
println!("{}", n);
c.zero().sel1().close()
}
type NegCli<R, S, T> =
Choose<(Eps,
R,
Send<i64, Recv<i64, Var<Z>>>,
S, T)>;
fn neg_client<R, S, T>(c: Chan<(), Rec<NegCli<R, S, T>>>) {
let (c, n) = c.enter().sel3().send(42).recv();
println!("{}", n);
c.zero().sel1().close();
}
type SqrtCli<R, S, T> =
Choose<(Eps,
R,
S,
Send<f64, Offer<(Recv<f64, Var<Z>>, Var<Z>)>>,
T)>;
fn sqrt_client<R, S, T>(c: Chan<(), Rec<SqrtCli<R, S, T>>>) {
match c.enter().sel4().send(42.0).offer() {
B1(c) => {
let (c, n) = c.recv();
println!("{}", n);
c.zero().sel1().close();
}
B2(c) => {
println!("Couldn't take square root!");
c.zero().sel1().close();
}
}
}
// `fn_client` sends a function over the channel
type PrimeCli<R, S, T> =
Choose<(Eps,
R,
S,
T,
Send<fn(i64) -> bool, Send<i64, Recv<bool, Var<Z>>>>)>;
fn fn_client<R, S, T>(c: Chan<(), Rec<PrimeCli<R, S, T>>>) {
fn even(n: i64) -> bool {
n % 2 == 0
}
let (c, b) = c.enter()
.sel5()
.send(even)
.send(42)
.recv();
println!("{}", b);
c.zero().sel1().close();
}
// `ask_neg` and `get_neg` use delegation, that is, sending a channel over
// another channel.
// `ask_neg` selects the negation operation and sends an integer, whereafter it
// sends the whole channel to `get_neg`. `get_neg` then receives the negated
// integer and prints it.
type AskNeg<R, S, T> =
Choose<(Eps,
R,
Send<i64, Recv<i64, Var<Z>>>,
S, T)>;
fn ask_neg<R: std::marker::Send + 'static, S: std::marker::Send + 'static, T: std::marker::Send + 'static>
(c1: Chan<(), Rec<AskNeg<R, S, T>>>,
c2: Chan<(), Send<Chan<(AskNeg<R, S, T>, ()), Recv<i64, Var<Z>>>, Eps>>) {
let c1 = c1.enter().sel3().send(42);
c2.send(c1).close();
}
fn get_neg<R: std::marker::Send + 'static, S: std::marker::Send + 'static, T: std::marker::Send + 'static>
(c1: Chan<(), Recv<Chan<(AskNeg<R, S, T>, ()), Recv<i64, Var<Z>>>, Eps>>) {
let (c1, c2) = c1.recv();
let (c2, n) = c2.recv();
println!("{}", n);
c2.zero().sel1().close();
c1.close();
}
fn main() {
connect(server, add_client);
connect(server, neg_client);
connect(server, sqrt_client);
connect(server, fn_client);
let (c1, c1_) = session_channel();
let (c2, c2_) = session_channel();
let t1 = spawn(move || server(c1));
let t2 = spawn(move || ask_neg(c1_, c2));
let t3 = spawn(move || get_neg(c2_));
let _ = t1.join();
let _ = t2.join();
let _ = t3.join();
}