From: ·········@my-dejanews.com
Subject: Does LISP have a "union" construct?
Date: 
Message-ID: <742l77$tul$1@nnrp1.dejanews.com>
Hello,
I am a C/C++ programmer trying to learn LISP. I wanted
to know whether  LISP  has the construct equivalent to
the "union" construct of C.
Ex.
	union A {
	    int   i;
	    float f;
	};

declares a union in C. A variable of the above type
has size equal to 4 bytes
i.e. size of variable of type A = max(size of int, size of float)
considering size of int = 4 and size of float = 4.

i.e. the variable can hold either an int or a float
at any given instant but not both.

Thanking you in advance,
Mayur

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

From: Marco Antoniotti
Subject: Re: Does LISP have a "union" construct?
Date: 
Message-ID: <lwzp967uy8.fsf@copernico.parades.rm.cnr.it>
·········@my-dejanews.com writes:

> Hello,
> I am a C/C++ programmer trying to learn LISP. I wanted
> to know whether  LISP  has the construct equivalent to
> the "union" construct of C.
> Ex.
> 	union A {
> 	    int   i;
> 	    float f;
> 	};
> 
> declares a union in C. A variable of the above type
> has size equal to 4 bytes
> i.e. size of variable of type A = max(size of int, size of float)
> considering size of int = 4 and size of float = 4.
> 
> i.e. the variable can hold either an int or a float
> at any given instant but not both.
> 

In C you would usually use something like

struct C {
  union { int i; float f; }
  short tag;
}


and then say

{
   struct C *theC = /* some pointer to a C */

   if (theC->tag == INT_TAG)
	...
   else if (theC->tag == FLOAT_TAG)
}

In Common Lisp you do

(let ((theC #| some C, either integer of float |#))
  (typecase
    (integer ....)
    (float   ....)))

I.e. withouth entering into technicalities, since CL is "dynamically
typed" you do not need unions.

Another, IMHO evil, use of unions in C is

typedef union {
  struct { int head[4]; int rest[4] }
  int raw[8];       
} different_views;

Then you can do

{
  different_vievs dv1, dv2;

  /* fill in dv1 and dv2 */

  if (dv1.raw[7] == dv2.rest[3]) /* do stuff */
  if (dv1.rest[1] == dv2.raw[5]) /* do stuff */
}


You can achieve this in CL with displaced arrays, but the idiom is not
very terse.

As an aside, you cannot do that even in Ada.  You have to resort to
the similar 'displacing' idioms.

Cheers

-- 
Marco Antoniotti ===========================================
PARADES, Via San Pantaleo 66, I-00186 Rome, ITALY
tel. +39 - (0)6 - 68 10 03 17, fax. +39 - (0)6 - 68 80 79 26
http://www.parades.rm.cnr.it
From: Rainer Joswig
Subject: Re: Does LISP have a "union" construct?
Date: 
Message-ID: <joswig-0212981101570001@pbg3.lavielle.com>
In article <··············@copernico.parades.rm.cnr.it>, Marco Antoniotti
<·······@copernico.parades.rm.cnr.it> wrote:

> Another, IMHO evil, use of unions in C is
> 
> typedef union {
>   struct { int head[4]; int rest[4] }
>   int raw[8];       
> } different_views;
> 
> Then you can do
> 
> {
>   different_vievs dv1, dv2;
> 
>   /* fill in dv1 and dv2 */
> 
>   if (dv1.raw[7] == dv2.rest[3]) /* do stuff */
>   if (dv1.rest[1] == dv2.raw[5]) /* do stuff */
> }
> 
> 
> You can achieve this in CL with displaced arrays, but the idiom is not
> very terse.
>

From the ANSI CL HyperSpec on MAKE-ARRAY:

   If displaced-to is non-nil, make-array will create a
   displaced array and  displaced-to is the target of
   that displaced array. In that case, the
-> consequences are undefined if the actual
   array element type of displaced-to is not
   type equivalent to the actual array element
   type of the array being created. If displaced-to
   is nil, the array is not a displaced array.

-- 
http://www.lavielle.com/~joswig
From: David Thornley
Subject: Re: Does LISP have a "union" construct?
Date: 
Message-ID: <mjf92.2028$764.8010100@ptah.visi.com>
In article <·······················@pbg3.lavielle.com>,
Rainer Joswig <······@lavielle.com> wrote:
>In article <··············@copernico.parades.rm.cnr.it>, Marco Antoniotti
><·······@copernico.parades.rm.cnr.it> wrote:
>
>> Another, IMHO evil, use of unions in C is
>> 
>> typedef union {
>>   struct { int head[4]; int rest[4] }
>>   int raw[8];       
>> } different_views;
>> 
>> Then you can do
>> 
>> {
>>   different_vievs dv1, dv2;
>> 
>>   /* fill in dv1 and dv2 */
>> 
>>   if (dv1.raw[7] == dv2.rest[3]) /* do stuff */
>>   if (dv1.rest[1] == dv2.raw[5]) /* do stuff */
>> }
>> 
>> 
>> You can achieve this in CL with displaced arrays, but the idiom is not
>> very terse.
>>
>
>From the ANSI CL HyperSpec on MAKE-ARRAY:
>
>   If displaced-to is non-nil, make-array will create a
>   displaced array and  displaced-to is the target of
>   that displaced array. In that case, the
>-> consequences are undefined if the actual
>   array element type of displaced-to is not

No difference.  The result is undefined in either language, and a naive
implementation of unions/displaced arrays in either language is likely
to work as expected, given certain caveats.  Assuming the caveats
change (very likely), or the implementation is a bit more sophisticated,
or one uses a compiler with certain common optimizations turned on,
it's likely to fail in either case.

So the answer is that Lisp has all the functionality of a C union as
a result of dynamic typing.

--
David H. Thornley                        | These opinions are mine.  I
·····@thornley.net                       | do give them freely to those
http://www.thornley.net/~thornley/david/ | who run too slowly.       O-
From: Rainer Joswig
Subject: Re: Does LISP have a "union" construct?
Date: 
Message-ID: <joswig-0212982036130001@pbg3.lavielle.com>
In article <······················@ptah.visi.com>, ········@visi.com
(David Thornley) wrote:

> >From the ANSI CL HyperSpec on MAKE-ARRAY:
> >
> >   If displaced-to is non-nil, make-array will create a
> >   displaced array and  displaced-to is the target of
> >   that displaced array. In that case, the
> >-> consequences are undefined if the actual
> >   array element type of displaced-to is not
> 
> No difference.  The result is undefined in either language, and a naive
> implementation of unions/displaced arrays in either language is likely
> to work as expected, given certain caveats.  Assuming the caveats
> change (very likely), or the implementation is a bit more sophisticated,
> or one uses a compiler with certain common optimizations turned on,
> it's likely to fail in either case.
> 
> So the answer is that Lisp has all the functionality of a C union as
> a result of dynamic typing.

Certain implementations may signal an error when
you want to create such an displaced array.

-- 
http://www.lavielle.com/~joswig
From: Erik Naggum
Subject: Re: Does LISP have a "union" construct?
Date: 
Message-ID: <3121647269687464@naggum.no>
* ······@lavielle.com (Rainer Joswig)
| Certain implementations may signal an error when
| you want to create such an displaced array.

  from my reading of the standard, implementations are required to do this
  only for safe code.  "safe code" means a prevailing (optimize (safety 3))
  declaration.

  at least under Allegro CL, an (optimize (safety 0)) declaration turns off
  every check there is, but you are definitely on your own if you do that.

#:Erik
-- 
  The Microsoft Dating Program -- where do you want to crash tonight?
From: Barry Margolin
Subject: Re: Does LISP have a "union" construct?
Date: 
Message-ID: <axe92.61$5M.12307@burlma1-snr1.gtei.net>
In article <············@nnrp1.dejanews.com>,
 <·········@my-dejanews.com> wrote:
>Hello,
>I am a C/C++ programmer trying to learn LISP. I wanted
>to know whether  LISP  has the construct equivalent to
>the "union" construct of C.
>Ex.
>	union A {
>	    int   i;
>	    float f;
>	};
>
>declares a union in C. A variable of the above type
>has size equal to 4 bytes
>i.e. size of variable of type A = max(size of int, size of float)
>considering size of int = 4 and size of float = 4.
>
>i.e. the variable can hold either an int or a float
>at any given instant but not both.

In Lisp, variables hold references to other objects, not the objects
themselves (although in many implementations, fixnums are implemented as
immediate objects -- the value is embedded in the reference).  So the size
of a variable or structure/instance slot is independent of the type of the
value "contained" in it -- they're all the size of a pointer.  The main
exception to this is arrays with certain :ELEMENT-TYPE settings; most
implementations can optimize the storage layout of integer and floating
point arrays.

The closest equivalent to a union type declaration is the OR type
specifier.  For the above reason, it doesn't have any impact on variable
size.  And if you use it in the :ELEMENT-TYPE option to MAKE-ARRAY, I would
expect every implementation to upgrade the element type to T and create an
untyped array.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
From: Erann Gat
Subject: Re: Does LISP have a "union" construct?
Date: 
Message-ID: <gat-0212980930440001@milo.jpl.nasa.gov>
In article <············@nnrp1.dejanews.com>, ·········@my-dejanews.com wrote:

> Hello,
> I am a C/C++ programmer trying to learn LISP. I wanted
> to know whether  LISP  has the construct equivalent to
> the "union" construct of C.
> Ex.
>         union A {
>             int   i;
>             float f;
>         };
> 
> declares a union in C. A variable of the above type
> has size equal to 4 bytes
> i.e. size of variable of type A = max(size of int, size of float)
> considering size of int = 4 and size of float = 4.
> 
> i.e. the variable can hold either an int or a float
> at any given instant but not both.

Yes, Lisp has unions.  In fact, every variable in Lisp is by default
a union of all possible Lisp types.  If you want a constrained union
like your example you can use the OR construct in Lisp's type
declaration system.  The example above would translate into Lisp
roughly as (deftype A (or integer float)) or, to be really strict
about it, (deftype A (or fixnum short-float)).  Using the type system
in Lisp is optional.  It is primarily useful for optimizing and making
code easier to understand.

Erann Gat
···@jpl.nasa.gov
From: ·········@my-dejanews.com
Subject: Sorry folks! for: Does LISP have a "union" construct?
Date: 
Message-ID: <747t7o$dkd$1@nnrp1.dejanews.com>
hello,

It was really very foolish on my part to ask such a question!

Actually, i have been programming in C/C++ for the past
few yrs. and these languages are statically typed. So, i was
confused and forgot that Lisp is a dynamically typed
language!

Thanks once again!
Mayur



-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own