Re: language design implications for variant records in a pascal-like language

Gene <gene.ressler@gmail.com>
Sun, 2 Jan 2011 14:05:18 -0800 (PST)

          From comp.compilers

Related articles
[17 earlier articles]
Re: language design implications for variant records in a pascal-like gneuner2@comcast.net (George Neuner) (2010-12-30)
Re: language design implications for variant records in a pascal-like bobduff@shell01.TheWorld.com (Robert A Duff) (2010-12-31)
Re: language design implications for variant records in a pascal-like bobduff@shell01.TheWorld.com (Robert A Duff) (2010-12-31)
Re: language design implications for variant records in a pascal-like fw@deneb.enyo.de (Florian Weimer) (2010-12-31)
Re: language design implications for variant records in a pascal-like noitalmost@cox.net (noitalmost) (2010-12-31)
Re: language design implications for variant records in a pascal-like gah@ugcs.caltech.edu (glen herrmannsfeldt) (2011-01-02)
Re: language design implications for variant records in a pascal-like gene.ressler@gmail.com (Gene) (2011-01-02)
Re: language design implications for variant records in a pascal-like torbenm@diku.dk (2011-01-04)
Re: language design implications for variant records in a pascal-like jm@bourguet.org (Jean-Marc Bourguet) (2011-01-05)
Re: language design implications for variant records in a pascal-like gneuner2@comcast.net (George Neuner) (2011-01-06)
Re: language design implications for variant records in a pascal-like gneuner2@comcast.net (George Neuner) (2011-01-06)
Re: language design implications for variant records in a pascal-like gneuner2@comcast.net (George Neuner) (2011-01-06)
Re: language design implications for variant records in a pascal-like gneuner2@comcast.net (George Neuner) (2011-01-06)
[32 later articles]
| List of all articles for this month |

From: Gene <gene.ressler@gmail.com>
Newsgroups: comp.compilers
Date: Sun, 2 Jan 2011 14:05:18 -0800 (PST)
Organization: Compilers Central
References: 11-01-008
Keywords: design, storage
Posted-Date: 02 Jan 2011 20:41:03 EST

On Friday, December 31, 2010 5:02:23 PM UTC-5, noitalmost wrote:
> For now, I'm not going to include variant records in my language, but I'm
> going to try to keep my options open for later. So I guess I'd like an example
> of where it would be nice to have a variant record. So, assuming our language
> is C++ (so we eliminate cases in C where a union is used only because we don't
> have inheritance), can someone give me a system progamming example that
> requires a union? I'm still looking for that "aha!" moment of its usefulness
> if the language has classes.
>
> Also, is alternation the general term for this type of construct, or is that
> just the way one says union in ML?


You'd use a union or vr anywhere a block of memory must serve multiple
purposes in different contexts. One example is a buffer manager. A buffer
block can be either in use or free. When in use, as much space as possible
should be usable as buffer space. When free, that same space can be used
entirely for free list pointers and other administrative data. The VR tag
indicates the current use.


Disclaimer: I'm familiar with but far from an expert in ML.


Alternation types in ML--I've also heard them called disjunctive types--and
unions/VRs in Pascal-like languages do serve a common logical purpose of
programming language abstraction. When you want a name to represent one of a
set of other types, give it an AT or VR respectively.


On the other hand, they are not identical. Or rather their semantics differ
along with the storage models of the languages in which they're embedded. On
one hand are Pascal-like languages (including Ada, C, C++ in the current
context) where names refer to blocks of memory. On the other there are the
lisp descendants and functional languages (like Common Lisp, ML, Ruby, and in
part Java, Python, Perl, PHP, and others) where names correspond to references
to blocks of memory, i.e. pointers. At the implementation level of the latter
model, alternation types (I have also heard them called disjunctive types)
amount to a pointer being able to refer to objects of different types and
sizes. (I am ignoring "unboxing" optimizations; they lead to multiple types
being able to occupy the same 4- or 8-byte block in lieu of a pointer.)


In other words, unions/VRs in Pascal-like languages are often used explicitly
to allow a single block of storage to serve multiple purposes. Since the
lisp-like languages take the programmer out of the storage management
business, this purpose can't be served by their disjunctive types. In the
buffer manager example, ML might overlay admin data over buffer space in the
same block of memory, or it might use blocks of different sizes for the
different purposes, creating garbage every time a name takes on a different
variant value. The programmer doesn't have explicit control.


Happy New Year, all.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.