6

I was reading through this funny page explaing continuations in racket.

They present code to save the current continuation of a computation (and use this trick later to implement backtracking). The code looks as follows:

(define (currcc)
 (call/cc (lambda (cc) (cc cc))))

Now I'd like to do the same trick in Standard ML, and since SML of New Jersey seems to be the only interpreter implementing continuations I'm doing it there.

The continuation signature there looks as follows:

type 'a cont
val callcc : ('a cont -> 'a) -> 'a
val throw : 'a cont -> 'a -> 'b
val isolate : ('a -> unit) -> 'a cont

The direct translation would be:

val currcc () = callcc (fn cc => throw cc cc)

but the type system of ML forbids this (as this is circular).

So I tried something like:

val glcc = ref (isolate (fn x => ()) : unit cont)    
fun savecc () = (callcc (fn (cc : unit cont) => glcc := cc); !glcc)
fun backjump () = throw (!glcc) ();

and various other tricks with references, but I can't find a way to really save the current continuation (because I change the wanted continuation by the use of !glcc afterwards, in this example).

Does anybody know about a way to implement operators savecc and backjump that let me save the current continuation of a program and jump back to that point afterwards in Standard ML?

Many thanks in advance!

Yannick

Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202

0 Answers0