caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "François Bobot" <bobot@lri.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Wish: mutable variant types, equivalence with records
Date: Fri, 30 Mar 2012 10:00:08 -0500	[thread overview]
Message-ID: <4F75CA78.4080103@lri.fr> (raw)
In-Reply-To: <87k4229td1.fsf@frosties.localnet>

On 30/03/2012 07:16, Goswin von Brederlow wrote:
> François Bobot<bobot@lri.fr>  writes:
>
>> On 24/03/2012 13:45, Wojciech Meyer wrote:
>>> Please see [1], Alain Frisch has been working recently on implementing
>>> in-line records for constructor arguments.
>>>
>>> It's more implementation/design implications than people might think.
>>>
>>> [1] http://caml.inria.fr/mantis/view.php?id=5528
>>
>> In the thread of this proposed feature, there is a remark that inlined
>> record and normal record can become competing features. There is also
>> the burden that inlined record as proposed can be modified only in
>> pattern matching.
>>
>> But can't we consider that, for a semantic, syntax and typing perspective:
>>
>> type t =
>>     | A of string
>>     | B of ({msg: string; mutable foo:int} as t2)
>>     | C
>>
>> is exactly the same thing than:
>>
>> type t =
>>     | A of string
>>     | B of t2
>>     | C
>>
>> and t2 = {msg: string; mutable foo:int}
>
> That would change existing code because B of t2 currently is a Block of
> size 1 with tag B and pointer to a record of type t2.

I don't propose to change how is compiled the second definition. Just 
that if a developer use the first definition in a module he can consider 
that he define t and t2 like in the second definition. The only 
exception is if you use Obj.magic: Obj.magic (B r) == Obj.magic r.
But Obj.magic is not in the semantic I assume.

>
>> The only difference is that when you create a record of type t2 the
>> tag is directly the one of B in the first case and is the default tag
>> for record (the first tag if I remember well) in the second case. So
>> in the first case applying the constructor B is just the identity.
>
> Maybe the type of a record could be altered to, at least internally,
> include a tag value:
Every records already include a tag value, it's just always the same: 
the tag of the first non-constant constructor of a sum type.

>
> type t = A of string | B of ({msg: string; mutable foo:int} as t2) | C
> type t2 = {[B] x : int; }
>
> The inline record would be identical to the external record with tag
> value and (t2 :>  t) would work even implicitly.

I'm not sure that we should make implicit conversion (inference? 
principality?). But that B r is a noop can be very interesting.

If we want to play to extend this feature (perhaps too much) we can play 
with conversion between sum type without cost by allowing Ap to be also 
the identity:


type tpublic =
| A of ({... } as ta)
| B of ({... } as tb)

type tprivate =
| Ap of ta
| Bp of tb
| Cp of ...


Since A,ta and Ap share the same tag the following function is the identity:

let private_of_public = function
   | A x -> Ap x
   | B x -> Bp x

and this one just make one test:

let public_of_private = function
   | Ap x -> A x
   | Bp x -> B x
   | Cp _ -> invalid_arg "public_of_private: non-public value"


And the semantic is the same than if I define the type ta and tb as 
usual record, just the compilation change.

-- 
François Bobot

  reply	other threads:[~2012-03-30 15:00 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-24 18:26 Goswin von Brederlow
2012-03-24 18:32 ` Lukasz Stafiniak
2012-03-24 18:39   ` Lukasz Stafiniak
2012-03-24 18:42     ` Lukasz Stafiniak
2012-03-25 22:45       ` Goswin von Brederlow
2012-03-24 18:42 ` Jonathan Protzenko
2012-03-24 18:45 ` Wojciech Meyer
2012-03-24 18:59   ` Lukasz Stafiniak
2012-03-29 22:46   ` François Bobot
2012-03-30 12:16     ` Goswin von Brederlow
2012-03-30 15:00       ` François Bobot [this message]
2012-03-31 15:52         ` Goswin von Brederlow
2012-03-31 19:17     ` Alain Frisch
2012-03-26  8:41 ` Romain Bardou

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=4F75CA78.4080103@lri.fr \
    --to=bobot@lri.fr \
    --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).