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_POST, 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 B20AFBB84 for ; Wed, 10 Dec 2008 12:15:43 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AuoBAIQzP0lIDtyfkGdsb2JhbACDHI9uPgEBAQEJCQwHEQOvCIEEjDwBAwEDgwQ X-IronPort-AV: E=Sophos;i="4.33,746,1220220000"; d="scan'208";a="18256410" Received: from fg-out-1718.google.com ([72.14.220.159]) by mail2-smtp-roc.national.inria.fr with ESMTP; 10 Dec 2008 12:15:43 +0100 Received: by fg-out-1718.google.com with SMTP id l27so188823fgb.43 for ; Wed, 10 Dec 2008 03:15:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type:content-transfer-encoding :content-disposition; bh=lOQQ8MfDcx7oho7/fb4gixeCONCliYNu6i79NjCUmWA=; b=j/H0F7lm/V3v76d2tpFkPZkRKPz5a/viGv84BZEEoBtyYiPHWVajjx5LXzNsAJhau/ z+oja2IopCOWNjjGLU86tC3+2J0GQFIjupyChER1OqEoMuWN34yDFaHVhGr1K7KPc6aa axKANJu0p8wEAmA+YMNUWLLc1zatnnFXVWuXQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type :content-transfer-encoding:content-disposition; b=k79oXCx65BzlAu54n2X3zs82B/GlO/db14C8cmy0GSQikEAK6seG7nVCRcqCNu8JHD Sg3QHuk1k6AlEeYg/7DOPJNspqjkZaIb6wHSlIVTYTq1Z7+mN/GdQqOcUOM0hWitCgb4 +7Rq/+jn5969v461q65SggtzE/WzDuh0JXnng= Received: by 10.86.4.14 with SMTP id 14mr599639fgd.27.1228907743091; Wed, 10 Dec 2008 03:15:43 -0800 (PST) Received: by 10.86.99.18 with HTTP; Wed, 10 Dec 2008 03:15:43 -0800 (PST) Message-ID: Date: Wed, 10 Dec 2008 11:15:43 +0000 From: "Conglun Yao" To: caml-list@yquem.inria.fr Subject: How to handle try .... finally properly? MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Spam: no; 0.00; ocaml:01 camlp:01 camlp:01 exn:01 exn:01 ocaml:01 .....:98 invoke:01 ideally:01 caml-list:01 functions:01 constraint:01 partially:02 argument:02 argument:02 Sorry, no replies in ocaml-beginner, post it again in caml-list. Dear all, I'm thinking about how to handle try .... finally in ocaml, and found one of camlp4 implementations from http://bluestorm.info/camlp4/dev/try/pa_tryfinally.ml.html Example code adopted from Gabriel Scherer's pa_tryfinally Example Input : let transform f x = GlMat.push(); try f x finally GlMat.pop() Output : let transform f x = (GlMat.push (); let after () = GlMat.pop () and result = try `Result (f x) with | exn -> `Exn exn in (after (); match result with | `Result v -> v | `Exn e -> raise e)) At the first glance, it answered my question. However, it solved the problem partially, only working on functions with one argument. If we feed the * transform * a function with more than one argument, (it is possible because of curring) transform (fun x y -> .... some logic staff .... ) x y will invoke the * after () * before the ((fun x y -> .....) x) y is really executed. Ideally, * transform * function is specified as allowing a function f with only one parameter. But it seems impossible in OCaml, as f : 'a -> 'b doesn't means f can and only accept one argument. ( 'b could be 'c -> 'd ) If we could constraint 'b as a non-function type, it can solve the problem. Or someone knows a better solution to work around the problem. Thanks. Conglun