If anybody cares, I can do what I want in SML/NJ using a particularly
ugly combination of callcc and store, see below. Is there a sane way to
achieve the same thing? I suspect I want delimited continuations, but I
am not sure.
-----
open SMLofNJ.Cont ;
(* A perverse way of computing (p false, p true) by invoking p only once. *)
fun two p =
let val c = ref NONE (* here we store the continuation *)
val a = ref NONE (* here we store p false *)
val b = ref NONE (* here we store p true *)
in
(* store p true into a, and save the continuation *)
a := SOME (p (callcc (fn k => (c := SOME k; true)))) ;
(* see if we're hapenning the first or the second time *)
if !b = NONE then (
(* first time around *)
b := !a ; (* store p false into b *)
(* reinvoke to compute p true *)
let val SOME k = !c in throw k false end)
else
(* second time around *)
let val SOME x = !a
val SOME y = !b
in
(x, y)
end
end
-----
Andrej