caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Which control structure?
@ 2007-10-02 18:41 Andrej Bauer
  2007-10-02 22:24 ` [Caml-list] " Andrej Bauer
  0 siblings, 1 reply; 4+ 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] 4+ messages in thread
* Re: [Caml-list] Which control structure?
@ 2007-10-03  8:00 oleg
  2007-10-03  9:30 ` Andrej Bauer
  0 siblings, 1 reply; 4+ messages in thread
From: oleg @ 2007-10-03  8:00 UTC (permalink / raw)
  To: Andrej.Bauer; +Cc: caml-list


Andrei Bauer wrote:

> (* A perverse way of computing (p false, p true) by invoking p only once. *)

I'm afraid the given code may invoke p twice:

        a := SOME (p (callcc (fn k => (c := SOME k; true)))) ;

Here, the continuation is captured while evaluating the argument of p
-- before p is even invoked. So, the captured continuation contains
the invocation of p. Invoking that continuation twice will enter p
twice.

> I can do what I want in SML/NJ using a particularly ugly combination
> of callcc and store,

In almost all useful circumstances call/cc appears in combination with
store -- which is a dead give-away that we are dealing with delimited
continuations. The following code does compute (p false, p true) by
really entering p only once (but exiting it twice). The argument to p
must be a thunk, so we are able to enter p, or to get p to swallow the
hook.

open Delimcc;;

let shift p f = take_subcont p (fun sk () ->
   push_prompt p (fun () -> (f (fun c ->
     push_prompt p (fun () -> push_subcont sk (fun () -> c))))))
;;

(* val shift : 'a Delimcc.prompt -> (('b -> 'a) -> 'a) -> 'b = <fun> *)

let abort p v = take_subcont p (fun sk () -> v);;
(* val abort : 'a Delimcc.prompt -> 'a -> 'b = <fun> *)

let two p =
  let prompt = new_prompt () in
  let result = new_prompt () in
  push_prompt result (fun () ->
   push_prompt prompt (fun () ->
     p (fun () -> shift prompt (fun sk -> 
       abort result (sk false, sk true))));
    failwith "can't happen")
;;

(* val two : ((unit -> bool) -> 'a) -> 'a * 'a = <fun> *)

let p arg = print_endline "P is invoked. Haven't evaled the arg yet";
            not (arg ());;
(* val p : (unit -> bool) -> bool = <fun> *)

let test = two p;;

(*
 P is invoked. Haven't evaled the arg yet
 val test : bool * bool = (true, false)
*)

The output proves that p has been entered only once. One might find
the technique of returning the result by aborting a bit unusual -- on
the other hand, when trying to deceive the devil all means are good...

Incidentally, the amb in OCaml does precisely the same tricks:
	http://okmij.org/ftp/ML/ML.html#amb

(and more, e.g., probabilistic execution)


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-10-03  9:30 UTC | newest]

Thread overview: 4+ 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
2007-10-03  8:00 oleg
2007-10-03  9:30 ` Andrej Bauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).