Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arity specialized branching constructs #23

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ chan_select = ["compiletest_rs"]

[dependencies]
compiletest_rs = { version = "*", optional = true }

[dependencies.branch_impls]
path = "src/branch_impls"
97 changes: 50 additions & 47 deletions examples/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,39 @@ use std::thread::spawn;

// Offers: Add, Negate, Sqrt, Eval
type Srv =
Offer<Eps,
Offer<Recv<i64, Recv<i64, Send<i64, Var<Z>>>>,
Offer<Recv<i64, Send<i64, Var<Z>>>,
Offer<Recv<f64, Choose<Send<f64, Var<Z>>, Var<Z>>>,
Recv<fn(i64) -> bool, Recv<i64, Send<bool, Var<Z>>>>>>>>;
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 {
c = offer!{ c,
CLOSE => {
use session_types::Branch5::*;
c = match c.offer() {
B1(c) => {
c.close();
return
},
ADD => {
B2(c) => {
let (c, n) = c.recv();
let (c, m) = c.recv();
c.send(n + m).zero()
},
NEGATE => {
B3(c) => {
let (c, n) = c.recv();
c.send(-n).zero()
},
SQRT => {
B4(c) => {
let (c, x) = c.recv();
if x >= 0.0 {
c.sel1().send(x.sqrt()).zero()
} else {
c.sel2().zero()
}
},
EVAL => {
B5(c) => {
let (c, f) = c.recv();
let (c, n) = c.recv();
c.send(f(n)).zero()
Expand All @@ -53,43 +54,44 @@ fn server(c: Chan<(), Rec<Srv>>) {
// uses of session types, but they do showcase subtyping, recursion and how to
// work the types in general.

type AddCli<R> =
Choose<Eps,
Choose<Send<i64, Send<i64, Recv<i64, Var<Z>>>>, R>>;
type AddCli<R, S, T> =
Choose<(Eps,
Send<i64, Send<i64, Recv<i64, Var<Z>>>>,
R, S, T)>;

fn add_client<R>(c: Chan<(), Rec<AddCli<R>>>) {
let (c, n) = c.enter().sel2().sel1().send(42).send(1).recv();
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> =
Choose<Eps,
Choose<R,
Choose<Send<i64, Recv<i64, Var<Z>>>,
S>>>;
type NegCli<R, S, T> =
Choose<(Eps,
R,
Send<i64, Recv<i64, Var<Z>>>,
S, T)>;

fn neg_client<R, S>(c: Chan<(), Rec<NegCli<R, S>>>) {
let (c, n) = c.enter().skip2().sel1().send(42).recv();
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,
Choose<R,
Choose<S,
Choose<Send<f64, Offer<Recv<f64, Var<Z>>, Var<Z>>>,
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().skip3().sel1().send(42.0).offer() {
Left(c) => {
match c.enter().sel4().send(42.0).offer() {
B1(c) => {
let (c, n) = c.recv();
println!("{}", n);
c.zero().sel1().close();
}
Right(c) => {
B2(c) => {
println!("Couldn't take square root!");
c.zero().sel1().close();
}
Expand All @@ -99,48 +101,49 @@ fn sqrt_client<R, S, T>(c: Chan<(), Rec<SqrtCli<R, S, T>>>) {
// `fn_client` sends a function over the channel

type PrimeCli<R, S, T> =
Choose<Eps,
Choose<R,
Choose<S,
Choose<T,
Send<fn(i64) -> bool, Send<i64, Recv<bool, Var<Z>>>>>>>>;
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()
.skip4()
.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> =
Choose<Eps,
Choose<R,
Choose<Send<i64, Recv<i64, Var<Z>>>,
S>>>;
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>(c1: Chan<(), Rec<AskNeg<R, S>>>,
c2: Chan<(), Send<Chan<(AskNeg<R, S>, ()), Recv<i64, Var<Z>>>, Eps>>) {
let c1 = c1.enter().sel2().sel2().sel1().send(42);
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>(c1: Chan<(), Recv<Chan<(AskNeg<R, S>, ()), Recv<i64, Var<Z>>>, Eps>>) {
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);
Expand Down
62 changes: 27 additions & 35 deletions examples/atm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ use session_types::*;
use std::thread::spawn;

type Id = String;
type Atm = Recv<Id, Choose<Rec<AtmInner>, Eps>>;
type Atm = Recv<Id, Choose<(Rec<AtmInner>, Eps)>>;

type AtmInner = Offer<AtmDeposit,
Offer<AtmWithdraw,
Offer<AtmBalance,
Eps>>>;
type AtmInner = Offer<(AtmDeposit,
AtmWithdraw,
Eps)>;

type AtmDeposit = Recv<u64, Send<u64, Var<Z>>>;
type AtmWithdraw = Recv<u64, Choose<Var<Z>, Var<Z>>>;
type AtmBalance = Send<u64, Var<Z>>;
type AtmWithdraw = Recv<u64, Choose<(Var<Z>, Var<Z>)>>;

type Client = <Atm as HasDual>::Dual;

Expand All @@ -30,59 +28,53 @@ fn atm(c: Chan<(), Atm>) {
c.sel1().enter()
};
let mut balance = 0;

loop {
c = offer! {
c,
Deposit => {
c = match c.offer() {
Branch3::B1(c) => {
let (c, amt) = c.recv();
balance += amt;
c.send(balance).zero()
balance = amt;
c.send(balance).zero() // c.send(new_bal): Chan<(AtmInner, ()) Var<Z>>
},
Withdraw => {
Branch3::B2(c) => {
let (c, amt) = c.recv();
if amt > balance {
c.sel2().zero()
} else {
balance -= amt;
if amt <= balance {
balance = balance - amt;
c.sel1().zero()
} else {
c.sel2().zero()
}
},
Balance => {
c.send(balance).zero()
},
Quit => {
c.close();
break
}
}
Branch3::B3(c) => { c.close(); break }
};
}
}

fn deposit_client(c: Chan<(), Client>) {
let c = match c.send("Deposit Client".to_string()).offer() {
Left(c) => c.enter(),
Right(_) => panic!("deposit_client: expected to be approved")
B1(c) => c.enter(),
B2(_) => panic!("deposit_client: expected to be approved")
};

let (c, new_balance) = c.sel1().send(200).recv();
println!("deposit_client: new balance: {}", new_balance);
c.zero().skip3().close();
c.zero().sel3().close();
}

fn withdraw_client(c: Chan<(), Client>) {
let c = match c.send("Withdraw Client".to_string()).offer() {
Left(c) => c.enter(),
Right(_) => panic!("withdraw_client: expected to be approved")
B1(c) => c.enter(),
B2(_) => panic!("withdraw_client: expected to be approved")
};

match c.sel2().sel1().send(100).offer() {
Left(c) => {
match c.sel2().send(100).offer() {
B1(c) => {
println!("withdraw_client: Successfully withdrew 100");
c.zero().skip3().close();
c.zero().sel3().close();
}
Right(c) => {
B2(c) => {
println!("withdraw_client: Could not withdraw. Depositing instead.");
c.zero().sel1().send(50).recv().0.zero().skip3().close();
c.zero().sel1().send(50).recv().0.zero().sel3().close();
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions examples/echo-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ use session_types::*;

use std::thread::spawn;

type Srv = Offer<Eps, Recv<String, Var<Z>>>;
type Srv = Offer<(Eps, Recv<String, Var<Z>>)>;
fn srv(c: Chan<(), Rec<Srv>>) {

let mut c = c.enter();

loop {
c = offer!{ c,
CLOSE => {
c = match c.offer() {
Branch2::B1(c) => {
println!("Closing server.");
c.close();
break
},
RECV => {
Branch2::B2(c) => {
let (c, s) = c.recv();
println!("Received: {}", s);
c.zero()
Expand Down
6 changes: 3 additions & 3 deletions examples/many-clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::mpsc::{channel, Receiver};
use std::thread::spawn;
use rand::random;

type Server = Recv<u8, Choose<Send<u8, Eps>, Eps>>;
type Server = Recv<u8, Choose<(Send<u8, Eps>, Eps)>>;
type Client = <Server as HasDual>::Dual;

fn server_handler(c: Chan<(), Server>) {
Expand Down Expand Up @@ -34,12 +34,12 @@ fn server(rx: Receiver<Chan<(), Server>>) {
fn client_handler(c: Chan<(), Client>) {
let n = random();
match c.send(n).offer() {
Left(c) => {
B1(c) => {
let (c, n2) = c.recv();
c.close();
println!("{} + 42 = {}", n, n2);
},
Right(c) => {
B2(c) => {
c.close();
println!("{} + 42 is an overflow :(", n);
}
Expand Down
16 changes: 8 additions & 8 deletions examples/planeclip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ fn intersect(p1: Point, p2: Point, plane: Plane) -> Option<Point> {
}
}

type SendList<A> = Rec<Choose<Eps, Send<A, Var<Z>>>>;
type RecvList<A> = Rec<Offer<Eps, Recv<A, Var<Z>>>>;
type SendList<A> = Rec<Choose<(Eps, Send<A, Var<Z>>)>>;
type RecvList<A> = Rec<Offer<(Eps, Recv<A, Var<Z>>)>>;

fn sendlist<A: std::marker::Send+Copy+'static>
(c: Chan<(), SendList<A>>, xs: Vec<A>)
Expand All @@ -74,11 +74,11 @@ fn recvlist<A: std::marker::Send+'static>
let mut c = c.enter();
loop {
c = match c.offer() {
Left(c) => {
B1(c) => {
c.close();
break;
}
Right(c) => {
B2(c) => {
let (c, x) = c.recv();
v.push(x);
c.zero()
Expand All @@ -98,12 +98,12 @@ fn clipper(plane: Plane,
let (pt0, mut pt);

match ic.offer() {
Left(c) => {
B1(c) => {
c.close();
oc.sel1().close();
return
}
Right(c) => {
B2(c) => {
let (c, ptz) = c.recv();
ic = c.zero();
pt0 = ptz;
Expand All @@ -116,15 +116,15 @@ fn clipper(plane: Plane,
oc = oc.sel2().send(pt).zero();
}
ic = match ic.offer() {
Left(c) => {
B1(c) => {
if let Some(pt) = intersect(pt, pt0, plane) {
oc = oc.sel2().send(pt).zero();
}
c.close();
oc.sel1().close();
break;
}
Right(ic) => {
B2(ic) => {
let (ic, pt2) = ic.recv();
if let Some(pt) = intersect(pt, pt2, plane) {
oc = oc.sel2().send(pt).zero();
Expand Down
Loading