by Twey · 3w ago · 0 comments
Sure, the same can be said of an async library — so long as you're okay with none of your other flows being able to make progress in the meantime. You can just poll in a loop.
> It's the best of both world but as always you have a tradeoff, and in this case it's the ability to express the state machine as if it was a straightforward function.
‘The best of both worlds’ means that it shares the advantages of both, but the main (arguably only) advantage of async/await is that you get to write your temporally separated flows sequentially. (And maybe also that they look a bit like functions, I guess.) That's not true here.
> A Sans-IO library needs none of these.
To be clear I'm not by any means arguing that ‘sans-IO’ (or, as we used to call it, pure functional programming) is a bad thing. I'm very in favour of it. I'm just saying that a ‘sans-IO’ design shouldn't need to eschew async/await, which you can write in terms of pure functions.
(As a sibling comment points out, there are languages where that is not true, i.e. async/await inherently introduces some kind of side effect around concurrency or a global event loop. Thankfully that's not the case in Rust.)
An generic future is much harder to be called like this. You need to to create wakers and if you don't properly handle them you'll busy loop. You also need to provide the exact runtime the future needs for its I/O.
> so long as you're okay with none of your other flows being able to make progress in the meantime
And isn't that what blocking code does all the time?
> ‘The best of both worlds’ means that it shares the advantages of both
If you look at it from the perspective of a caller:
- you can easily use it from blocking code without needing a runtime - you can easily use it from async code without blocking the executor
As such it does provide the best of both worlds to the caller.
It is true that it's the worst of both worlds for who is writing the Sans-IO library though.
> or, as we used to call it, pure functional programming
You can for sure describe this as "pure" and "functional", but I bet most people think of a different paradigm when talking about pure functional programming.
You don't actually have to handle I/O with the async runtime if you don't want. You _could_ just poll the futures yourself in a loop, which is roughly the same as what happens if you just call the transition function in a loop for a manual state machine. And likewise if you just keep calling the transition function with empty input you'll also busy-loop.
> And isn't that what blocking code does all the time?
Sure, if you have only one flow you care about or you have threads to take care of your concurrency, this is fine. You can do it with any model (blocking, async, manual state machine).
> - you can easily use it from blocking code without needing a runtime - you can easily use it from async code without blocking the executor
But these things are already true of a state machine compiled from async/await instead of written by hand. `poll()` is just a function, and you can call it where you like. By the contract of `Future`, just like the contract of the state machine transition, it's guaranteed to ‘not block’. The waker machinery exists to help you write engines that decide when to poll, but if you've got your own external notion of that you're free to just call `poll()`. The standard library even has some optimization around this case, e.g. `Waker::noop()`.
> You can for sure describe this as "pure" and "functional", but I bet most people think of a different paradigm when talking about pure functional programming.
I agree, but it's confusing to me that this is the case. Original pure functional programming was all about ‘how can we write more useful programs without any effects’ and ‘what benefits can we get from doing so’. I think those questions are kind of answered; since Moggi's work in the early nineties it's been focused more on ‘how can we keep the benefits of pure functions while also being able to encode effectful logic?’, to which manually-written state machines are a rather primitive sort of answer. They bear pretty strong resemblance to iteratees/transducers/pipes/etc. from that lineage, except that those things aim to build a higher-level combinator language on top to reduce the amount of manual state-machine writing one has to do.
How would the future get the I/O result then?
> And likewise if you just keep calling the transition function with empty input you'll also busy-loop.
This makes no sense, why would I ever want to do that?
> but if you've got your own external notion of that
That's a very big if. I said "easily" and this is not easy at all.
Moreover it ignores the context: the SansIO/async/await code is generally a library written by a user, while the code that wants to use it is written by someone else entirely. Good luck sharing this external notion between the two!
---
I agree that it would be ideal to have a way to automatically generate the state machine from straightforward looking code, and async/await kinda does this, but this usecase requires more flexibility than what async/await provides.
You can write directly into the future (if you own it and know its structure), use a channel, etc. It's not the best ergonomics (see the comment uptree about this) but it's doable.
> This makes no sense, why would I ever want to do that?
Why would you ever want to wait on a Waker that you won't wake? :)
> That's a very big if. I said "easily" and this is not easy at all.
> Moreover it ignores the context: the SansIO/async/await code is generally a library written by a user, while the code that wants to use it is written by someone else entirely. Good luck sharing this external notion between the two!
It's not more difficult than with the manual state machine? That also has no general notion of inputs — you have to call specific functions to advance particular states with their inputs. There's no general equivalent of `poll`; the library and its caller have to agree on an API.
> I agree that it would be ideal to have a way to automatically generate the state machine from straightforward looking code, and async/await kinda does this, but this usecase requires more flexibility than what async/await provides.
I'm not trying to claim that (Rust's particular flavour of) async/await is suitable here. It certainly has some sharp edges, and it may well be that some of them bite for this project. I'd have loved to see an analysis of what and how, if so.
My point is just that use of async/await is not excluded by wanting to write some code ‘sans-I/O’. That's not a claim that it's universally applicable to all problems.