From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 8C42FBC37 for ; Tue, 9 Feb 2010 04:07:34 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApsBANdgcEvRVdnhkWdsb2JhbACDMo13ghKHTDwBAQEBCQsKBxMDq1EugUqFDIhxAQEDBYI+gTZbBA X-IronPort-AV: E=Sophos;i="4.49,433,1262559600"; d="scan'208";a="52067189" Received: from mail-gx0-f225.google.com ([209.85.217.225]) by mail1-smtp-roc.national.inria.fr with ESMTP; 09 Feb 2010 04:07:23 +0100 Received: by gxk25 with SMTP id 25so4745048gxk.17 for ; Mon, 08 Feb 2010 19:07:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type; bh=V4yvyM6iNJy32/aJ2ULrxsKbZRUItSZB1urvoWDMCm0=; b=xxaslKlGqvhUX3A9JienlcsJ+rZIU4FF6surIAOg6PP1tt88375Xjb+QsiUGXnCMCQ VtvCn352i3ez0jkX/zKlJmvkP5jRslpSWTFTPEGmcZW4vcX7zVIBnX6wJrEGS06HG1rp g/6M3FFz2TAB3mmjIreJKeEJcs9TCyubFPzWg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=YrKTQG6d8J0yXe2gKkk+ICrgBqQUOZvQr8bQIGWdyw0LbjVKjVmyx+kCmULGdX5CkB d/Qjyvjw31YFA8ctBYQ1+qHefcj9inJbM0kP7RrhMg/8G0+g3hkQuSvBQeg7P+tWudro TioMp+x99f+AfkBP8vrocs0zseefbrsy7wOlo= MIME-Version: 1.0 Received: by 10.101.7.23 with SMTP id k23mr9632021ani.36.1265684842783; Mon, 08 Feb 2010 19:07:22 -0800 (PST) Date: Mon, 8 Feb 2010 21:07:22 -0600 Message-ID: <14cf844b1002081907y2900c313q97ae0cb6f4c92394@mail.gmail.com> Subject: Preventing values from escaping a context From: Rich Neswold To: Inria Ocaml Mailing List Content-Type: multipart/alternative; boundary=001636c5bdd173a349047f223742 X-Spam: no; 0.00; ocaml:01 val:01 compiler:01 haskell:01 forall:01 ocaml:01 monads:01 val:01 compiler:01 haskell:01 forall:01 monads:01 exception:01 exception:01 functions:01 X-Attachments: cset="UTF-8" cset="UTF-8" --001636c5bdd173a349047f223742 Content-Type: text/plain; charset=UTF-8 Hello, 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 constraints!) Thanks for any help you can provide! -- Rich Google Reader: https://www.google.com/reader/shared/rich.neswold Jabber ID: rich@neswold.homeunix.net --001636c5bdd173a349047f223742 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hello,

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

= Most of the functions in my library take a parameter that describes the cur= rent environment. I call this data type, "context". The context i= s passed to a function which will then use other functions in the library t= o get its job done. The signature of the function that starts all this is:<= /div>

val usingContext : (conte= xt -> 'a) -> 'a

('u= singContext' takes other parameters which are used to create the contex= t 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 returne= d by the function parameter (or re-raises the exception.)

My question is this: Is there a way to make the compile= r reject a function parameter from returning the context parameter? For ins= tance, the identity function should be disallowed since the context is inva= lid outside the scope of 'usingContext'. It's true that a retur= ned context would be unusable, since its resources are gone, but it would b= e nice to prevent contexts from escaping the 'usingContext' functio= n entirely.

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

(I real= ize that making this context a monad is a legitimate solution. However, unt= il I see the Ocaml community including monads in the standard library, I th= ink I'll stick with idiomatic Ocaml. I'd also like to solve this fu= nctionally, so I'm discounting the use of objects. Sorry for these cons= traints!)

Thanks for any help you can provide!

--001636c5bdd173a349047f223742--