From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.4 required=5.0 tests=AWL,NO_REAL_NAME,SPF_FAIL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id 12428BC69 for ; Thu, 4 Oct 2007 04:18:45 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAHLqA0fAXQImh2dsb2JhbACOOAEBCQon X-IronPort-AV: E=Sophos;i="4.21,226,1188770400"; d="scan'208";a="3724877" Received: from discorde.inria.fr ([192.93.2.38]) by mail3-smtp-sop.national.inria.fr with ESMTP; 04 Oct 2007 04:18:44 +0200 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l942Iig0025407 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Thu, 4 Oct 2007 04:18:44 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAHLqA0fAEKcgh2dsb2JhbACOOAEBCQon X-IronPort-AV: E=Sophos;i="4.21,226,1188770400"; d="scan'208";a="2104628" Received: from selenite.metnet.navy.mil ([192.16.167.32]) by mail1-smtp-roc.national.inria.fr with ESMTP; 04 Oct 2007 04:18:43 +0200 Received: (qmail 30692 invoked by uid 107); 4 Oct 2007 02:18:41 -0000 Received: from unknown (HELO Adric.metnet.fnmoc.navy.mil) (10.100.105.152) by selenite.metnet.navy.mil with SMTP; 4 Oct 2007 02:18:41 -0000 Received: by Adric.metnet.fnmoc.navy.mil (Postfix, from userid 760) id 96554A99F; Wed, 3 Oct 2007 19:16:49 -0700 (PDT) To: kirillkh@gmail.com Cc: caml-list@inria.fr In-reply-to: Subject: Re: Locally-polymorphic exceptions [was: folding over a file] Reply-To: oleg@pobox.com Errors-To: oleg@okmij.org From: oleg@pobox.com Message-Id: <20071004021649.96554A99F@Adric.metnet.fnmoc.navy.mil> Date: Wed, 3 Oct 2007 19:16:49 -0700 (PDT) X-Miltered: at discorde with ID 47044D84.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; oleg:01 ocamlopt:01 channel-:01 'a-:01 'b-:01 prev:01 val:01 prev:01 val:01 failwith:01 iteration:01 recursive:01 mutable:01 delimited:01 delimited:01 > Could you be more specific regarding the continuations' performance impact? > Would it matter in practice? Would you recommend using this function in a > general-purpose library instead of the imperative-style implementation that > was suggested? For use in practice (including ocamlopt -- the case delimcc does not yet support: I should really fix that) one should probably `inline' the implementation of abort into the code of fold_file. The result will _literally_ be the following: exception Done (* could be hidden in a module *) let fold_file (file: in_channel) (read_func: in_channel->'a) (elem_func: 'a->'b->'b) (seed: 'b) = let result = ref None in let rec loop prev_val = let input = try read_func file with End_of_file -> result := Some prev_val; raise Done in let combined_val = elem_func input prev_val in loop combined_val in try loop seed with Done -> (match !result with Some x -> x | _ -> failwith "impossible!") ;; (* val fold_file : in_channel -> (in_channel -> 'a) -> ('a -> 'b -> 'b) -> 'b -> 'b = *) let line_count filename = let f = open_in filename in let counter _ count = count + 1 in fold_file f input_line counter 0;; (* val line_count : string -> int = *) let test = line_count "/etc/motd";; (* val test : int = 24 *) It should be noted the differences from the previous imperative solutions: the reference cell result is written only once and read only once in the whole folding -- namely, at the very end. The reference cell is written to, and then immediately read from. The bulk of the iteration is functional and tail recursive. The use of mutable cell is the trick behind typing of multi-prompt delimited continuations. One may even say that if a type system supports reference cells, it shall support multi-prompt delimited continuations -- *and vice versa*. > Also, is there a good manual on delimited continuations for a beginner with > minimum of external references? Perhaps the most comprehensive and self-contained paper on delimited continuations is A static simulation of dynamic delimited control by Chung-chieh Shan http://www.cs.rutgers.edu/~ccshan/recur/recur-hosc-final.pdf I have heard some people have found the introduction section of http://okmij.org/ftp/Computation/Continuations.html#context-OS helpful. Please note Christopher Strachey's quote on the above page. It was said back in 1974! Here's another quote from the same Strachey and Wadsworth's paper: Those of us who have worked with continuations for some time have soon learned to think of them as natural and in fact often simpler than the earlier methods. Christopher Strachey and Christopher P. Wadsworth, 1974.