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.1 required=5.0 tests=AWL,HTML_MESSAGE,SPF_NEUTRAL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 52EEBBC69 for ; Wed, 3 Oct 2007 14:19:59 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAOQkA0fAXQInemdsb2JhbACCPTSLRwEBCQo X-IronPort-AV: E=Sophos;i="4.21,224,1188770400"; d="scan'208";a="2323656" Received: from concorde.inria.fr ([192.93.2.39]) by mail2-smtp-roc.national.inria.fr with ESMTP; 03 Oct 2007 14:19:58 +0200 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id l93CJwSx019075 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Wed, 3 Oct 2007 14:19:58 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAJMlA0fRVZKxkmdsb2JhbACCPTSLRwEBAQEHBAQRFg X-IronPort-AV: E=Sophos;i="4.21,224,1188770400"; d="scan'208";a="3697214" Received: from wa-out-1112.google.com ([209.85.146.177]) by mail3-smtp-sop.national.inria.fr with ESMTP; 03 Oct 2007 14:19:57 +0200 Received: by wa-out-1112.google.com with SMTP id m34so6206239wag for ; Wed, 03 Oct 2007 05:19:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; bh=e/omy6O1h4X4poKjqbACiuaswXdv4J4CT6olmz9aTJI=; b=C2wTnbXhCSvbZP1P74PwujzJGim2lslPTlHz/lvD8JaKIlhwMAuj/GOsj+fPsAKSXernIth+UHmbY0K8CbFPBo5zFhszu9Oo3dhP6fitgiA1dRcEUEWba6G249g3HkDTFo1cHpabB2SiQBHH3ExLbuIzcBkDiZ3b6Wbu/AYJ+vo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=GeDqCRFQkqLshSTmDr0UU1naPHUw99GQjdc3nblsCHTwdXI2rQvDIFQnOL0J6aruP7i8LUCmSjB6GUNE1h5CHS3Qa20K3GqJf6zJQbfrpFO6oTnoCNH5gA6pAtZq1tP/hVvzsBUhLzi98T4rSaRYV28hbyL0kj65Eccrnm+HSsY= Received: by 10.114.156.1 with SMTP id d1mr8119764wae.1191413996493; Wed, 03 Oct 2007 05:19:56 -0700 (PDT) Received: by 10.114.25.8 with HTTP; Wed, 3 Oct 2007 05:19:56 -0700 (PDT) Message-ID: Date: Wed, 3 Oct 2007 14:19:56 +0200 From: kirillkh To: "Daniel de Rauglaudre" Subject: Re: [Caml-list] Re: Locally-polymorphic exceptions [was: folding over a file] Cc: caml-list@inria.fr In-Reply-To: <20071003114832.GB23824@yquem.inria.fr> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_23149_30635042.1191413996487" References: <20071003083529.40DA2A99F@Adric.metnet.fnmoc.navy.mil> <20071003114832.GB23824@yquem.inria.fr> X-Miltered: at concorde with ID 470388EE.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; rauglaudre:01 rauglaudre:01 oleg:01 oleg:01 channel-:01 'a-:01 'b-:01 recursive:01 invocation:01 encapsulated:01 blabla:01 prev:01 val:01 val:01 prev:01 ------=_Part_23149_30635042.1191413996487 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, 2007/10/3, Daniel de Rauglaudre : > > Hi, > > > 2007/10/3, oleg@pobox.com : > > > > exception Done of 'a > > > > let fold_file (file: in_channel) > > (read_func: in_channel->'a) > > (elem_func: 'a->'b->'b) > > (seed: 'b) = > > [...] > > Personnally, I don't like exceptions because they generally control too > much part of code. It's easy enough to localize the exception scope in this case and to see that catching it at the top recursive invocation gets rid of it for sure. Even the most dreadful beast is harmless, when encapsulated in a cage. (C) I wonder what are continuations' maintenance properties, though. Any comments? I'm not fluent in them, yet. I often practice things like: > > match try Some (...) with [ Exception -> None ] with > [ Some v -> blabla > | None -> blublu ] > > I would write your function like this: > > value fold_file (file : in_channel) (read_func : in_channel -> 'a) > (elem_func : 'a -> 'b -> 'b) (seed : 'b) > = > let rec loop prev_val = > match try Some (read_func file) with [ End_of_file -> None ] with > [ Some input -> > let combined_val = elem_func input prev_val in > loop combined_val > | None -> prev_val ] > in > loop seed > ; A similar solution has been proposed in the original discussion ("best and fastest way to read lines from a file?" thread). But then someone suggested using a second exception instead, which is better performance-wise, since it avoids variant allocations on the common code path. What I tried to do with polymorphic exception is implement a generalized channel fold combinator based on this idea. -Kirill ------=_Part_23149_30635042.1191413996487 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi,

2007/10/3, Daniel de Rauglaudre <daniel.de_rauglaudre@inria.fr>:
Hi,

> 2007/10/3, oleg@pobox.com <oleg@pobox.com>:
>
> exception Done of 'a
>
>  let fold_file (file: in_channel)
>               (read_func: in_channel->'a)
>               (elem_func: 'a->'b->'b)
>               (seed: 'b) =
> [...]

Personnally, I don't like exceptions because they generally control too
much part of code.

It's easy enough to localize the exception scope in this case and to see that catching it at the top recursive invocation gets rid of it for sure.
Even the most dreadful beast is harmless, when encapsulated in a cage. (C)

I wonder what are continuations' maintenance properties, though. Any comments? I'm not fluent in them, yet.

I often practice things like:

  match try Some (...) with [ Exception -> None ] with
  [ Some v -> blabla
  | None -> blublu ]

I would write your function like this:

  value fold_file (file : in_channel) (read_func : in_channel -> 'a)
    (elem_func : 'a -> 'b -> 'b) (seed : 'b)
  =
    let rec loop prev_val =
      match try Some (read_func file) with [ End_of_file -> None ] with
      [ Some input ->
          let combined_val = elem_func input prev_val in
          loop combined_val
      | None -> prev_val ]
    in
    loop seed
  ;

A similar solution has been proposed in the original discussion ("best and fastest way to read lines from a file?" thread). But then someone suggested using a second exception instead, which is better performance-wise, since it avoids variant allocations on the common code path. What I tried to do with polymorphic exception is implement a generalized channel fold combinator based on this idea.

-Kirill
------=_Part_23149_30635042.1191413996487--