Munksgaard / session-types

MIT License
550 stars 21 forks source link

Arity specialized branching constructs #23

Open Munksgaard opened 9 years ago

Munksgaard commented 9 years ago

This is an experiment that introduces arity specialized branching constructs. It's not meant to be immediately merged, but to serve as a discussion point regarding this alternative library design, and whether it's something we might want.

The idea is to add multiple specialized Offer/Choose constructs according to arity. That way, instead of chaining lots of Offer's and having to do sel2().sel2().sel2(), we'd simply have an OfferN with associated sel1(), sel2(), ..., and selN() methods.

So far I've only introduced Offer3 and Choose3 structs, but they serve to give us an idea of how the library would end up looking.

Edit: Almost all of the tests and examples will be failing right now, but that's to be expected. Consult the ATM example (examples/atm.rs) to see how it looks from a users perspective.

Munksgaard commented 9 years ago

We'd definitely want some macros to actually define the different constructs and methods instead of doing it manually.

Munksgaard commented 9 years ago

Pros:

Cons:

match c.offer() {
    Branch3::B1(c) => ...
    Branch3::B2(c) => ...
    Branch3::B3(c) => ...
}
...

If you later change the arity of the branch you have to actually change all of these Branch3s to Branch4s, for example.

Manishearth commented 9 years ago

I wonder if we can make this work with tuples?

Munksgaard commented 9 years ago

Good idea, @Manishearth! Check out c936af4. We still need the BranchN structs as far as I can tell, but we get rid of the multitude of OfferN structs.

Manishearth commented 9 years ago

We could just impl Dual on all the tuple types with a macro, and then the rest should follow on generic Offer<T> and Choose<T> structs.

Manishearth commented 9 years ago

Oh, but the enums need to be made no matter what. In that case we shouldn't complicate it with tuples and just macro all the things I guess. The macro for this is pretty simple afaict.

Munksgaard commented 9 years ago

I actually think I prefer the tupled version over the OfferN version regardless.

Manishearth commented 9 years ago

SGTM. We can always make OfferN typedefs anyway.