* Which control structure?
@ 2007-10-02 18:41 Andrej Bauer
2007-10-02 22:24 ` [Caml-list] " Andrej Bauer
0 siblings, 1 reply; 2+ messages in thread
From: Andrej Bauer @ 2007-10-02 18:41 UTC (permalink / raw)
To: caml-list
I would like to have a control structure which allows me to "copy
continuations". The idea is to obtain a continuation and then try what
would happen if we passed it various values.
An example: suppose we have a p : (unit -> bool) -> bool. I want to compute
(p (fun () -> false), p (fun -> true))
in a particularly strange way (I have my reasons), something like this:
callcc (fun k ->
p (fun () -> (callcc l -> k (l false, l true))); raise Badness)
This of course does not make any sense, but I am trying to convey an idea:
- we register the continuation k
- we start evaluating p on our specially crafted argument
- if p never evaluates the specially crafted argument we raise
an exception
- if p evaluates the special argument, we record "the continuation"
l and then we want to _evaluate it twice_: once by passing it
false and once by passing it true. We want to collect the results
and pass them to the outer continuation k.
I am asking, is there out there (in whatever language) something that
would let me do this?
Andrej
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [Caml-list] Which control structure?
2007-10-02 18:41 Which control structure? Andrej Bauer
@ 2007-10-02 22:24 ` Andrej Bauer
0 siblings, 0 replies; 2+ messages in thread
From: Andrej Bauer @ 2007-10-02 22:24 UTC (permalink / raw)
To: caml-list
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2007-10-02 22:24 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-02 18:41 Which control structure? Andrej Bauer
2007-10-02 22:24 ` [Caml-list] " Andrej Bauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox