caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Arrays and private types
Date: Thu, 15 Mar 2012 14:18:54 +0100	[thread overview]
Message-ID: <CAPFanBEnpZnA-O4mKreyK6frZP4aqJ=wstxqB_rK4yWuAwesBg@mail.gmail.com> (raw)
In-Reply-To: <20120315110427.GA12350@zed.irill.org>

> Thanks Gabriel, very nice solution. If I go this way, I guess there is
> no way to access array elements using the usual a.(i) syntax (where i
> = M.key i)... [...]
> Is this a problem I can solve using a camlp4 decorator ?

I don't think you need -- nor want to use -- a camlp4 extension. a.(i)
is desugared into (Array.get a i) at a purely syntactical level in
OCaml, so you could overload its behavior by changing the Array module
in the typing environment.

With my example you could write, for example:
module A1 = ArrayMake(struct end)
let () =
  let module Array = A1 in
  let k = A1.key in
  assert (A1.make 3 true).(k 2);;

You could even define the ArrayMake functor so that it returns a
structure with an Array submodule. You would then write, using 3.12
"local open" syntax:

module A1 = ArrayMake(struct end)
let () =
  let open A1 in
  assert (Array.make 3 true).(k 2)

That said, I don't think that the slight readability benefit of
writing a.(i) instead of (get a i) will outweigh the confusion among
your readers that don't understand what you're doing with this weird
Array stuff.

On Thu, Mar 15, 2012 at 12:04 PM, Pietro Abate
<Pietro.Abate@pps.jussieu.fr> wrote:
> On 15/03/12 00:00, Gabriel Scherer wrote:
>> Here is a proposal:
>>   https://gitorious.org/gasche-snippets/private-array-keys-type/blobs/master/private_array_key_types.ml
>>
>> It works by using a functor to generate "fresh" private types for
>> keys. Note that the arrays themselves are still polymorphic (no
>> IntArray FloatArray etc.). The user still has to use the discipline to
>> produce a new application of ArrayMake each time she wants to use a
>> different kind of array: if she only does `module A = ArrayMake(struct
>> end)` and then use `A` for everything, there will be no additional
>> safety guarantee.
>
> Thanks Gabriel, very nice solution. If I go this way, I guess there is
> no way to access array elements using the usual a.(i) syntax (where i
> = M.key i)... (I've noticed your cleaver use of private on the array
> type to avoid using the normal array syntax on your private arrays).
>
> Is this a problem I can solve using a camlp4 decorator ?
>
> This seems a bit complicated as the a.(i) syntax will be context
> dependent, that is, the same syntax used with two different array types
> should be translated to different get/set calls... and at the camlp4
> level I don't have access to type information... Ideally, instead of
> changing all my array access calls, I'd like just to change the type
> of my indexes such that all my generic ints will be replaced by
> M.key of the appropriate type...
>
> p
>
> ps: please do not Cc me. I'm subscribed to the list.
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


      reply	other threads:[~2012-03-15 13:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-14 20:12 Pietro Abate
2012-03-14 23:00 ` Gabriel Scherer
2012-03-15 11:04   ` Pietro Abate
2012-03-15 13:18     ` Gabriel Scherer [this message]

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='CAPFanBEnpZnA-O4mKreyK6frZP4aqJ=wstxqB_rK4yWuAwesBg@mail.gmail.com' \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    /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).