caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Guillaume Yziquel <guillaume.yziquel@citycable.ch>
To: orbitz@ezabel.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Scoped Bound Resource Management just for C++?
Date: Thu, 10 Feb 2011 11:39:53 +0100	[thread overview]
Message-ID: <20110210103953.GU17055@localhost> (raw)
In-Reply-To: <F325FC2E-D852-4A7A-8550-F77797392BD0@ezabel.com>

Le Wednesday 09 Feb 2011 à 10:15:43 (-0500), orbitz@ezabel.com a écrit :
> Thanks for the answers everyone.
> 
> How does one safely write code in Ocaml that guarantees resources
> will be freed?  Guillaume mentioned the with-idiom, but even that
> doesn't seem entirely safe.

It was Dmitry Grebeniuk that mentioned it. Not me.

To me, the closest you can get is:

module type Resource = sig

	type t (* opaque type for mystuff. *)

	val v : t option ref

	val free : t -> unit

	(* Some functions manipulating type t, without storing
	 * type t values in any other structures such as lists.
	 * Should avoid anything that 'unopacifies' type t *)

end

let sbrm (x : (module Resource)) =
	let module R = (val x : Resource) in
	let do_stuff z =
		(* Write your code here *)
	in
	let Some w = !v in
	let return_value = do_stuff w in
	v := None;
	free w;
	return_value
	
let wrap_my_stuff (my_stuff : my_type) = (module struct

	type t = my_type

	let v = ref (Some my_stuff)

	(* Declarations of other functions *)

end : Resource)

Essentially, you put my_stuff into a first-class module, when calling
the sbrm function, the module is unpacked, and the type R.t is local to
the scope of the function sbrm. This forbids you to store the value in
the reference v anywhere else (excepts if you use existentialish-type stuff
like objects, first-class modules, or funky records). So you code your
do_stuff function under this typing constraints, when you're done, you
invalidate the reference v, and then free the w stuff, and then return
the value.

This code is glaringly unsatisfactory and inefficient. It's however the
only way I see of (mildly) forbiding at the type level to put the value
that is supposed to have a scope-bounded lifecycle somewhere else in the
heap that might tell the GC that it should live longer.

This also assumes that as soon as you create an object of mystuff, it is
immediately wrapped using wrap_my_stuff, and not used anywhere else.

The memory content of mystuff may live on, but it gets 'finalised' in
the scope of the sbrm function.

You still have issues with putting your stuff into existential types (I
do not think this can be avoided). And in the current state, this idiom
is error-prone. Could perhaps be lifted to a Camlp4 syntax extension
which would make it more 'robust'. Something like

opaque Resource = struct

	opaque type t = mytype

	(* manipulations of type t or of type 'mytype' that gets
	 * rewritten to 't' by Camlp4 *)

end

would generate the module type, the embedding of mytypes into
first-class modules, and the adequate sbrm function, together with an
sbrm keyword mimicking the with idiom.

Needless to say, the with- idiom is much more practical.

Otherwise you would need to refine the type system of OCaml to include
information as to whether an argument can be attached to another
datastructure inside a given function. I do not see why it could not be
done, but I do not see why it should be done.

-- 
     Guillaume Yziquel


  parent reply	other threads:[~2011-02-10 10:40 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-08 23:57 orbitz
2011-02-09  0:46 ` Guillaume Yziquel
2011-02-09  0:48 ` Jacques Garrigue
2011-02-09  6:25 ` dmitry grebeniuk
2011-02-09 12:01 ` rossberg
2011-02-09 15:15   ` orbitz
2011-02-09 16:14     ` Gerd Stolpmann
2011-02-09 16:52       ` David Rajchenbach-Teller
2011-02-09 17:54         ` orbitz
2011-02-09 21:50           ` Jon Harrop
2011-02-10  8:10           ` David Rajchenbach-Teller
2011-02-10 10:39     ` Guillaume Yziquel [this message]
2011-02-10 10:59       ` Guillaume Yziquel
2011-02-09 19:11   ` Florian Weimer
2011-02-09 20:10     ` Andreas Rossberg
2011-02-09 20:45       ` Florian Weimer
2011-02-09 21:12         ` Andreas Rossberg
2011-02-10 21:31           ` Florian Weimer
2011-02-09 18:03 ` Jon Harrop
2011-02-09 20:47 ` Norman Hardy
2011-02-09 21:00   ` Gabriel Scherer

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=20110210103953.GU17055@localhost \
    --to=guillaume.yziquel@citycable.ch \
    --cc=caml-list@inria.fr \
    --cc=orbitz@ezabel.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;
as well as URLs for NNTP newsgroup(s).