caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Which control structure?
@ 2007-10-03  8:00 oleg
  2007-10-03  9:30 ` Andrej Bauer
  0 siblings, 1 reply; 3+ 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] 3+ messages in thread

* Re: [Caml-list] Which control structure?
  2007-10-03  8:00 [Caml-list] Which control structure? oleg
@ 2007-10-03  9:30 ` Andrej Bauer
  0 siblings, 0 replies; 3+ messages in thread
From: Andrej Bauer @ 2007-10-03  9:30 UTC (permalink / raw)
  To: oleg, caml-list

oleg@pobox.com wrote:
> 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.

I suspected that much, and hoped for a reply from you :-)

> The following code does compute (p false, p true) by
> really entering p only once (but exiting it twice).

Excellent, thank you.

> The argument to p must be a thunk, so we are able to enter p, or to get p to swallow the
> hook.

This is not a problem because in my actual non-simplified problem p is a 
functional of type (int -> bool) -> bool. Now let me see if I can apply 
delimited continuations in the non-simplified version. Thank you again!

Andrej


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

* Re: [Caml-list] Which control structure?
  2007-10-02 18:41 Andrej Bauer
@ 2007-10-02 22:24 ` Andrej Bauer
  0 siblings, 0 replies; 3+ 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] 3+ messages in thread

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-03  8:00 [Caml-list] Which control structure? oleg
2007-10-03  9:30 ` Andrej Bauer
  -- strict thread matches above, loose matches on Subject: below --
2007-10-02 18:41 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;
as well as URLs for NNTP newsgroup(s).