English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Preventing values from escaping a context
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-02-09 (03:07)
From: Rich Neswold <rich.neswold@g...>
Subject: Preventing values from escaping a context

I'm writing my first serious Ocaml library and have a question, which I'll
state later on.

Most of the functions in my library take a parameter that describes the
current environment. I call this data type, "context". The context is passed
to a function which will then use other functions in the library to get its
job done. The signature of the function that starts all this is:

val usingContext : (context -> 'a) -> 'a

('usingContext' takes other parameters which are used to create the context
parameter. I dropped them in this example to simplify the problem.)

So 'usingContext' creates a context and passes it to the given function.
'usingContext' will free up the resources of the context whether the
function parameter returns normally, or throws an exception. After cleaning
up the resources, it returns the value returned by the function parameter
(or re-raises the exception.)

My question is this: Is there a way to make the compiler reject a function
parameter from returning the context parameter? For instance, the identity
function should be disallowed since the context is invalid outside the scope
of 'usingContext'. It's true that a returned context would be unusable,
since its resources are gone, but it would be nice to prevent contexts from
escaping the 'usingContext' function entirely.

I'm aware that the ST monad, in Haskell, uses some forall abilities to
prevent stuff from leaving the ST monad. Hopefully there's a way to achieve
this using Ocaml.

(I realize that making this context a monad is a legitimate solution.
However, until I see the Ocaml community including monads in the standard
library, I think I'll stick with idiomatic Ocaml. I'd also like to solve
this functionally, so I'm discounting the use of objects. Sorry for these

Thanks for any help you can provide!


Google Reader: https://www.google.com/reader/shared/rich.neswold
Jabber ID: rich@neswold.homeunix.net