* Ask-if-continue wrapper?
@ 2005-12-28 8:44 Stephen Brackin
2005-12-28 9:36 ` [Caml-list] " Jonathan Roewen
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Stephen Brackin @ 2005-12-28 8:44 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 1079 bytes --]
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
[-- Attachment #2: Type: text/html, Size: 3741 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Ask-if-continue wrapper?
2005-12-28 8:44 Ask-if-continue wrapper? Stephen Brackin
@ 2005-12-28 9:36 ` Jonathan Roewen
2005-12-28 9:53 ` Florian Weimer
2005-12-28 18:34 ` Xavier Leroy
2 siblings, 0 replies; 4+ messages in thread
From: Jonathan Roewen @ 2005-12-28 9:36 UTC (permalink / raw)
To: Stephen Brackin; +Cc: caml-list
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
>
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Ask-if-continue wrapper?
2005-12-28 8:44 Ask-if-continue wrapper? Stephen Brackin
2005-12-28 9:36 ` [Caml-list] " Jonathan Roewen
@ 2005-12-28 9:53 ` Florian Weimer
2005-12-28 18:34 ` Xavier Leroy
2 siblings, 0 replies; 4+ messages in thread
From: Florian Weimer @ 2005-12-28 9:53 UTC (permalink / raw)
To: Stephen Brackin; +Cc: caml-list
* Stephen Brackin:
> 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.
Other folks call this "asynchronous transfer of control", and it is
generally thought to be a terrible mistakes once you've put it into a
language. It leads to horrible complexities and tends to make lots of
good library code incorrect.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Ask-if-continue wrapper?
2005-12-28 8:44 Ask-if-continue wrapper? Stephen Brackin
2005-12-28 9:36 ` [Caml-list] " Jonathan Roewen
2005-12-28 9:53 ` Florian Weimer
@ 2005-12-28 18:34 ` Xavier Leroy
2 siblings, 0 replies; 4+ messages in thread
From: Xavier Leroy @ 2005-12-28 18:34 UTC (permalink / raw)
To: Stephen Brackin; +Cc: caml-list
> 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.
The latter ("the computation state of ...") is not something you can
manipulate programmatically in OCaml. However, there is no need to:
Unix timer signals are sufficient to do what you want. (If you're on
Windows, there's nothing I can do for you, in this particular instance
and in general.) See below for the code.
A word of caution: if your function f performs I/O operations, be
prepared for them to fail with a EINTR Unix error. That's the Unix
(SVR4 / POSIX) way of being unhelpful...
- Xavier Leroy
----------------------------------------------------------------------
let set_timer tsecs =
ignore (Unix.setitimer Unix.ITIMER_REAL
{ Unix.it_interval = 0.0; Unix.it_value = tsecs })
exception Timeout
let handle_sigalrm signo =
print_string "Continue for how long? "; flush stdout;
let f = read_float() in
if f <= 0.0
then raise Timeout
else set_timer f
let continueq f arg tsecs defaultval =
let oldsig = Sys.signal Sys.sigalrm (Sys.Signal_handle handle_sigalrm) in
try
set_timer tsecs;
let res = f arg in
set_timer 0.0;
Sys.set_signal Sys.sigalrm oldsig;
res
with Timeout ->
Sys.set_signal Sys.sigalrm oldsig;
defaultval
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2005-12-28 18:34 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-12-28 8:44 Ask-if-continue wrapper? Stephen Brackin
2005-12-28 9:36 ` [Caml-list] " Jonathan Roewen
2005-12-28 9:53 ` Florian Weimer
2005-12-28 18:34 ` Xavier Leroy
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox