caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Continuations
@ 2002-12-10 18:49 Ceri Storey
  2002-12-11  1:43 ` [Caml-list] Site down Eric Merritt
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Ceri Storey @ 2002-12-10 18:49 UTC (permalink / raw)
  To: caml-list

I was just wondering if there any possibility of there being support
for continuations in a future version of ocaml?

They'd be useful in all sorts of situations, eg: restarable network
protocol parsers, or co-routines.
-- 
Ceri Storey <cez@compsoc.man.ac.uk>
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Caml-list] Site down
  2002-12-10 18:49 [Caml-list] Continuations Ceri Storey
@ 2002-12-11  1:43 ` Eric Merritt
  2002-12-11  9:19 ` [Caml-list] Continuations Christophe Raffalli
  2002-12-12  5:51 ` [Caml-list] Continuations Michaël Grünewald
  2 siblings, 0 replies; 5+ messages in thread
From: Eric Merritt @ 2002-12-11  1:43 UTC (permalink / raw)
  To: caml-list

Everyone,

It seems the ocaml (www.ocaml.org) site is down. Does
anyone have any clue as to when it will come back up?

Thanks,
Eric

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Continuations
  2002-12-10 18:49 [Caml-list] Continuations Ceri Storey
  2002-12-11  1:43 ` [Caml-list] Site down Eric Merritt
@ 2002-12-11  9:19 ` Christophe Raffalli
  2002-12-12  5:51 ` [Caml-list] Continuations Michaël Grünewald
  2 siblings, 0 replies; 5+ messages in thread
From: Christophe Raffalli @ 2002-12-11  9:19 UTC (permalink / raw)
  To: Ceri Storey; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1812 bytes --]

Ceri Storey wrote:
> I was just wondering if there any possibility of there being support
> for continuations in a future version of ocaml?
> 
> They'd be useful in all sorts of situations, eg: restarable network
> protocol parsers, or co-routines.

Continuations are nice ... but they need to save all the stack. This can
be done in a reasonable way by saving only one page (or two if the first 
one is almost empty) of the stack and marking the other pages as 
unwritable and saving the next pages only when needed but:

- This is not portable to all platforms (windows may be OK ?).
- Intensive use of continuations are still time consuming.
- Saving all the stack leads to important memory leaks because in 
general only some of the information in the stack are necessary to call 
the continuation and the other leads to useless pointer kept for the GC.

It is in general better to implement yourself the bactracking you need 
by keeping a minimal record containing the information you need to 
backtrack and adding one argument (with such a list of records) to all 
the functions that may trigger backtracking.

But, still, if you program well, and you know about the possible memory 
leaks, you can program with continuations and it is a pity they are not 
there in OCaml :-( Especialy for those (like me) who extract programs 
from classical proofs :-)

-- 
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
---------------------------------------------
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature
---------------------------------------------

[-- Attachment #2: Type: application/pgp-signature, Size: 252 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Caml-list] Re: Continuations
  2002-12-10 18:49 [Caml-list] Continuations Ceri Storey
  2002-12-11  1:43 ` [Caml-list] Site down Eric Merritt
  2002-12-11  9:19 ` [Caml-list] Continuations Christophe Raffalli
@ 2002-12-12  5:51 ` Michaël Grünewald
  2002-12-15 17:06   ` [Caml-list] Continuations: an implementation Christophe Raffalli
  2 siblings, 1 reply; 5+ messages in thread
From: Michaël Grünewald @ 2002-12-12  5:51 UTC (permalink / raw)
  To: caml-list

Ceri Storey <cez@mrtall.compsoc.man.ac.uk> writes:

> I was just wondering if there any possibility of there being support
> for continuations in a future version of ocaml?

Are not they possible ? If i remember well (i did not check it back), the
following sample do what is called `Programming with rupture and
continuation' (i learned it from J. Chazarin "Programmer avec Scheme").

Help me: just after the "resoud_probleme" definition, you can spot ";;", the
i can not achieve to remove (i got a syntax error, then).

---- BEGIN CODE ----
type strategie = Abandonne | Ignore | Remplace of int

exception Rupture of (strategie -> int)

let resoud_probleme l =

    let rec continue ax togo strategy =
	match strategy with
	    | Abandonne -> 	ax
	    | Ignore -> 	resoud ax (List.tl togo)
	    | Remplace x -> 	resoud ax (x::(List.tl togo))

    and resoud ax l = 
	match l with 
	    | [] ->	ax
	    | 0::tl -> 	raise (Rupture (continue ax l))
	    | hd::tl -> resoud (ax + hd) tl

    in resoud 0 l
;;

let process_problem l s =
    try  resoud_probleme l
    with Rupture closure -> closure s
    	
in
let strategie_to_string s = 
    match s with
	| Abandonne -> "Abandonne"
	| Ignore -> "Ignore"
	| Remplace x -> Printf.sprintf "Remplace par %d" x

in
let monitor_problem l s =
    Printf.printf "Solution: %d (%s)\n" (process_problem l s) 
    (strategie_to_string s)

in

let main () = 
    let pblm = [10; 23; 33; 0; 12] in
	List.iter (monitor_problem pblm) [Abandonne; Ignore; 
					  Remplace 1; Remplace 12];
	exit 0

in main()
---- END CODE ----

This code is in the awful French/English mix i write when programming, there
is a little lexicon by the end of this message.

Maybe this is not what you call Rupture/Cotninuation (you did not speak of
rupture), but after my memories, this is.

Code explanation: `resoud_problem' must process an horrific and very looong
calculous (the sum of 5 or 6 integers from a list), but take the occurence
of '0' as a critical situation where the calculous cannot be resumed in a
consistent way, it asks the users which Strategy to apply (give up, ignore
critical value, correct the value).

Lexicon :
Abandonne : give up
Ignore : ignore (it could have been 'remove')
Remplace : replace

-- 
Michaël Grünewald <michael-grunewald@wanadoo.fr>  - RSA PGP Key ID: 0x20D90C12
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Caml-list] Continuations: an implementation
  2002-12-12  5:51 ` [Caml-list] Continuations Michaël Grünewald
@ 2002-12-15 17:06   ` Christophe Raffalli
  0 siblings, 0 replies; 5+ messages in thread
From: Christophe Raffalli @ 2002-12-15 17:06 UTC (permalink / raw)
  Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 2826 bytes --]

Here is an implementation of callcc for OCaml using fork.

The only problems are:
- I did not test it (but it should work)
- The limited number of fork calls

Note: the implementation of fork on Linux, makes a minimum of page copying, so
it should be reasonably fast

-- callcc.mli --
(* callcc fn: standard callcc, uses exit code 3 for internal use. *)

val callcc : (('a -> 'b) -> 'a) -> 'a
----------------

-- callcc.ml ---
open Unix
open Sys

let all_signals = [
   sigabrt;   (* Abnormal termination *)
   sigalrm;   (* Timeout *)
   sigfpe;    (* Arithmetic exception *)
   sighup;    (* Hangup on controlling terminal *)
   sigill;    (* In id hardware instruction *)
   sigint;    (* Interactive interrupt (ctrl-C) *)
   sigkill;   (* Termination (cannot be ignored) *)
   sigpipe;   (* Broken pipe *)
   sigquit;   (* Interactive termination *)
   sigsegv;   (* In id memory reference *)
   sigterm;   (* Termination *)
   sigusr1;   (* Application-defined signal 1 *)
   sigusr2;   (* Application-defined signal 2 *)
(*  sigchld;       Child process terminated, do not ignore that one !*)
   sigcont;   (* Continue *)
   sigstop;   (* Stop *)
   sigtstp;   (* Interactive stop *)
   sigttin;   (* Terminal read from background process *)
   sigttou;   (* Terminal write from background process *)
   sigvtalrm; (* Timeout in virtual time *)
   sigprof   (* Profiling interrupt *)
]

let ignore_all_signals () =
   let previous = List.map (fun s -> try signal s Signal_ignore with Sys_error 
_ -> Signal_ignore) all_signals in
   fun () ->
     List.iter2 (fun s b -> try set_signal s b with Sys_error _ -> ()) 
all_signals previous


let callcc (fn : ('a -> 'b) -> 'a) =
   let filename = Filename.temp_file "callcc" "val" in
   let gn (x : 'a) =
     let ch = open_out filename in
     Marshal.to_channel ch x [Marshal.Closures];
     close_out ch;
     exit 3
   in
   let restore_signals = ignore_all_signals () in
   let pid = fork () in
   if pid = 0 then begin
     restore_signals ();
     fn gn
   end else
     let rec kn pid =
       match waitpid [] pid with
	_, WEXITED 3 ->
	  let ch = open_in filename in
	  let r = Marshal.from_channel ch in
	  close_in ch;
	  remove filename;
	  let pid = fork () in
	  if pid = 0 then (restore_signals (); r) else kn pid
       | _, WEXITED n -> exit n
       | _, _ -> exit 1
     in kn pid
----------------




-- 
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
---------------------------------------------
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature
---------------------------------------------

[-- Attachment #2: Type: application/pgp-signature, Size: 252 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-12-15 21:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-10 18:49 [Caml-list] Continuations Ceri Storey
2002-12-11  1:43 ` [Caml-list] Site down Eric Merritt
2002-12-11  9:19 ` [Caml-list] Continuations Christophe Raffalli
2002-12-12  5:51 ` [Caml-list] Continuations Michaël Grünewald
2002-12-15 17:06   ` [Caml-list] Continuations: an implementation Christophe Raffalli

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).