From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 8973F7EEF8 for ; Tue, 11 Aug 2015 13:15:12 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of gabriel.scherer@gmail.com) identity=pra; client-ip=209.85.223.176; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of gabriel.scherer@gmail.com designates 209.85.223.176 as permitted sender) identity=mailfrom; client-ip=209.85.223.176; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="gabriel.scherer@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-io0-f176.google.com) identity=helo; client-ip=209.85.223.176; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="gabriel.scherer@gmail.com"; x-sender="postmaster@mail-io0-f176.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0BrAQAp2MlVlLDfVdFdg29pBoMeqg+SMYV5AoEwB0wBAQEBAQESAQEBAQcLCwkfMIQjAQEBAwESEQQZARsSCwEDAQsGBQsNAgIJHQICIQEBEQEFAQoSBhMSEId2AQMKCA2qeoEvPjGLQIFsgnmLJgoZJwMKFUKEbwEBAQEBBQEBAQEBFwEFDoEUii+CTx6CHAeCaYFDBYVpDI8bhQSFeoFsgg+QSIVnEiOBFxeEDzwzAYJLAQEB X-IPAS-Result: A0BrAQAp2MlVlLDfVdFdg29pBoMeqg+SMYV5AoEwB0wBAQEBAQESAQEBAQcLCwkfMIQjAQEBAwESEQQZARsSCwEDAQsGBQsNAgIJHQICIQEBEQEFAQoSBhMSEId2AQMKCA2qeoEvPjGLQIFsgnmLJgoZJwMKFUKEbwEBAQEBBQEBAQEBFwEFDoEUii+CTx6CHAeCaYFDBYVpDI8bhQSFeoFsgg+QSIVnEiOBFxeEDzwzAYJLAQEB X-IronPort-AV: E=Sophos;i="5.15,652,1432591200"; d="scan'208";a="173278451" Received: from mail-io0-f176.google.com ([209.85.223.176]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 11 Aug 2015 13:15:11 +0200 Received: by ioeg141 with SMTP id g141so198185747ioe.3 for ; Tue, 11 Aug 2015 04:15:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=5lzwlVesEL0JFpwcIfBUUArePHwi7QXh2Z+ZjQn5+lY=; b=Ttf4cy6DJWgopF6sC51198NA5dLbzC7dxuWvNQSY0SHj08vN3mz/VKL6Q5NtpibiVy xdg7E84GQvG5QVUkjngWuJ12dMdilB2KJXlVIvnMtcgF/XBmfW/T0VHn+mP/rz1jrLkr 3mqf/R7PlbM/kyQORVBUpyOFIjRi1o2FwpGguWVb4xnIweIOKdUN+zG5UoiKIAGJNaSC 1STrYmXR6JVFkKap5fScfGXVzyvrTjL0mqnwMUsIVaF4iTZvWOwp41BliIsYp5vJEig2 YwIvy7WpgwnxvzLm3zueNTWCRAZRkUZiDysgEmod33MuVMXihBDjn6I2u3Z2n1D8SBPe krew== X-Received: by 10.107.13.201 with SMTP id 192mr27441651ion.70.1439291710513; Tue, 11 Aug 2015 04:15:10 -0700 (PDT) MIME-Version: 1.0 Received: by 10.79.68.132 with HTTP; Tue, 11 Aug 2015 04:14:31 -0700 (PDT) In-Reply-To: <20150811093625.GA5949@frosties> References: <20150806094030.GE16477@frosties> <864mkcci29.fsf@gmail.com> <20150811093625.GA5949@frosties> From: Gabriel Scherer Date: Tue, 11 Aug 2015 13:14:31 +0200 Message-ID: To: Goswin von Brederlow Cc: Malcolm Matalka , Nicolas Ojeda Bar , caml users Content-Type: text/plain; charset=UTF-8 Subject: Re: [Caml-list] Destructive use of file descriptors > Yeah. I was thinking that that might be avoided. Something on the line > of putting the file descriptor into a local module so ocaml would > complain about it escaping its scope or something. You can use the "ST monad" trick to track escape of values or effects: "Region-based resource management", Oleg http://okmij.org/ftp/Haskell/regions.html However, this requires writing the resource-manipulating operations in monadic style. module ST : sig type ('s, 'a) st val return : 'a -> ('s, 'a) st val bind : ('a -> ('s, 'b) st) -> ('s, 'a) st -> ('s, 'b) st type ('s, 'a) st_ref val st_ref : 'a -> ('s, ('s, 'a) st_ref) st val get : ('s, 'a) st_ref -> ('s, 'a) st val set : ('s, 'a) st_ref -> 'a -> ('s, unit) st type 'a secretive = { run : 's . unit -> ('s, 'a) st } val run_st : 'a secretive -> 'a end = struct type ('s, 'a) st = 'a let return x = x let bind f m = f m type ('s, 'a) st_ref = 'a ref let st_ref, get, set = ref, (!), (:=) type 'a secretive = { run : 's . unit -> ('s, 'a) st } let run_st st = st.run () end let (>>=) m f = ST.bind f m # let test = ST.run_st { ST.run = fun () -> ST.st_ref 1 >>= fun r -> ST.get r >>= fun before -> ST.set r 2 >>= fun () -> ST.get r >>= fun after -> ST.return (before + after) };; val test : int = 3 # let test_escape_1 = ST.run_st { ST.run = fun () -> ST.st_ref 1 >>= fun r -> ST.return r };; Error: This field value has type unit -> ('a, ('a, int) ST.st_ref) ST.st which is less general than 'b. unit -> ('b, 'c) ST.st # let test_escape_2 = let hole = ref None in ST.run_st { ST.run = fun () -> ST.st_ref 1 >>= fun r -> hole := Some r; ST.return () }; !hole;; Error: This field value has type unit -> ('a, unit) ST.st which is less general than 'b. unit -> ('b, 'c) ST.st On Tue, Aug 11, 2015 at 11:36 AM, Goswin von Brederlow wrote: > On Thu, Aug 06, 2015 at 07:50:38PM +0000, Malcolm Matalka wrote: >> Nicolas Ojeda Bar writes: >> >> > Hi, >> > >> > No magic needed in OCaml: >> > >> > let with_open path f = >> > let oc = open_out path in >> > match f oc with >> > | x -> close_out_noerr oc; x >> > | exception e -> close_out_noerr oc; raise e >> > >> > and then use it as >> > >> > with_open "foo" (fun oc -> output_string oc str) >> > >> > Cheers >> > >> > Nicolas >> > >> >> The weakness with this (which I'm not sure if Python offers protection) >> is returning something that references the file created. It can lead to >> some confusing error messages. > > Yeah. I was thinking that that might be avoided. Something on the line > of putting the file descriptor into a local module so ocaml would > complain about it escaping its scope or something. > > At least unlike the other suggestion this one closes the FD on exception. > > MfG > Goswin > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs