253 points · by todsacerdoti · 3y ago · 98 comments
When Erik Meijer worked with Gilad Bracha and others on the Dart team to bring async to Dart, he approached with a very similar "lets look at the table of all the combinations and make sure we didn't leave any holes". In this case, the two axes (effects) he considered were asynchrony and iteration. So Dart supports all four combinations:
* Synchronous single-value functions are just normal function declarations. Within one, you use `return` to yield the one value.
* Synchronous multi-value functions are generators, marked `sync`, which return a `Iterable<T>`. Within one, you use `yield` and `yield` to emit values.
* Asynchronous single-value functions are marked `async` and return a `Future<T>`. Within on, you use `await` to pause on asynchronous operations and consume values. A `return` valued is implicitly wrapped in a future.
* Asynchronous multi-value functions are marked `async` and return a `Stream<T>`. Within one, you can use both `yield` and `yield` to emit values and `await` to suspend and consume asynchronous values.
We also have an asynchronous `await for` statement that can be used to imperatively consume a stream.
There's no third axis for fallibility, because Dart chooses to comprehensively use exceptions for operations that fail (which I quite like).
Overall, it's a really rich, comprehensive approach to the problem. I have mixed feelings about it. It really does cover all the bases. But, in practice, `async` functions and `await for` are very* rarely used but add a lot of complexity to the language and engineering burden to the implementations.
\*
**
Either one works to get an * into your non-code block text.Still waiting for the day that HN supports Markdown for inline code. :(
Can you catch exceptions? Can you catch them when thrown iterating an Iterable, awaiting a Future, or iteratively awaiting values from a Stream?
It doesn't seem like you can get around needing fallibility. What if I have an asynchronous operation that can fail? What if I need a sequence of possibly-empty values?
There’s no axis for fallability because all operations are fallible. Yes, you can catch exceptions when iterating, awaiting, or iteratively awaiting. You have fallibility. What you don’t have is a way of marking operations as non-fallible.
You can catch exceptions. If an iterator throws, you can catch that by surrounding the for loop in a try catch block.
Futures are sort of like Result types in that a Future can complete with either a value or an error. Exceptions inside asynchronous functions are automatically caught and wrapped into error futures, which then propagate out through the returned future object. When you await a future, if the future completes with an error, that then gets unwrapped back into a thrown exception.
Incidentally, for curious readers, the first version of the Future trait in Rust always returned a Result<T, E> from its poll method. The second version decided to instead return T directly, since T could be Result<T, E> too, making things a bit more general, but also leading to a tiny bit of type tetris.
Doesn't mean that I'm saying anything about whatever Dart does here, just that I also think there's a connection here :)
(There's a secondary reason which is that Rust does not have higher-kinded types and thus cannot even express `Monad` to begin with. It recently got GATs which can encode it, but that feature was designed as an intentional alternative to full complexity of higher-kinded types.)
The author of TFA wrote this thread on the subject: https://twitter.com/withoutboats/status/1027702531361857536
I do believe, however, that there is a different design for a programming language that solves Rust's core needs (efficient and safe) and yet which supports something akin to generic "control-flow effects"... which, to be clear, might not fit the specific definition of Monad.
Maybe, for example, such a language simply doesn't support early return (a feature I can do without). Maybe it has linear types (which I maintain are missing from Rust, and yes: I have read the article that claims it is difficult and will note that that same article also firmly admits it is possible).
Maybe, instead of using Monad, the right interface for a control-flow effect is somehow designed around the concept of how optimizing compilers need to reify these effects into actual efficient code, or maybe the way to get them feels more like an unhygienic macro... the design space isn't limited here.
To that end, the one bit of this thread that I personally find valuable is the argument that borrowing is somehow totally incompatible with the idea of a yield point that is a function, which I find both surprising but also possible... I certainly haven't put a ton of thought into it and am so tired right now I likely couldn't even appreciate a trivial and complete proof of such :(.
Regardless: I also am not claiming that Rust shouldn't exist with its current trade offs (at least, for this reason... I do think its non-monadic design should have exceptions and am almost entirely sure they would be compatible with the rest of the language; I am extremely disappointed that they weren't provided).
However, to see this article talking about these three things and only using the word "monad" a single time buried in some table near the end was quite shocking as it is trying to lay out a ton of unusual terminology for something the author clearly understands deeply... it almost feels like they are actively avoiding speaking the word "monad" ;P.
(I suspect their reason for avoiding the word "monad" was that it is totally alien and obfuscatory to a significant part of the target audience, and it's relatively easy for those who are familiar to connect the dots themselves, as you clearly did.)
Joining the productivity of automatic memory management, with the performance tooling that linear types can provide.
Hence why I follow with great interest such experiments.
I did find the use of the term "register" slightly jarring given its somewhat overloaded nature between its natural language meaning, used here, and also the sense of "a specific place in which to store a datum". Reading the Wikipedia page on the term, I came across "diatype", which to me seemed to convey the same meaning without the potential confusion from the polysemantics of "register"...?
Whenever I encounter a dual like this I like to investigate the etymology. I assume the use in linguistics is by metaphor to its use in music, "the range of a voice or instrument." More broadly, the term register comes to us from the Latin for a list of items recorded, ultimately from a word for carrying. Maybe there's something to make of that in connection to each of these very different meanings, I don't know.
The problem with diatype is that register, although not an extremely common word, is I thought somewhat well known, whereas I've never heard of diatype. I've been known to overestimate how familiar words of this sort are to other people, though, so maybe they're equally obscure to most of my readers.
CPU registers carry values, musical registers carry tunes. (Or something very roughly along those lines, anyway; etymology is messy and complicated.)
Given that "withoutboats" is a Foucault reference, this shouldn't surprise anyone who's familiar with that intellectual tradition.
For a bit more discussion on this, there's a fantastic comment on the Reddit discussion https://www.reddit.com/r/rust/comments/11m0j30/comment/jbflz...
The full quote is "In civilizations without boats, dreams dry up, espionage takes the place of adventure and the police take the place of pirates," from his work "Of Other Spaces, Heterotopias."
The sorts of language/library constructs that are used are likely to vary across "people in (programming task) grouping".
Like the subjunctive mood. I imagine not super helpful for English speakers where we have exactly one verb that takes a change on subjunctive mood.
In most languages with it, entering the subjunctive mood requires a prefixing context/preposition which is analogous to function coloring system/async keyword
Isn't that what the "keyword generics" initiative is all about? The Rust dev team seems to be quite aware that algebraic effects in Rust might be desirable, but coming up with a good design might involve quite a bit of work. It took a long time for even the async MVP to shape up properly, and supporting full effects might be even more complicated.
[0] https://blog.rust-lang.org/inside-rust/2022/07/27/keyword-ge...
> In Rust, this distinction in register can look like this:
> - Will I use references and borrowing, or will I liberally use clone to avoid it?
> In general, Rust strives to have an obvious register which to operate which is still going to be performant enough (and hopefully accessible enough!), while allowing users to switch register when they need to.
One of the biggest ergonomics problems in Rust, in my experience, is that for certain things (like references vs clones, or generics vs enums vs dynamic dispatch), it can be very hard to change registers once you realize you need a different one. Often it means making changes throughout your entire codebase
I don't have an easy solution but I feel like I at least have words for the problem now
Awesome post! I hope things like that will move forward.
I haven't written much Rust but when I did I was very liberal with calls to .clone().
What is important to know is that your program is not automatically bad just because you decided not to use only references.
In my case the data was relatively small and I just wanted functionality. But I felt like if I could have worked out borrowing it would've generated more efficient code and less memory consumption.
> But I felt like if I could have worked out borrowing it would've generated more efficient code and less memory consumption.
That is possible! But while you're learning, this isn't the most important thing to focus on. And depending on what you're trying to do, using borrowing may be easy, or may be hard, and while you're trying to learn, you don't know enough to evaluate the tradeoff on that basis, so in my humble opinion, it is absolutely okay to clone, and is probably even what you should do in most cases.
Once you're feeling good enough about Rust to start digging deeper, working on trying to not do this is a valuable skill. But imho it's a waste of time when you're starting, and may lead you into a pit of despair.
I tend to follow a "clone first, figure out lifetimes later if profiling shows that it would be worthwhile" rule. If I always did the optimal thing with lifetimes, I'd never actually finish anything.
The closer you get to physical reality, the harder it is to be able to fit the world into your model, and you need to fit your model to the world. In Rust, unsafe is the way to manage this difficult set of tradeoffs
> Futures combinators are not used because the combinator model suffered some specific problems with state sharing in futures
Futures need to own all of their state. If you want to use that state across multiple combinators (in async/await terms, "across an await point"), given that rust doesn't have GC, you have to somehow make it available to both closures. This meant Arc<Mutex<_>> around things even though they were being used in sequence, because both closures needed to own the state and the compiler doesn't know the closures will only be called in sequence. It was a mess, and was a big hurdle for adoption of futures before async/await was added.
It's almost like there's also still some gap in the borrow checking, but it's not obvious to me how to address that.
I had been a little confused as I'd seen the combinators available in the futures crate (along with Stream) and it's a little tricky to figure out the status and review of techniques that are implemented there but not standardized.
"Please don't complain about tangential annoyances—e.g. article or website formats, name collisions, or back-button breakage. They're too common to be interesting."