Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jonathan Roewen <jonathan.roewen@gmail.com>
To: Stephen Brackin <stephen.brackin@verizon.net>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Ask-if-continue wrapper?
Date: Wed, 28 Dec 2005 22:36:06 +1300	[thread overview]
Message-ID: <ad8cfe7e0512280136t18283b25w8fb0eabeb241d003@mail.gmail.com> (raw)
In-Reply-To: <0IS700AHX9M9TKT2@vms042.mailsrvcs.net>

Sounds like you'd need continuations to do it nicely/properly. I have
no idea if it's possible to support continuations in ocaml. But why
not use a couple of threads?

Something like:

let continueq f args timeout timeout_val =
  let result = ref timeout_val in
  let on_done = ref (fun () -> ()) in (* hack cause I dunno how to get
around the order of declarations *)
  let do_computation () = result := f args; !on_done () in
  let comp_t = Thread.create do_computation () in
  let rec do_timeout t =
    if t <= 0 then
      Thread.kill comp_t
    else begin
      Thread.delay (float_of_int t);
      print_string "set timeout: ";
      do_timeout (read_int ());
    end in
  let timeout_t = Thread.create do_timeout timeout in
  on_done := (fun () -> Thread.kill timeout_t);
  Thread.join timeout_t;
  !result;;

Very ugly, but should do what you want. I have to add that one of the
thread implementations for ocaml doesn't implement Thread.kill -- I
can't remember which -- which would break this horribly :-|

Jonathan

On 12/28/05, Stephen Brackin <stephen.brackin@verizon.net> wrote:
>
>
> I'd like an OCaml function, which I'll call continueq, with the property
> that for any function f with argument(s) fargs,
>
> continueq f fargs tsecs defaultval
>
> starts evaluating f on fargs and lets this evaluation proceed for up to
> tsecs seconds. If the computation of (f fargs) completes in this time, then
> it returns the result of that computation. Otherwise, it asks the user how
> many seconds to let the computation of (f fargs) proceed. If the user inputs
> a value less than or equal to 0, then it returns defaultval. If the user
> inputs a value tsecs' greater than 0, then it evaluates
>
> continueq f' fargs' tsecs' defaultval
>
> where (f' fargs') denotes the computation state of (f fargs) at the time it
> was interrupted.
>
> I want to give the user the option of continuing without having to repeat
> earlier calculations. This is similar to the checkpoint utility ckpt, and
> similar to the "timeout" function described in the O'Reilly "'Developing
> Applications with Objective Caml" reference book, but not quite the same as
> either. Will someone please tell me how to do it?
>
> Steve
>
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list:
> http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
>


  reply	other threads:[~2005-12-28  9:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-28  8:44 Stephen Brackin
2005-12-28  9:36 ` Jonathan Roewen [this message]
2005-12-28  9:53 ` [Caml-list] " Florian Weimer
2005-12-28 18:34 ` Xavier Leroy

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=ad8cfe7e0512280136t18283b25w8fb0eabeb241d003@mail.gmail.com \
    --to=jonathan.roewen@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=stephen.brackin@verizon.net \
    /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