caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Allsopp <dra-news@metastack.com>
To: Alain Frisch <alain.frisch@lexifi.com>,
	Dmitry Bely <dmitry.bely@gmail.com>,
	Caml List <caml-list@inria.fr>
Subject: RE: [Caml-list] GADT memory representation
Date: Sat, 3 Dec 2016 14:50:31 +0000	[thread overview]
Message-ID: <E51C5B015DBD1348A1D85763337FB6D9013556F3CE@Remus.metastack.local> (raw)
In-Reply-To: <3f89d13a-14f0-090b-be96-d3f67fcb95f3@lexifi.com>

Alain Frisch wrote:
> On 01/12/2016 10:52, David Allsopp wrote:
> > Dmitry Bely wrote:
> >> I need to access/modify GADT data from C glue code. What is their
> >> memory representation? Is there any difference from ordinary sum types?
> >
> > It's the same - GADTs are "just" add a lot of clever typing stuff on top
> of a normal sum type - they don't affect the runtime operation of the
> code.
> >
> >> Unfortunately OCaml manual doesn't even mention GADTs in section
> >> "Interfacing C with OCaml".
> >
> > That's worth a GPR/Mantis issue.
> 
> I'm not sure we want to document the memory layout of GADTs.  I don't
> think there are concrete plans to do so, but it might be considered to
> change the representation of GADTs so that they cannot be used to compare
> values of different types with the polymorphic comparison function.

Surely we never want the situation where any kind of OCaml value is at least officially inaccessible from the C side? Does that officially exist elsewhere in the runtime?

There are various cases where the GADT soundness can allow the C bindings to rely on type safety, for example:

type hive = _

type 'a value =  
| REG_SZ : string value
| REG_DWORD : int32 value

external writeWindowsRegistry : hive -> key:string -> value:string -> 'a value -> 'a -> unit

is much easier to implement both on the C and OCaml sides with a GADT than the undecorated:

external writeWindowsRegistry : hive -> key:string -> value:string -> value -> 'a -> unit

where either the C or OCaml side must ensure that the data parameter has a type corresponding to the constructor.

> Today you can write:
>    type t = E: 'a -> t
> 
>    let () = assert(E 1 = E true)
> 
> A similar "bad" behavior used to be available for exceptions and this was
> "fixed" by changing their representation (their "slot" now has object_tag
> and a (per process) unique id).  We might want to do the same for GADTs
> (not an easy decision since it would add a lot of overhead) and for first-
> class modules as well.

Indeed it will be nice to get rid of it - though I think that losing the equivalence between GADT and regular sum type would be a shame.


David

      parent reply	other threads:[~2016-12-03 14:50 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-01  9:22 Dmitry Bely
2016-12-01  9:52 ` David Allsopp
2016-12-01 10:26   ` Dmitry Bely
2016-12-01 11:51   ` Alain Frisch
2016-12-01 14:12     ` octachron
2016-12-01 14:32     ` Dmitry Bely
2016-12-01 14:50       ` Gabriel Scherer
2016-12-01 15:21     ` Josh Berdine
2016-12-03 14:50     ` David Allsopp [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=E51C5B015DBD1348A1D85763337FB6D9013556F3CE@Remus.metastack.local \
    --to=dra-news@metastack.com \
    --cc=alain.frisch@lexifi.com \
    --cc=caml-list@inria.fr \
    --cc=dmitry.bely@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).