Question

An API for multi-prompt delimited continuations is described in the paper Delimited Control in OCaml, Abstractly and Concretely System Description.

My question concerns the type of push_subcont : ('a,'b) subcont -> (unit -> 'a) -> 'b. Why is this type not ('a,'b) subcont -> 'a -> 'b? Furthermore, why is there a separate type for subconts: why not simply ('a,'b) subcont = 'a -> 'b? I'm almost certain there is a good reason for that, because Oleg makes things as elegant as possible (but not more elegant).

Thanks!

Was it helpful?

Solution

Why not ('a,'b) subcont -> 'a -> 'b?

I think it is for the same reason as for push_prompt -- which is easier to understand. push_prompt p (fun () -> e) is intuitively a form of try e with p: the prompt p is placed on the stack as a handler, and e runs under this handler. If you used push_prompt p e instead, a strict language would evaluate the arguments p and e first, and e would run and "raise exceptions" before the prompt is set.

push_subcont sk (fun () -> e) could have the same kind of problems: it is a kind, intuitively, of "restart the computation sk that was interrupted by an exception". It is important that e is run inside the context of the computation, rather than outside it, for example if it wishes to raise exceptions corresponding to handlers installed by sk.

Why not simply ('a,'b) subcont = 'a -> 'b?

That could be done if there was only one way to restart subcontinuations: they could be returned "pre-restarted", under the form of functions that, when applied, restarts with the given argument.

But that is not the case: there are push_subcont and push_delim_subcont, described at the end of the article, that have different semantics. The "caller" should choose which restart technique to use. They both need to access internal data of the subcontiuation, so they could not operate on the subcontinuation-as-function representation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top