caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: oleg@pobox.com
To: Andrej.Bauer@fmf.uni-lj.si
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Which control structure?
Date: Wed,  3 Oct 2007 01:00:53 -0700 (PDT)	[thread overview]
Message-ID: <20071003080053.5B954A9A1@Adric.metnet.fnmoc.navy.mil> (raw)


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)


             reply	other threads:[~2007-10-03  8:02 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-03  8:00 oleg [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20071003080053.5B954A9A1@Adric.metnet.fnmoc.navy.mil \
    --to=oleg@pobox.com \
    --cc=Andrej.Bauer@fmf.uni-lj.si \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).