caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Tiphaine Turpin <Tiphaine.Turpin@irisa.fr>
To: Rich Neswold <rich.neswold@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Preventing values from escaping a context
Date: Tue, 09 Feb 2010 19:45:13 +0100	[thread overview]
Message-ID: <4B71AD39.8020503@irisa.fr> (raw)
In-Reply-To: <14cf844b1002091009p1a3181j793c2d6b2cdcbae5@mail.gmail.com>



Rich Neswold a écrit :
> On Tue, Feb 9, 2010 at 2:31 AM, Tiphaine Turpin
> <Tiphaine.Turpin@irisa.fr <mailto:Tiphaine.Turpin@irisa.fr>> wrote:
>
>     Jacques Garrigue a écrit :
>     > From: Rich Neswold <rich.neswold@gmail.com
>     <mailto: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:
>
Sorry, I shouldn't have suggested such an exotic feature (polymorphic
records) without any explanation. Here is an adaptation of your example.
Of course, this kind of trick is not very intuitive, and using it in an
API may not always be a good idea.

Tiphaine

module type Test =
sig
  type 'a context = Context of int * int
  type 'b contextUser = {f : 'a . 'a context -> 'b}
  val usingContext : 'b contextUser -> 'b
end

module InsTest : Test =
struct
  type 'a context = Context of int * int
  type 'b contextUser = {f : 'a . 'a context -> 'b}
  let usingContext f = f.f (Context (1, 2))
end

open InsTest

(* Allowed *)
let _ = usingContext {f = function Context (x, _) -> x}

(* Rejected *)
let _ = usingContext {f = function x -> x}

(* Allowed *)
let _ = usingContext {f = function Context (x, y) -> Context (x, y)}
(* If you also wanted to prevent this one, you could make the type
   "context" either asbtract, with accessors (if you want context
   information to be accessed only in restricted, "consistent" way) or
   private (intuitively, "read-only") *)


  reply	other threads:[~2010-02-09 18:48 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-09  3:07 Rich Neswold
2010-02-09  3:38 ` [Caml-list] " Jacques Garrigue
2010-02-09  8:24   ` Miles Sabin
2010-02-09  8:43     ` Jacques Garrigue
2010-02-09 17:18     ` Rich Neswold
2010-02-09  8:31   ` Tiphaine Turpin
2010-02-09 18:09     ` Rich Neswold
2010-02-09 18:45       ` Tiphaine Turpin [this message]
2010-02-10  0:39         ` Rich Neswold
2010-02-10  8:55           ` Goswin von Brederlow
2010-02-10 18:00             ` Rich Neswold
2010-02-10 21:37               ` Goswin von Brederlow
2010-02-09 17:13   ` Rich Neswold
2010-02-09  3:59 ` Yaron Minsky
2010-02-09 17:16   ` Rich Neswold
2010-02-11 10:39   ` Alexey Rodriguez
2010-02-11 11:05     ` rossberg
2010-02-11 13:52       ` Alexey Rodriguez
2010-02-11 15:17         ` rossberg

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=4B71AD39.8020503@irisa.fr \
    --to=tiphaine.turpin@irisa.fr \
    --cc=caml-list@inria.fr \
    --cc=rich.neswold@gmail.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).