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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 37D4DBC37 for ; Tue, 9 Feb 2010 09:34:40 +0100 (CET) X-IronPort-AV: E=Sophos;i="4.49,435,1262559600"; d="scan'208";a="44024209" Received: from arvin.irisa.fr (HELO [131.254.11.86]) ([131.254.11.86]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 09 Feb 2010 09:34:39 +0100 Message-ID: <4B711D44.7040509@irisa.fr> Date: Tue, 09 Feb 2010 09:31:00 +0100 From: Tiphaine Turpin User-Agent: Thunderbird 2.0.0.19 (X11/20090216) MIME-Version: 1.0 To: Jacques Garrigue Cc: rich.neswold@gmail.com, caml-list@inria.fr Subject: Re: [Caml-list] Preventing values from escaping a context References: <14cf844b1002081907y2900c313q97ae0cb6f4c92394@mail.gmail.com> <20100209.123813.39168535.garrigue@math.nagoya-u.ac.jp> In-Reply-To: <20100209.123813.39168535.garrigue@math.nagoya-u.ac.jp> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; irisa:01 val:01 compiler:01 ocaml:01 runtime:01 beginner's:01 ocaml:01 bug:01 polymorphic:01 beginners:01 partial:01 caml-list:01 caml-list:01 functions:01 functions:01 Jacques Garrigue a écrit : > From: Rich Neswold > > >> 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 >> > [...] > >> 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. >> > > The short answer is no. > Types are not sufficient to prevent values from escaping. > In ocaml, you have both functions and references. > There is at least a partial solution using polymorhic records or other ways of quantifying type variables inside a type expression : If you artificially parameterise the type context with an unused parameter (and hide the type definition), you can then require the argument function to be polymorphic with respect to this parameter, which should prevent it from returning or storing its argument. However, it still allows Jacques Garrigue's example, if there exists a function chgCtx which takes any context (ignoring the parameter) and does something with it. Maybe there is a way to type chgCtx so as to prevent that ? Tiphaine > So you can always plug a function of type [unit -> unit] into a > reference, and the function is allowed to access the full context > available when it was created. > > let r = ref (fun () -> ()) > > let f ctx = > r := (fun () -> chgCtx ctx) > > (* later on *) > > List.hd !r () > > > The language is just too expressive... > You should rather look into adding a dynamic flag to your context, > causing a runtime error if you use it later. > > Jacques Garrigue > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > >