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,DNS_FROM_RFC_ABUSE, 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 discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id D9416BC69 for ; Sat, 21 Oct 2006 17:03:27 +0200 (CEST) Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.187]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id k9LF3RVX002208 for ; Sat, 21 Oct 2006 17:03:27 +0200 Received: by nf-out-0910.google.com with SMTP id y38so1764910nfb for ; Sat, 21 Oct 2006 08:03:25 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=sO7AtYfKwQriFbUIUzPs3AJOe0QT/cHvdksfDMUVVnhKESdXLOzT0okDmIUzJ5ghgkufPOBj7U8mz0nxYhNvOU//B8632hkzkzcXHwPnHmxoX3bzU1zb7BvnATChavEf/KHsjPSG4Y9+34And5u4gBrm925scmB3DxD9eDzvIVo= Received: by 10.48.254.10 with SMTP id b10mr2662852nfi; Sat, 21 Oct 2006 08:03:25 -0700 (PDT) Received: by 10.49.7.2 with HTTP; Sat, 21 Oct 2006 08:03:25 -0700 (PDT) Message-ID: <875c7e070610210803i5abf3b78j6e83c55c991f05cf@mail.gmail.com> Date: Sat, 21 Oct 2006 11:03:25 -0400 From: "Chris King" To: "Christophe Raffalli" Subject: Re: [Caml-list] try and tail call Cc: caml-list@inria.fr In-Reply-To: <453A27EB.8070609@univ-savoie.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <453A27EB.8070609@univ-savoie.fr> X-j-chkmail-Score: MSGID : 453A36BF.000 on discorde : j-chkmail score : X : 0/20 1 X-Miltered: at discorde with ID 453A36BF.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; christophe:01 raffalli:01 raffalli:01 univ-savoie:01 ocamlopt:01 statically:01 syntax:01 usefull:01 bug:01 avoided:01 syntax:01 camlp:01 recursive:01 wrote:01 encode:01 On 10/21/06, Christophe Raffalli wrote: > > consider this piece of code: > > (try > let b = f a in > (fun () -> g b) > with > (fun () -> h a)) () > > > Is the call to "g" a tail call and is it omptimized as such ? Yes, it is, at least in the assembly code output by ocamlopt. (Neat trick, btw.) > If not, how to encode a feature like this one: > > try > ... > end > ... (* the exception raised here are not handles by with but > propagated *) > with > ... What's typically used is something along the lines of: match (try Some (f a) with Not_found -> None) with | Some b -> g b | None -> h a though I wouldn't be surprised if the method you used above is slightly faster since it avoids creating unnecessary option objects (the anonymous functions are statically allocated). > By the way I think this "try ... [end ...] with ..." syntax is usefull > anyway because you have often a bug when > you write > > try > let b = f a in (* you know this may raise Not_found *) > g b (* you assume wrongly that this can not raise Not_found *) > with > Not_found -> h a > > The unwanted capture of Not_found could be avoided with try ... end ... > with ... Many people agree with you... I find almost always that I want something like the syntax you describe above, rather than what Caml provides natively. Martin Jambon provides an example syntax extension in his Camlp4 tutorial[1] which allows the use of "let-try-in-with" blocks: let b = try f a in g b with Not_found -> h a Here, only exceptions raised by "f a" are caught by the try block, and "g b" occurs in a recursive context. Hope this helps, Chris [1] http://martin.jambon.free.fr/extend-ocaml-syntax.html#lettry