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 A3D897F747 for ; Fri, 8 Aug 2014 21:46:10 +0200 (CEST) Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of frederic.bour@lakaban.net) identity=pra; client-ip=94.23.239.155; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="frederic.bour@lakaban.net"; x-sender="frederic.bour@lakaban.net"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of frederic.bour@lakaban.net designates 94.23.239.155 as permitted sender) identity=mailfrom; client-ip=94.23.239.155; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="frederic.bour@lakaban.net"; x-sender="frederic.bour@lakaban.net"; 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.lakaban.net) identity=helo; client-ip=94.23.239.155; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="frederic.bour@lakaban.net"; x-sender="postmaster@mail.lakaban.net"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AsgaABUo5VNeF++b/2dsb2JhbABagmItAU9Xgi8uGoJgqyuELpVdgWOHSAGBK3eEAwEBBAEMDgkEGQEBExkLAQQLCQIYDR0CAiEkEgYTEgmIEwMJDAmTTJs9d4UCAQWLBAMKhWARBo0fgUVkBAeCeYFShQUFjQyDKoRlggmBVoVQhyADhi+BRoJ9 X-IPAS-Result: AsgaABUo5VNeF++b/2dsb2JhbABagmItAU9Xgi8uGoJgqyuELpVdgWOHSAGBK3eEAwEBBAEMDgkEGQEBExkLAQQLCQIYDR0CAiEkEgYTEgmIEwMJDAmTTJs9d4UCAQWLBAMKhWARBo0fgUVkBAeCeYFShQUFjQyDKoRlggmBVoVQhyADhi+BRoJ9 X-IronPort-AV: E=Sophos;i="5.01,827,1400018400"; d="scan'208";a="88760392" Received: from lakaban.net (HELO mail.lakaban.net) ([94.23.239.155]) by mail2-smtp-roc.national.inria.fr with ESMTP; 08 Aug 2014 21:46:09 +0200 Received: from [192.168.0.31] (cpc27-cmbg15-2-0-cust159.5-4.cable.virginm.net [86.27.188.160]) (Authenticated sender: defre@ygg-drasil.fr) by mail.lakaban.net (Postfix) with ESMTPSA id 254141200019; Fri, 8 Aug 2014 21:42:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=lakaban.net; s=default; t=1407526959; bh=ztG9P/pebVRNajBZscObMai/KJ8Mk6N2C4ZlHeISv50=; h=Date:From:Subject:To:Cc:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=WY0nK5Lg8z3fJjFqby1nMet8pUBeh098JApH29ry3lhLl7geMDGViev9knL6SORNY HeI6kwzsJmM+X2PeOJY0SSR5rZBfljr4cUYKNvA4Jp1its9uR4WQY72WBaKIPrI4pl Bv0fzNur+aWchYf9kznDz5LSLunDxhMv/u7qPhxM= Date: Fri, 08 Aug 2014 19:46:23 +0001 From: =?iso-8859-1?b?RnLpZOlyaWM=?= Bour To: Markus Mottl Cc: Gabriel Scherer , Ben Millwood , Philippe Veber , caml users Message-Id: <1407527123.1213.3@mail.lakaban.net> In-Reply-To: References: <1407522483.1213.2@mail.lakaban.net> X-Mailer: geary/0.6.1 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=-bSt8KoiiMdwfeuvMGcOu" Subject: Re: [Caml-list] Not letting channels escape. --=-bSt8KoiiMdwfeuvMGcOu Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable But you can't do any meaningful computation with this value. It's just a blackbox. In your example, the (ST.input_line c) inside the closure just build a=20 value of type (string option, '_s) t for a specific s. In particular you will never be able to execute any action on the input=20 channel. This is not the case with the proposed implementation, because effects=20 are executed immediately. To ensure isolation, all effects should be "frozen": module ST : sig type ('a, 's) t val return : 'a -> ('a, 's) t val bind : ('a, 's) t -> ('a -> ('b, 's) t) -> ('b, 's) t type 's chan type 'a f =3D { f : 's . 's chan -> ('a, 's) t } val with_file : string -> f:'a f -> 'a val input_line : 's chan -> (string, 's) t end =3D struct type ('a, 's) t =3D unit -> 'a let return x =3D fun () -> x let bind x f =3D fun () -> f (x ()) () type 's chan =3D in_channel type 'a f =3D { f : 's . 's chan -> ('a, 's) t } let with_file fp ~f:{ f } =3D let ic =3D open_in fp in try let result =3D f ic () in close_in_noerr ic; result with exn -> close_in_noerr ic; raise exn let input_line c =3D fun () -> input_line c end Le ven. 8 ao=C3=BBt 2014 =C3=A0 20:30, Markus Mottl a=20 =C3=A9crit : > The escaping value can still be manipulated through a closure, outside > of "with_file". The goal was to prevent this. >=20 > On Fri, Aug 8, 2014 at 2:28 PM, Fr=C3=A9d=C3=A9ric Bour=20 > wrote: >> ST.input_line is just a reified effect, it can't be executed=20 >> outside of the >> ST monad. >> You can make the value escape, but you can't do anything with it. >>=20 >> And because of the existential variable being propagated, it can't=20 >> be >> executed outside of this run of the ST value. >>=20 >> Le ven. 8 ao=C3=BBt 2014 =C3=A0 19:23, Markus Mottl=20 >> a =C3=A9crit : >>=20 >> How would you implement this safely with ST here? I wasn't using the >> standard input_line but "ST.input_line", which already returns a=20 >> monadic >> type. The trick here was to use the monadic "return" to return a=20 >> closure >> that captures the existential variable, allowing me to execute the >> computation outside of the safe region. Regards, Markus On Fri, Aug=20 >> 8, 2014 >> at 1:37 PM, Gabriel Scherer wrote: >>=20 >> The ST trick only works when all primitives affecting resource are=20 >> in the >> monadic abstraction (they mention the ST region variable in their >> computation type). This is not the case in Markus example as=20 >> "input_line" is >> a non-typed effect. Using ST safely would be possible in OCaml, but=20 >> you >> would have to completely eschew the standard library and use a=20 >> different >> base where all effectful functions have a monadic type. It is the=20 >> library, >> not the language itself, that allows this. On the contrary, linear=20 >> types are >> distinctly a language feature. Using monads to encapsulate a form of >> linearity is an old trick. If you want to have a taste of built-in=20 >> linear >> typing, you may want to give Mezzo a try (=20 >> http://protz.github.io/mezzo/ ). >> On Fri, Aug 8, 2014 at 7:21 PM, Markus Mottl=20 >> wrote: >>=20 >> I see, I was replying to the "reference problem" and hadn't read=20 >> your >> implementation, which, besides existentials, already requires=20 >> monads as >> return values. Actually, it just occurred to me that one can even=20 >> break the >> monadic approach in a purely functional way. You are just one=20 >> "return" away >> from disaster: let f =3D ST.with_file "foo.txt" ~f:{ ST.f =3D fun c ->= =20 >> ST.return >> (fun () -> ignore (ST.input_line c)) } in f () You'd have to=20 >> eliminate >> "return", in which case it wouldn't be a monad anymore and not=20 >> general >> enough for realistic uses of "with_file". Regards, Markus On Fri,=20 >> Aug 8, >> 2014 at 12:01 PM, Ben Millwood wrote: > I >> protected against that in my module by carrying the existential=20 >> type > >> variable in the result of input_line as well, because I stumbled=20 >> into > >> exactly that example while originally drafting my e-mail :) > > In=20 >> a sense >> I'm reinventing monadic IO but in a bit of a half-hearted > way. It=20 >> > >> wouldn't take much work to make it a bit more fully-hearted, but it=20 >> > would >>> still be inconvenient to actually use. > > > On 8 August 2014=20 >>> 16:44, >> Markus Mottl wrote: >> >> It doesn't even=20 >> require >> references to screw things up here. Just >> return the closure=20 >> containing >> the channel from within "f": >> >> In_channel.with_file "foo.txt"=20 >> ~f:(fun ic >> () -> input_line ic) >> |> fun f -> f () >> >> The initial=20 >> Stream-example is >> basically just an instance of this >> "returning a closure"=20 >> problem. >> >> >> But the availability of references and exceptions arguably makes >>=20 >> things >> worse, because you cannot even use monadic I/O + existential >>=20 >> types to >> achieve guaranteed safety. >> >> Regards, >> Markus >> >> On Fri,=20 >> Aug 8, >> 2014 at 10:49 AM, Ben Millwood >> >>=20 >> wrote: >> > >> It's been pointed out to me that the above certainly isn't=20 >> perfectly >> > >> secure. >> > E.g. >> > >> > let f =3D ref (fun () -> ()) in >> >=20 >> with_file >> "safe.ml" ~f:{ f =3D fun c -> >> > return (f :=3D fun () -> >> >=20 >> Fn.ignore (map >> (input_line c) ~f:print_string_option)) }; >> > !f () >> > >> > gets >> Exception: (Sys_error "Bad file descriptor"). Even though the >> >=20 >> channel >>>> > and any operations on it can't escape the closure, the type of=20 >>>> a >> > >> function >> > which uses them needn't mention them at all. >> > >>=20 >> > It's >> pretty hard to do anything about this in the presence of >> >=20 >> unrestricted >>>> > side effects, so perhaps there's a reason why the Haskellers=20 >>>> are >> > >> excited >> > about this sort of thing and you don't see it in OCaml=20 >> so much >> :) >> > >> > That said, you do seem to be forced to make a bit more=20 >> of an >> effort >> > to >> > break >> > things here, so I don't think the=20 >> technique >> is completely without >> > merit, >> > perhaps in cases where you'd=20 >> be >> defining all your own operations >> > anyway, >> > so >> > the=20 >> duplication >> isn't an issue. >> > >> > >> > On 8 August 2014 12:30, Ben Millwood >> >> > wrote: >> >> >> >> There's a trick=20 >> with >> existential types, as used in e.g. Haskell's ST >> >> monad. It=20 >> uses the >> fact that an existentially-quantified type >> >> variable >> >>=20 >> can't >> >> >> escape its scope, so if your channel type and results that depend=20 >> on >> >> >> it >> >> are >> >> parametrised by an existential type variable, the >> corresponding >> >> values >> >> can't >> >> escape the scope of the >> callback either. >> >> >> >> Something like: >> >> >> >> module ST=20 >> : sig >> >>>> type ('a, 's) t >> >> include Monad.S2 with type ('a, 's) t :=3D=20 >>>> ('a, 's) t >>>> >> type 's chan >> >> type 'a f =3D { f : 's . 's chan -> ('a, 's)=20 >>>> t } >> >>>> val with_file : string -> f:'a f -> 'a >> >> >> >> val input_line=20 >>>> : 's >> chan -> (string option, 's) t >> >> end =3D struct >> >> module T =3D=20 >> struct >> >>>> type ('a, 's) t =3D 'a >> >> let return x =3D x >> >> let bind x f = =3D=20 >>>> f x >> >>>> let map x ~f =3D f x >> >> end >> >> include T >> >> include=20 >>>> Monad.Make2(T) >>>> >> type 's chan =3D In_channel.t >> >> type 'a f =3D { f : 's . 's=20 >>>> chan -> >> ('a, 's) t } >> >> let with_file fp ~f:{ f } =3D In_channel.with_file=20 >> fp ~f >> >>>> let input_line c =3D In_channel.input_line c >> >> end >> >> ;; >>=20 >>>> >> >> >> >> match ST.with_file "safe.ml" ~f:{ ST.f =3D fun c -> ST.input_line c }=20 >> >> >> >> with >> >> | None -> print_endline "None" >> >> | Some line ->=20 >> print_endline >> line >> >> >> >> >> >> On 8 August 2014 11:23, Philippe Veber >> >> >> wrote: >> >>> >> >>> Dear all, >>=20 >> >>> >> >>>>> many libraries like lwt, batteries or core provide a very nice=20 >>>>> >> >>> >> idiom >> >>> to >> >>> be used when a function uses a resource=20 >> (file, >> connection, mutex, >> >>> et >> >>> cetera), for instance in >> Core.In_channel, the function: >> >>> >> >>> val with_file :=20 >> ?binary:bool -> >> string -> f:(t -> 'a) -> 'a >> >>> >> >>> opens a channel for [f]=20 >> and >> ensures it is closed after the call to >> >>> [f], >> >>> even if=20 >> it raises >> an exception. So these functions basically >> >>> prevent >> >>>=20 >> from >> >>> >> leaking resources. They fail, however, to prevent a user from using=20 >> >> >>> >> the >> >>> resource after it has been released. For instance,=20 >> writing: >> >>>>> >> >>> input_char (In_channel.with_file fn (fun x -> x)) >> >>>=20 >>>>> >> >>> >> is perfectly legal type-wise, but will fail at run-time. There are=20 >> >> >>> of >>>> >>> course less obvious situations, for instance if you define a=20 >>>> >> >>> >> function: >> >>> >> >>> val lines : in_channel -> string Stream.t=20 >> >> >>> >> >>>>> then the following will also fail: >> >>> >> >>> Stream.iter f >> (In_channel.with_file fn lines) >> >>> >> >>> My question is the=20 >> following: >> is there a way to have the compiler >> >>> check >> >>> resources=20 >> are not >> used after they are closed? I presume this can >> >>> only >> >>>=20 >> be >> >>> >> achieved by strongly restricting the kind of function passed to >>=20 >> >>> >> [with_file]. >> >>> One simple restriction I see is to define a=20 >> type of >> immediate >> >>> value, >> >>> that >> >>> roughly correspond to=20 >> "simple" >> datatypes (no closures, no lazy >> >>> expressions): >> >>> >> >>>=20 >> module >> Immediate : sig >> >>> type 'a t =3D private 'a >> >>> val int : int=20 >> -> int t >>>> >>> val list : ('a -> 'a t) -> 'a list -> 'a list t >> >>> val=20 >>>> tuple : >> ('a -> 'a t) -> ('b -> 'b t) -> ('a * 'b) -> ('a * >> >>> 'b) t >>=20 >> >>> (* >> for records, use the same trick than in >> >>> >> http://www.lexifi.com/blog/dynamic-types *) >> >>> ... >> >>> end=20 >> >> >>> >> >>>>> and have the type of [with_file] changed to >> >>> >> >>> val=20 >>>>> with_file >> : string -> f:(in_channel -> 'a Immediate.t) -> 'a >> >>> >> >>>=20 >> I'm sure >> there are lots of smarter solutions out there. Would >> >>> anyone=20 >> >> >>> >> happen to know some? >> >>> >> >>> Cheers, >> >>> Philippe. >> >>>=20 >> >> >> >> >>> >> >> >> >> -- >> Markus Mottl http://www.ocaml.info >> markus.mottl@gmail.com > > -- Markus Mottl http://www.ocaml.info >> markus.mottl@gmail.com -- Caml-list mailing list. Subscription=20 >> management >> and archives: https://sympa.inria.fr/sympa/arc/caml-list Beginner's=20 >> list: >> http://groups.yahoo.com/group/ocaml_beginners Bug reports: >> http://caml.inria.fr/bin/caml-bugs >>=20 >> -- >> Markus Mottl http://www.ocaml.info markus.mottl@gmail.com >> -- >> 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 >=20 >=20 >=20 > --=20 > Markus Mottl http://www.ocaml.info=20=20=20=20=20=20=20=20 > markus.mottl@gmail.com >=20 > --=20 > 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 --=-bSt8KoiiMdwfeuvMGcOu Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable But you can't do any meaningful computation with this value.
It's just = a blackbox.

In your example, the (ST.input_line c)= inside the closure just build a value of type (string option, '_s) t for a= specific s.
In particular you will never be able to execute any = action on the input channel.

This is not the case = with the proposed implementation, because effects are executed immediately.=
To ensure isolation, all effects should be "frozen":
<= br>
module ST : sig
  type ('a, 's) t
  val return : 'a -> ('a, 's) t
  val bind : ('a,= 's) t -> ('a -> ('b, 's) t) -> ('b, 's) t
  type '= s chan
  type 'a f =3D { f : 's . 's chan -> ('a, 's) t }=
  val with_file : string -> f:'a f -> 'a
  val input_line : 's chan -> (string, 's) t
end =3D struct
  type ('a, 's) t =3D unit -> 'a
  let return x =3D fun () -> x
  let bind x f =3D = fun () -> f (x ()) ()

  type 's chan =3D i= n_channel
  type 'a f =3D { f : 's . 's chan -> ('a, 's) = t }
  let with_file fp ~f:{ f } =3D
    = let ic =3D open_in fp in
    try
   = ;   let result =3D f ic () in
      close_in_= noerr ic;
      result
    wit= h exn ->
      close_in_noerr ic;
&nb= sp;     raise exn
  let input_line c =3D fun () -&= gt; input_line c
end

Le ven. 8 ao=C3=BBt 2014= =C3=A0 20:30, Markus Mottl <markus.mottl@gmail.com> a =C3=A9crit&nbs= p;:
The escaping value can still be manipulated through a closure, out= side of "with_file". The goal was to prevent this. On Fri, Aug 8, 2014 at 2:28 PM, Fr=C3=A9d=C3=A9ric Bour <frederic.bour@l= akaban.net> wrote:
ST.input_line is just a reified effect, it can't be executed o= utside of the ST monad. You can make the value escape, but you can't do anything with it. And because of the existential variable being propagated, it can't be executed outside of this run of the ST value. Le ven. 8 ao=C3=BBt 2014 =C3=A0 19:23, Markus Mottl <markus.mottl@gmail= .com> a =C3=A9crit : How would you implement this safely with ST here? I wasn't using the standard input_line but "ST.input_line", which already returns a monadic type. The trick here was to use the monadic "return" to return a closure that captures the existential variable, allowing me to execute the computation outside of the safe region. Regards, Markus On Fri, Aug 8, 2014 at 1:37 PM, Gabriel Scherer <gabriel.scherer@gmail.com> wrote: The ST trick only works when all primitives affecting resource are in the monadic abstraction (they mention the ST region variable in their computation type). This is not the case in Markus example as "input_line" = is a non-typed effect. Using ST safely would be possible in OCaml, but you would have to completely eschew the standard library and use a different base where all effectful functions have a monadic type. It is the library, not the language itself, that allows this. On the contrary, linear types a= re distinctly a language feature. Using monads to encapsulate a form of linearity is an old trick. If you want to have a taste of built-in linear typing, you may want to give Mezzo a try ( http://protz.github.io/mezzo/ ). On Fri, Aug 8, 2014 at 7:21 PM, Markus Mottl <markus.mottl@gmail.com>= ; wrote: I see, I was replying to the "reference problem" and hadn't read your implementation, which, besides existentials, already requires monads as return values. Actually, it just occurred to me that one can even break the monadic approach in a purely functional way. You are just one "return" away from disaster: let f =3D ST.with_file "foo.txt" ~f:{ ST.f =3D fun c -> = ST.return (fun () -> ignore (ST.input_line c)) } in f () You'd have to eliminate "return", in which case it wouldn't be a monad anymore and not general enough for realistic uses of "with_file". Regards, Markus On Fri, Aug 8, 2014 at 12:01 PM, Ben Millwood <bmillwood@janestreet.com> wrote: >= ; I protected against that in my module by carrying the existential type > variable in the result of input_line as well, because I stumbled into > exactly that example while originally drafting my e-mail :) > > In a= sense I'm reinventing monadic IO but in a bit of a half-hearted > way. It > wouldn't take much work to make it a bit more fully-hearted, but it > w= ould
still be inconvenient to actually use. > > > On 8 Aug= ust 2014 16:44,
Markus Mottl <markus.mottl@gmail.com> wrote: >> &= gt;> It doesn't even require references to screw things up here. Just >> return the closure conta= ining the channel from within "f": >> >> In_channel.with_file "foo.t= xt" ~f:(fun ic () -> input_line ic) >> |> fun f -> f () >> >> = The initial Stream-example is basically just an instance of this >> "returning a closure" problem.= >> >> But the availability of references and exceptions arguably makes >> = things worse, because you cannot even use monadic I/O + existential >> type= s to achieve guaranteed safety. >> >> Regards, >> Markus >= > >> On Fri, Aug 8, 2014 at 10:49 AM, Ben Millwood >> <bmillwood@janestreet.com> &= gt;> wrote: >> > It's been pointed out to me that the above certainly isn't perfectly >&= gt; > secure. >> > E.g. >> > >> > let f =3D ref (fun = () -> ()) in >> > with_file "safe.ml" ~f:{ f =3D fun c -> >> > return (f :=3D fun () ->= >> > Fn.ignore (map (input_line c) ~f:print_string_option)) }; >> > !f () >> &g= t; >> > gets Exception: (Sys_error "Bad file descriptor"). Even though the >> >= ; channel
> and any operations on it can't escape the clo= sure, the type of a >> >
function >> > which uses them needn't m= ention them at all. >> > >> > It's pretty hard to do anything about this in the presence of >> > unr= estricted
> side effects, so perhaps there's a reason why= the Haskellers are >> >
excited >> > about this sort of thing a= nd you don't see it in OCaml so much :) >> > >> > That said, you do seem to be forced to make= a bit more of an effort >> > to >> > break >> > things here, so = I don't think the technique is completely without >> > merit, >> > perhaps in cases = where you'd be defining all your own operations >> > anyway, >> > so &g= t;> > the duplication isn't an issue. >> > >> > >> > On 8 August 2014= 12:30, Ben Millwood <bmillwood@janestreet.com> >> > wrote: >> >> &g= t;> >> There's a trick with existential types, as used in e.g. Haskell's ST >> >> monad. I= t uses the fact that an existentially-quantified type >> >> variable >= > >> can't >> >> escape its scope, so if your channel type and results that depend on >&= gt; >> it >> >> are >> >> parametrised by an existential = type variable, the corresponding >> >> values >> >> can't >> &g= t;> escape the scope of the callback either. >> >> >> >> Something like: >&= gt; >> >> >> module ST : sig >>
type ('a, 's) t >> >> include Monad.S2= with type ('a, 's) t :=3D ('a, 's) t >> type 's chan >> >> type 'a f =3D { f : 's . 's chan -= > ('a, 's) t } >> val with_file : string -> f:'a f -> 'a >> >> >> &g= t;> val input_line : 's
chan -> (string option, 's) t >> >&g= t; end =3D struct >> >> module T =3D struct >>
type ('a, 's) t =3D 'a >> >> let retur= n x =3D x >> >> let bind x f =3D f x >> let map x ~f =3D f x >> >> end >> >> include T >= ;> >> include Monad.Make2(T) >> type 's chan =3D In_channel.t >> >> type 'a f =3D { f= : 's . 's chan ->
('a, 's) t } >> >> let with_file fp = ~f:{ f } =3D In_channel.with_file fp ~f >>
let input_line c =3D In_channel.input_line c >&= gt; >> end >> >> ;; >> >> >> >>
match ST.with_file "safe.ml" ~f:{ ST.f =3D fun c= -> ST.input_line c } >> >> with >> >> | None -> print_endline "None" >> >>= | Some line -> print_endline line >> >> >> >> >> >> On 8 August 201= 4 11:23, Philippe Veber <philippe.veber@gmail.com> >> >> wrote: >> >>= ;> >> >>> Dear all, >> >>> >>
many libraries like lwt, batteries or = core provide a very nice >> >>>
idiom >> >>> to >= > >>> be used when a function uses a resource (file, connection, mutex, >> >>> et >> >>> cetera),= for instance in Core.In_channel, the function: >> >>> >> >>>= val with_file : ?binary:bool -> string -> f:(t -> 'a) -> 'a >> >>> >> >&g= t;> opens a channel for [f] and ensures it is closed after the call to >> >>> [f], >>= >>> even if it raises an exception. So these functions basically >> >>> prevent &= gt;> >>> from >> >>> leaking resources. They fail, however, to prevent a user from using >&g= t; >>> the >> >>> resource after it has been released. For instanc= e, writing: >>
>> >>> input_char (In_c= hannel.with_file fn (fun x -> x)) >> >>> >> >>= ;>
is perfectly legal type-wise, but w= ill fail at run-time. There are >> >>> of
>>> course less obvious situations, for i= nstance if you define a >> >>>
function: >> >>> >> >>= ;> val lines : in_channel -> string Stream.t >> >>> &g= t;>
then the following will also fail: >= ;> >>> >> >>> Stream.iter f
(In_channel.with_file fn lines) >= ;> >>> >> >>> My question is the following: is there a way to have the compiler >> >>> check >> &= gt;>> resources are not used after they are closed? I presume this can >> >>> only = >> >>> be >> >>> achieved by strongly restricting the kind of function passed to >> &= gt;>> [with_file]. >> >>> One simple restriction I see is to defi= ne a type of immediate >> >>> value, >> >>> that >>= >>> roughly correspond to "simple" datatypes (no closures, no lazy >> >>> expressions): >&g= t; >>> >> >>> module Immediate : sig >> >>> type 'a t =3D private 'a >> &g= t;>> val int : int -> int t
>>> val list : ('a -> 'a t) -> 'a l= ist -> 'a list t >> >>> val tuple :
('a -> 'a t) -> ('b -> 'b t) -> ('a = * 'b) -> ('a * >> >>> 'b) t >> >>> (* for records, use the same trick than in >> >>> http://www.lexifi.com= /blog/dynamic-types *) >> >>> ... >> >>> = end >> >>> >>
and have the type of [with_file] chang= ed to >> >>> >> >>> val with_file
: string -> f:(in_channel -> = 'a Immediate.t) -> 'a >> >>> >> >>> I'm su= re there are lots of smarter solutions out there. Would >> >>>= anyone >> >>> happen to know some? >> >>> >> >>> Cheers, &= gt;> >>> Philippe. >> >>> >> >> >= >
>> >> >> >> -- >> Markus Mottl <= a href=3D"http://www.ocaml.info">http://www.ocaml.info
markus.mottl@gmail.com > > -- Markus Mottl http://www.ocaml.info markus.mottl@gmail.com -- Caml-list mailing list. Subscription management and archives: https= ://sympa.inria.fr/sympa/arc/caml-list Beginner's list: http://groups.ya= hoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/ca= ml-bugs -- Markus Mottl http://www.ocaml.info m= arkus.mottl@gmail.com -- Caml-list mailing list. Subscription management and archives: https://sympa.inria= .fr/sympa/arc/caml-list Beginner's list: http://groups.ya= hoo.com/group/ocaml_beginners Bug reports: http://caml.inria.fr/bin/ca= ml-bugs
--=20
Markus Mottl http://www.ocam= l.info markus.mottl@gmail.com
--=20
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.inr= ia.fr/bin/caml-bugs
= --=-bSt8KoiiMdwfeuvMGcOu--