4 ms·
A linear type system isn't strictly necessary for this pattern; this is a common trick in Rust, which only has affine types: http://insanitybit.github.io/2016/0
by kibwen 9y ago
A linear type system isn't strictly necessary for this pattern; this is a common trick in Rust, which only has affine types: http://insanitybit.github.io/2016/05/30/beyond-memory-safety-with-types http://insanitybit.github.io/2016/05/30/beyond-memory-safety... . Indeed, it's lovely for validating state machines at compile time.
- runeks 9y agoRust can’t guarantee that a value is consumed exactly once. This example creates a monad parametized over how many times a return value must be consumed. It has two settings: 1 (exactly once), and ω, which I’m prettying sure is the Greek abbreviation of “whatever” (zero or more times).
- kibwen 9y ago> Rust can’t guarantee that a value is consumed exactly once. As far as I can tell this isn't relevant to the pattern being discussed though. All that matters is that a type can be consumed at most once, which is a capability provided by both linear and affine types. If you're worried about guaranteeing that the state machine ends gracefully (in this case, the socket being closed), then that's the role that destructors play. Indeed, the fact that destructors exist is what initially gives the impression of Rust having a linear type system; the only time that Rust doesn't guarantee exactly-once behavior (degenerating to at-most-once) is when the destructor leaks, which is pretty hard to do by accident (e.g. creating cyclical references in Rust is non-trivial).
- masklinn 9y agoFor this specific state machine case though, an affine type system means you can't statically ensure the machine reaches a terminal state.
- kibwen 9y agoIt seems as though one could have the transition to the terminal state return a unique type, and then execute the entire state machine inside a function whose return type is the return type of the terminal transition, yes?
- masklinn 9y agoSure but now you can't really drive the state machine can you? The state machine has to drive you and provide callbacks for state transitions, something like that. edit: ah I see you'd extract the entire manipulation in a function which would return a terminal type or union thereof, yeah that'd work I guess.
- octachron 9y ago> ω, which I’m prettying sure is the Greek abbreviation of “whatever” (zero or more times). Or it is the classical notation for the ordinal associated with the set of integers ℕ, aka any number of times.
- masklinn 9y agoYup. On the other hand, as an expansion of that pattern linear type system allows ensuring (statically) the state machine reaches a terminal state. With an affine type system you could just drop the computation halfway.