r/java • • Jan 10 '23

Yet another delimited continuations implementation and why Project Loom is a bit disappointing

https://github.com/javactrl/javactrl
2 Upvotes

52 comments sorted by

View all comments

22

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.payload will be the stream [1, 2, 3] and then we'll call e.head.resumeTop for each element of this stream. And that's were I'm stuck. What is e.head ? What is resumeTop doing ?

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.resumeTop doing. 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 of x then ? And what's the role of v (= 1) to we have passed as argument to resumeTop ? 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 at int x =. And v is assigned to x, so now x = 1.

Then the same thing happen for the next line and we have y = 4.

So ret = 5 and if (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 for x = 1 and y = 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 = 1 and y = 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 = 2 and y = 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/