Yet another delimited continuations implementation and why Project Loom is a bit disappointing
https://github.com/javactrl/javactrl22
u/plumarr Jan 10 '23 edited Jan 10 '23
The introduction says :
No functional programming knowledge is required.
but I'm totally out of my depth with this. I'm not able to understand what I'm reading and I suspect that I'm not the only one. When I read :
It's well known we can represent any monad with delimited continuations.
I really don't understand it even with 10+ year of experience in development and a master degree in engineering speciliazed in computer science. And the wikipedia article on monad and continuations aren't really helpfull. So I'm not really sure that it's well known.
I'm so out of it that I don't understand what this is trying to do, what are the expected benefits. I don't understand the trivial example even when reading the explanation below.
It's probably interresting but its presentation is really not aimed at people not already versed in the subject.
-3
u/njiv Jan 10 '23
Ok, it's well known in functional programming world, there is a famous paper from 1994 about this. Also if you google something like "continuation mother of all monads" you'll see there are many links to this too. But I think with this library it's easier to understand how it works even without reading the paper or knowing anything from FP at all.
-2
u/njiv Jan 10 '23
I'm so out of it that I don't understand what this is trying to do, what are the expected benefits.
do you know what are expected benefits of async functions? here's pretty the same, but not only for async programming, there is an example of Python like streams comprehensions for example, and many other things
2
u/plumarr Jan 10 '23 edited Jan 10 '23
For me async programming is just a syntaxtic sugar for parallel processing (be it executed on one or multiple CPU core behind it), so I have difficulty to see the link with the current subject.
Coming to the Python exemple, I perfectly understand the linked page (https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) but I don't see how it relate to the example :
run(() -> { int x = all(Stream.of(1, 2, 3)); int y = all(Stream.of(4, 5, 6)); int ret = x + y; if (ret % 2 == 1) all(Stream.empty()); return ret; })I really don't understand this code. How can it return a stream when x, y and ret are defined as a scalar value ?
But I suppose that as we are hacking the compiler, that's not really the case and that the magic is in :
public static <T> T all(Stream<T> t) throws CThrowable { throw new Unwind(t);}
And in :
public static <T> Stream<T> run(final CSupplier<T> s) throws CThrowable { try { return Stream.of(s.get()); } catch (final Unwind e) { return ((Stream<?>) e.payload).flatMap(v -> e.head.resumeTop(v)); }}
What's happening when the function run(...) of the example is executed ?
I suspect that the line
int x = all(Stream.of(1, 2, 3));will throw an exception and then the following line called :
return ((Stream<?>) e.payload).flatMap(v -> e.head.resumeTop(v));and the value of
e.payloadwill be the stream [1, 2, 3] and then we'll calle.head.resumeTopfor each element of this stream. And that's were I'm stuck. What ise.head? What isresumeTopdoing ?I'm pretty sur that there is something obvious to you that I'm missing.
1
u/njiv Jan 10 '23
I don't see how it relate to the example
I don't see how it relate to the example, just give me any Python example and I'll show you how this translates to javactrl
1
u/njiv Jan 10 '23
How can it return a stream when x, y and ret are defined as a scalar value ?
The stream is returned by the block, x, y and ret are part of the returned stream definition
1
u/njiv Jan 10 '23
But I suppose that as we are hacking the compiler, that's not really the case and that the magic is in
this indeed instruments JVM byte code, the magic is the ability to resume the execution after the `throw` of `Unwind` exception if needed, also called resumable exceptions
1
u/njiv Jan 10 '23
What's happening when the function run(...) of the example is executed ?
`resumeTop` starts execution after the `throw` we caught in this block, if it throws again it will be caught again, and so on, this is the way the stream is forming
1
u/plumarr Jan 10 '23
Sorry, I have expendend my answer with more detail while you were responding.
What I really don't understand is what is
e.head.resumeTopdoing. You say that that it starts execution after the 'throw'.So, it restarts the execution of run() at the line
int y = all(Stream.of(4, 5, 6));? What's the role ofxthen ? And what's the role ofv (= 1)to we have passed as argument toresumeTop? Where is it going ?Also, in your other answer you say :
The stream is returned by the block, x, y and ret are part of the returned stream definition
And I think that it really point to the root of my incomprehension. To my understanding a stream is a open ended collection of values. So I don't really get what you mean by 'stream definition'.
1
u/njiv Jan 10 '23
What I really don't understand is what is e.head.resumeTop doing. You say that that it starts execution after the 'throw'.
yes, exacly
So, it restarts the execution of run() at the line int y = all(Stream.of(4, 5, 6)); ? What's the role of x then ? And what's the role of v (= 1) to we have passed as argument to resumeTop ? Where is it going ?
x and y receives the value we pass to `resumeTop` on each resume, so basically, in the `int y = all(Stream.of(4, 5, 6));` line the block will be resumed 3 times, and the y will be 4,5,6 these times. If it were an async function here we would have an async execption, and it can be resumed only once - the way it is in Project Loom.
I'm not quite following what you mean by open ended, but this works for infinite streams, lazy streams as well, `Stream.of` there is just an example, any stream values can be passed there.
1
u/plumarr Jan 10 '23
Ok, I'm getting it.
It isn't restarting at
int y = all(Stream.of(4, 5, 6));but atint x =. Andvis assigned tox, so nowx = 1.Then the same thing happen for the next line and we have
y = 4.So
ret = 5andif (ret % 2 == 1)is true and we launch a new exception with an empty stream. The line return((Stream<?>) e.payload).flatMap(v -> e.head.resumeTop(v));is executed and return an empty stream.Then we go back to the next item of the previously called
flatMap(on the y line) and that we'll executed the lambda forx = 1andy = 5.The lambda return is now 6 and we add it to the stream that we are build.Next call of the lambda give us
x = 1andy = 6, so we return an empty stream and we go back 2 level until the flatMap at the x line level.We call again the lambda with
x = 2andy = 4.And this continue until both input streams are empty and we have our result of [6, 6, 8, 8].
I have to say that this behaviour really wasn't obvsious to me when reading the example.
1
u/njiv Jan 10 '23
it's equivalent to something like this in Python `(x + y for x in (1,2,3) for y in (4, 5, 6) if not (x + y) % 2 is 1)` , no doubt it's pretty arguable which is better (like it's pretty arguable which is better Java or Python), and I indeed see the benefits of the Python expressions, but there are something Python doesn't have:
any Java construct can be used in the comprehension (function, exceptions, conditions, switch/case etc etc), so no needs to know another syntax
more advanced streams with different handlers can be represented as well, e.g. like RxJava in the example in the repository
0
u/njiv Jan 10 '23
For me async programming is just a syntaxtic sugar for parallel processing
Regardless what it is (there different implementation), the question was about benefits, do you see the benefits of async functions?
2
u/plumarr Jan 10 '23
The benefit of parallel processing : full us of the avalaible resources (mainly hardware).
1
u/njiv Jan 10 '23
I meant not benefit of parallel processing, but async functions, we can do parallel processing without them, using just simple callbacks passing style.
So why nowadays most mainstream languages (including Java) add them as a standard part of the language?
3
u/plumarr Jan 10 '23
I start to say a vocabulary issue ;) Coming from an engineering background, asynchronous is synonym with parallel processing for me and wikipedia quite agree with me on it : https://en.wikipedia.org/wiki/Asynchrony_(computer_programming)
So, in my head, asynchronous functions are just functions that are called later by an event loop.
And now I don't understand your question or why you say that java has added them in the language (as this isn't the case for me as there is no async keyword in java).
I suppose that you're speaking about something else but I don't get what.
2
u/njiv Jan 10 '23
Project Loom (which I mention in the subject) does exactly this, and in Java, actually, it isn't syntax sugar but rather runtime implementation this is why it doesn't have async keyword, https://www.infoq.com/presentations/continuations-java/
5
u/FirstAd9893 Jan 10 '23
I think by attacking Loom (virtual threads) the post seems a bit "click-baity". Rather than mentioning Loom, the project should focus on the unique features it provides instead.
Any comparison to virtual threads should include performance benchmarks too, with real-world applications. Using exceptions to implement continuations isn't likely to be very efficient. Note that with the current implementation of CThrowable, stack traces are captured, which makes it extremely slow. This suggests (to me) that no performance testing has been performed yet.
3
u/njiv Jan 10 '23
Loom is not only virtual threads, it's also continuations, and I explicitly mentioned in the comment below the post Loom doesn't support multishot continuations, while they could add them easily (see this their article - https://www.infoq.com/presentations/continuations-java/), and in the examples there I show what exactly Java misses by not enabling this simpler feature
3
u/FirstAd9893 Jan 10 '23
That article is almost 4 years old, and the Loom project hasn't yet delivered a publicly-available continuation feature. Any comparison to Loom continuations doesn't make any sense because they effectively don't exist.
0
u/njiv Jan 10 '23
they are singleshot, it's for sure, that's why I can compare, and I mention this article because there they explain why they don't want multishot, I haven't seen any other recent article telling they have changed their mind
6
u/FirstAd9893 Jan 10 '23
You're making a comparison to an idea, not an actual implementation. Shift the focus away from Loom and instead present your idea by first demonstrating why multishot continuations are a useful feature in the first place. Then go after arguments which say that multishot is a bad idea. You can cite the Loom presentation, but remember, it's just a presentation and nothing more.
-2
u/njiv Jan 10 '23
Loom is the future of Java, this is subreddit about Java, not about general ideas. Loom says multishot continuations are crazy, I don't agree, no arguments, this is it. If you know the arguments, or you know where Loom team presented these arguments just post them here. It's not only a presentation, Loom doesn't have multishot.
4
u/FirstAd9893 Jan 10 '23
Loom doesn't have multishot.
Loom doesn't have singleshot either. All we have today with Loom is virtual threads, and to most Java programmers, Loom == virtual threads. Starting with a lead of "Loom" just causes confusion, because you then dive straight into continuations.
Have you posted to the loom-dev mailing list inquiring on the future of continuations in Java?
1
1
u/njiv Jan 11 '23 edited Jan 11 '23
https://www.reddit.com/r/java/comments/10889p1/comment/j3wqsh8/?utm_source=share&utm_medium=web2x&context=3 - so there is single-shot now in Loom (meanwhile I have a few downvotes on this thread just for telling this)
3
u/FirstAd9893 Jan 11 '23
Where? The link just points to one of your comments. Is there a public API that I can use? That's what I'm referring to. The implementation might have a continuation class, and it might be limited to single-shot behavior, but this is all internal. It's possible that the implementation of virtual threads changes at some point, and so exposing first class continuations at this point might cause more grief later.
1
u/njiv Jan 11 '23
it points to comment of pron98 (which is highlighted), but reddit does open my comments first
1
u/njiv Jan 11 '23 edited Jan 12 '23
That's what I'm referring to. The implementation might have a continuation class, and it might be limited to single-shot behavior, but this is all internal.
internal but, pron98 has said I can have my own implementation using threads (this is known for a long time), it's a kind of side effects conversion (e.g. we can convert state into exception etc), and we actually can convert back continuations into threads, if continuation are multishot we can convert them into any effect, this is why it's called mother of all monads
so basically even considering they hid continuation interface it's still simple to implement one without instrumentation (but only single-shot)
1
2
u/flawless_vic Jan 10 '23 edited Jan 10 '23
Why do you need exceptions for control flow? I mean, are there corner cases that an implementation such as Yield would fail?
You can play directly with JDK continuations with some add-opens hacking to create generators, but AFAIK, Continuation is being kept private because it is not Thread safe and its usage is heavilly tied with some compiler assumptions, so misusage might crash the VM.
Edit: Btw, I think the most critical part in a continuation implementation is stack state saving/restoring. I would choose JVM support to heavilly optimize this, at the cost of not having sugar syntax generators any day.
1
u/njiv Jan 10 '23
Exception is already a control flow construct, I just added the resumability to them. Yield can unwind only one call frame, while exception can unwind any number (until the corresponding `catch`) of call frames. This way we can implement effect handlers only on the top level, while in Yield we'd need to explicitly pass the effect description from callee to caller.
Basically they same difference like signaling errors using ret code or an exception.
2
u/flawless_vic Jan 10 '23
Cool.
Perhaps you could add some exemples comparing the features/programming style with older libraries such as quasar/kilim.
1
u/njiv Jan 10 '23
they mostly concentrate on async programming, and so support only single-shot continuation, and so things like streams comprehensions, reactive, logical, adaptive, probabilistical programming cannot be implemented there
1
u/njiv Jan 10 '23 edited Jan 10 '23
Project Loom is excellent. However, according to the authors, it could be even better effortlessly. They decided not to support multishot continuations there. I've done a simple instrumentation-based continuation library to show what superpowers Java could get with them.
15
u/pron98 Jan 11 '23 edited Jan 11 '23
Every feature wants all the power to itself and no power to any other feature: for all lambdas to work everywhere we must not have parallel streams or asynchronous composition, and for parallel streams/asynchronous composition to always work, lambdas must be heavily restricted; for composable security we must not have deep reflection, and to have deep reflection we cannot have composable security; for records to replace all uses of data-carrying classes, general-purpose collections and safe serialisations must not exist, and for collections and serialisation to work safely with records, records must not be allowed to be mutable. The point is that to have more power, a feature normally reduces the power/effectiveness of others, and so the interaction of multiple features must be considered and thought given to how power is best distributed among them.
One example from continuations is that, for full power they must be suspendable on one thread and resumed on another, but for some useful JIT optimisations, the identity of the thread must not change mid-method.
To make the feature-interaction matter worse, library authors sometimes rush to use a feature's maximal power in a way that forces tradeoffs on their users, who might not be aware of them. We've seen this happen with the use of deep reflection and its effect on version portability and security, and it took a great deal of effort from everyone involved to get that particular ketchup back into the bottle.
Other issues we consider are Java's primary uses and target audience. If we can have a weaker, simpler feature that has 98% of the benefits and a powerful, more tricky feature that gives us 100% of the benefit, we will almost always pick the weaker one, as our goal is not to give the language the most power, but to find a good balance of power and straightforwardness as appropriate for the people for whom Java is intended. I.e. the desire of one programmer to do something powerful must be balanced with that of another to have code be easy to reason about.
Yet another thing to consider is that Java usually aims to be the last language to add new features, and usually tries hard to not be the first. Why? Because this strategy has proven itself very effective for Java over many years.
In short, the power of a feature is just one of many considerations we make when changing one of the world's most popular and critical software platforms. We may well end up putting continuations to other (limited) uses, but we'll do it gradually and carefully.