Save current continuation in SMLofNJ

I read this fun page explaining the sequel to the racket.

They present code to preserve the ongoing computation (and use this trick later to implement backtracking). The code is as follows:

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

Now I would like to do the same trick in standard ML, and since SML from New Jersey seems to be the only translator implementing the sequel, I do it there.

continued subscription is as follows:

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

Direct translation will be:

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

but the ML type system forbids this (since it 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 links, but I canโ€™t find a way to really save the current continuation (because in this example I change the desired continuation with! glcc).

Does anyone know of a way to implement savecc and backjump that allow me to save the current continuation of the program and then return to this point in standard ML?

Thank you very much in advance!

Yannik

+6
source share

All Articles