From: Ben Millwood <bmillwood@janestreet.com>
To: Gabriel Scherer <gabriel.scherer@gmail.com>
Cc: Goswin von Brederlow <goswin-v-b@web.de>,
Malcolm Matalka <mmatalka@gmail.com>,
Nicolas Ojeda Bar <n.oje.bar@gmail.com>,
caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Destructive use of file descriptors
Date: Tue, 11 Aug 2015 12:32:57 +0100 [thread overview]
Message-ID: <CA+MHO51wWOTRvPgrGhJ4j9hWd0VqPCpbA_Z-PTqp22Ov6d8oHw@mail.gmail.com> (raw)
In-Reply-To: <CAPFanBHxap7pfGhAVyQ6BFW0=-sx0zjWx+-OVpz=O1ZQUcSGhw@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 3907 bytes --]
See also the following thread last year, "Not letting channels escape",
which (after some incomplete attempts from me) illustrates a fully safe
with_file function.
https://sympa.inria.fr/sympa/arc/caml-list/2014-08/msg00024.html
On 11 August 2015 at 12:14, Gabriel Scherer <gabriel.scherer@gmail.com>
wrote:
> > 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
> <goswin-v-b@web.de> wrote:
> > On Thu, Aug 06, 2015 at 07:50:38PM +0000, Malcolm Matalka wrote:
> >> Nicolas Ojeda Bar <n.oje.bar@gmail.com> 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
>
> --
> 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
>
[-- Attachment #2: Type: text/html, Size: 6184 bytes --]
next prev parent reply other threads:[~2015-08-11 11:33 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-06 9:40 Goswin von Brederlow
2015-08-06 9:48 ` Nicolas Ojeda Bar
2015-08-06 19:50 ` Malcolm Matalka
2015-08-11 9:36 ` Goswin von Brederlow
2015-08-11 11:14 ` Gabriel Scherer
2015-08-11 11:32 ` Ben Millwood [this message]
2015-08-06 9:48 ` Gabriel Scherer
2015-08-06 9:49 ` Pierrick Couderc
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CA+MHO51wWOTRvPgrGhJ4j9hWd0VqPCpbA_Z-PTqp22Ov6d8oHw@mail.gmail.com \
--to=bmillwood@janestreet.com \
--cc=caml-list@inria.fr \
--cc=gabriel.scherer@gmail.com \
--cc=goswin-v-b@web.de \
--cc=mmatalka@gmail.com \
--cc=n.oje.bar@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox