corda/test/extra/ComposableContinuations.java
Joel Dice 91e4d2b4a1 quick sketch of composable continuation implementation
I've been told by knowledgeable people that it is impossible to
implement composable continuations (AKA delimited continuations AKA
shift/reset) in terms of call-with-current-continuation.  Since I
don't yet understand why that is, I figured it would help my
understanding to attempt it and see how it fails.
2014-03-21 07:38:28 -06:00

44 lines
1.3 KiB
Java

package extra;
import static avian.Continuations.shift;
import static avian.Continuations.reset;
import avian.FunctionReceiver;
import avian.Function;
import java.util.concurrent.Callable;
public class ComposableContinuations {
private static void expect(boolean v) {
if (! v) throw new RuntimeException();
}
public static void main(String[] args) throws Exception {
expect(2 * reset(new Callable<Integer>() {
public Integer call() throws Exception {
return 1 + shift(new FunctionReceiver<Integer, Integer>() {
public Integer receive
(Function<Integer, Integer> continuation)
throws Exception
{
return continuation.call(5);
}
});
}
}) == 12);
expect(1 + reset(new Callable<Integer>() {
public Integer call() throws Exception {
return 2 * shift(new FunctionReceiver<Integer, Integer>() {
public Integer receive
(Function<Integer, Integer> continuation)
throws Exception
{
return continuation.call(continuation.call(4));
}
});
}
}) == 17);
}
}