Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] recursive or loop
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Till Varoquaux <till.varoquaux@g...>
Subject: Re: [Caml-list] recursive or loop
On 3/8/06, Thomas Fischbacher <Thomas.Fischbacher@physik.uni-muenchen.de> wrote:
>
> On Wed, 8 Mar 2006, Matthew O'Connor wrote:
>
> > > No it does not. But never mind. It would not help either.
> >
> > Doesn't CPS enable you to basically keep the entire call stack (or
> > equivalent) in the heap?
>
> Yes, it does.
>
> >  Can you explain why it wouldn't help?  Thanks.
>
> You still would have to write out a closure if you want to write all the
> state to disk.

I beleive specifying Marshal.Closures when marshalling should do the trick...

Another way to get along with suspending resuming is by marshalling a
continuation. You can use Xavier Leroy's callcc library and see if
this works out you (just call suspend when you went to exit with a
save point):

open Callcc
open Netchannels
open Marshal

let suspend ()=
  let save_state k=
    let pipe = new Netencoding.Base64.encoding_pipe() in
    with_out_obj_channel
      (new output_channel (open_out "state"))
      (fun ch ->
         let ch' = new output_filter pipe ch in
         ch' # output_string(to_string k [Closures]);
         ch' # close_out()
      );
    (* Skips a warning because this function does not return (it is an exit
       point)*)
    ignore(exit 0)
  in
  callcc save_state
(*
  Saves the "state" the application is in as an base64 encoded marshalled
  continuation.
*)
let _ =
  if (Sys.file_exists "state") then
    let res =
      (let pipe = new Netencoding.Base64.decoding_pipe() in
       with_in_obj_channel
         (new input_channel (open_in "state"))
         (fun ch ->
            let ch' = new input_filter ch pipe in
            let s = string_of_in_obj_channel ch' in
            s
         )) in
    Sys.remove("state");
    let k=Marshal.from_string res 0 in
      throw k ()

Although this has worked callcc is a proof of concept so be ready to
have some nasty bugs... (I've had weird segfaults for instance).
Cheers,
Till

> --
> regards,               tf@cip.physik.uni-muenchen.de              (o_
>  Thomas Fischbacher -  http://www.cip.physik.uni-muenchen.de/~tf  //\
> (lambda (n) ((lambda (p q r) (p p q r)) (lambda (g x y)           V_/_
> (if (= x 0) y (g g (- x 1) (* x y)))) n 1))                  (Debian GNU)
>
> _______________________________________________
> 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
>