Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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 <> 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
      (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
    ignore(exit 0)
  callcc save_state
  Saves the "state" the application is in as an base64 encoded marshalled
let _ =
  if (Sys.file_exists "state") then
    let res =
      (let pipe = new Netencoding.Base64.decoding_pipe() in
         (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
         )) in
    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).

> --
> regards,                   (o_
>  Thomas Fischbacher -  //\
> (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:
> Archives:
> Beginner's list:
> Bug reports: