caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dmitry Grebeniuk <gdsfh1@gmail.com>
To: Goswin von Brederlow <caml-list@inria.fr>
Subject: Re: [Caml-list] Obj.magic for polymorphic identifiers
Date: Mon, 28 Apr 2014 11:13:34 +0300	[thread overview]
Message-ID: <CAPi0vKX_91h1N=23Ho9K+RnTiNQe5ibOQn9CQLaR0BU6WWzKqw@mail.gmail.com> (raw)
In-Reply-To: <20140428073625.GA5776@frosties>

Hello.

>> # open Cdt;;
>> # let hlist = [ubox ti_int 123; ubox ti_string "qwe"; ubox ti_bool true];;
>> val hlist : Cdt.ubox list =
>>   [{ub_store = <fun>; ub_uti = <obj>}; {ub_store = <fun>; ub_uti = <obj>};
>>    {ub_store = <fun>; ub_uti = <obj>}]
>
> How do you encode a
>
> type foo = Foo | Bar
>
> Someone has to implement a ti_foo for it.

  I do not encode type foo anyhow, if its structure is
not needed, like in Romain's example.  As for ti_foo,
you are right:
  let ti_foo : foo #ti = new ti_simple "foo" ()
is enough to box/unbox values of type foo using
ti_foo.  (Also type name is not needed, but it eases
values introspection, so I always use meaningful
type names here.  Type annotation is not needed
too, in most cases.)

>>   if uti == (ti_string :> uti) then uget_exn ti_string ub else

> Here you have to match every possible type, which doesn't scale.

  It's just Romain's example encoded with my library.
Of course this approach doesn't scale, but second
piece of code ("show" method) scales well.

> Or you limit yourself to structure, which is unsound
> for retrieving the original type.

  Sorry, can't understand what you mean here;
but maybe other parts of my reply answer this.

> Where is your
>
> val unbox : type a . uti -> Cdt.ubox -> a
>
> (which isn't well typed like that)

  Of course it's not typed.  One has to provide
"witness" for this operation.  Both examples had
such "unbox":

uget_exn : #tti 'a -> ubox -> 'a

  So, no magic.  But no Obj too!

  Answering further possible questions:
- What if 'a and 'b, where 'a <> 'b, will have the
same "runtime type information"?  --
It's impossible.  ('a and 'b could _look_ different
due to type aliases or module abstractions,
but they have the same "original type")
- What if one creates two distinct "runtime type
informations" for given 'a, how will this work?  --
It won't work: once a "witness" was used to
create ubox, only this "witness" can be used to
retrieve typed value.
  (however I do some kind of "inheritance" on
"witnesses" and untyped methods dictionaries
that allows me to use some tricks like redefining
"show" or "cmp" methods leaving possibility
to work with uboxes using different typeinfos,
but it's not essential here.)

      reply	other threads:[~2014-04-28  8:13 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-22  8:03 Romain Bardou
2014-04-22  8:31 ` Jeremie Dimino
2014-04-24 14:28   ` Goswin von Brederlow
2014-05-04 23:46     ` Leo White
2014-04-24 15:30 ` Dmitry Grebeniuk
2014-04-28  7:36   ` Goswin von Brederlow
2014-04-28  8:13     ` Dmitry Grebeniuk [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='CAPi0vKX_91h1N=23Ho9K+RnTiNQe5ibOQn9CQLaR0BU6WWzKqw@mail.gmail.com' \
    --to=gdsfh1@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).