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 (18:09)
From: Rich Neswold <rich.neswold@g...>
Subject: Re: [Caml-list] Preventing values from escaping a context
On Tue, Feb 9, 2010 at 2:31 AM, Tiphaine Turpin <Tiphaine.Turpin@irisa.fr>wrote:

> Jacques Garrigue a écrit :
> > From: Rich Neswold <rich.neswold@gmail.com>
> >> My question is this: Is there a way to make the compiler reject a
> function
> >> parameter from returning the context parameter?
> > 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.

I'm not understanding you fully, sorry. I tried this:

module type Test =
  type 'a context = Context of int * int
  val usingContext : ('a context -> 'b) -> 'b

module InsTest : Test =
  type phantom = unit
  type 'a context = Context of int * int
  let usingContext f = f (Context (1, 2) : phantom context)

But I'm getting a "Signature mismatch" error in the usingContext signature.
Even trying to force the function:

let usingContext (f : 'a context -> 'b) = f (Context (1, 2) : phantom

causes the error (the compiler is too smart  :) I've tried some minor
variations, but without success. Am I even close to what you were

Thank you for your time,


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