[comp.lang.ada] Writing upgradable data structures

daveb@geac.UUCP (David Collier-Brown) (03/08/88)

Date: Fri, 4 Mar 88 23:33 EST
From: Barry Margolin <rutgers!think!barmar>
Subject: Re: Writing upgradable data structures
To: David Collier-Brown <uiucdcs!uiucuxc!unicus!geac!daveb>
In-Reply-To: <8803011228.AA18007@geac.UUCP>
Message-Id: <19880305043329.6.BARMAR@OCCAM.THINK.COM>

 While I was in the Multics group at Honeywell we did some
 investigation of implementing a Multics followon in Ada.  I thought
 the same thing regarding versioned structures, but I was convinced by
 some more experienced Ada people that we could do this using variant
 records.  When you upgrade the structure you add a new variant.  This
 does require recompiling users of the structure, but it doesn't force
 old, stable programs to be recoded unless they want to take advantage
 of the new structure's features.
 

>from dave:
>      Would you consider posting a variant of this? The requirement that
>    one update is an important one, and should be faced by the Ada
>    community.

Here's an example, but the idea is pretty simple.  If you want to post
it to comp.software-eng (I don't think it is appropriate for
comp.lang.c, and I don't subscribe to comp.software-eng), feel free.

type THING is
     record
       VERSION: constant (THING_VERSION_1, THING_VERSION_2, THING_VERSION_3, ...);
       -- first, the components common to all versions
       PART1: <type>;
       PART2: <type>;
       case VERSION of
	    -- No THING_VERSION_1 case, because it only has the common parts
	    when THING_VERSION_2 => PART3: <type>;
				    PART4: <type>;
	    when THING_VERSION_3 => PART3: <type>;
				    PART4: <type>;
				    PART5: <type>;
				    PART6: <type>;
	    when ...
       end case;
     end record;

Unfortunately, Ada requires you to repeat the common prefixes in each
version.  One thing I thought of was to use

type THING is
     record
       VERSION: constant (THING_VERSION_1, THING_VERSION_2, THING_VERSION_3, ...);
       -- first, the components common to all versions
       PART1: <type>;
       PART2: <type>;
       case VERSION of
	    -- No THING_VERSION_1 case, because it only has the common parts
	    when THING_VERSION_2 => PART3: <type>;
				    PART4: <type>;
	    when THING_VERSION_3 => COMMON: THING (VERSION => THING_VERSION_2);
				    PART5: <type>;
				    PART6: <type>;
	    ...
	    when THING_VERSION_n => COMMON: THING (VERSION => THING_VERSION_n-1);
				    PARTm-1: <type>;
				    PARTm: <type>;
       end case;
     end record;

First of all, I don't know whether Ada allows recursive type
declarations like this; if it doesn't, the above becomes:

type THING_V1 is
     record
       PART1: <type>;
       PART2: <type>;
     end record;

type THING_V2 is
     record
       COMMON: THING_V1;
       PART3: <type>;
       PART4: <type>;
     end record;

...

type THING_Vn is
     record
       COMMON: THING_Vn-1;
       PARTm-1: <type>;
       PARTm: <type>;

type THING is
     record
       VERSION: constant (THING_VERSION_1, THING_VERSION_2, THING_VERSION_3, ...);
       case VERSION of
	    when THING_VERSION_1 => CONTENTS: THING_V1;
	    when THING_VERSION_2 => CONTENTS: THING_V2;
	    ...
	    when THING_VERSION_n => CONTENTS: THING_Vn;
       end case;
     end record;

A problem with this, though, is that I think Ada requires the program to
specify all levels of a structure in a reference to a component, so a
program that wants to access PART1 of a THING must use X.CONTENTS.PART1
if it is a version 1 THING, but X.CONTENTS.CONTENTS.CONTENTS.PART1 if it
is a version 3 THING.  PL/I permits the program to leave out structure
qualifiers if the reference is unambiguous.

I don't subscribe to comp.software-eng, so if anyone has any comments on
this, reply via mail.

--
Barry Margolin
Thinking Machines Corp.

barmar@think.com
uunet!think!barmar

daveb@geac.UUCP (David Collier-Brown) (03/10/88)

In article <2417@geac.UUCP> Barry Margolin <rutgers!think!barmar>
writes:
> While I was in the Multics group at Honeywell we did some
> investigation of implementing a Multics followon in Ada.  I thought
> the same thing regarding versioned structures, but I was convinced by
> some more experienced Ada people that we could do this using variant
> records.  When you upgrade the structure you add a new variant.  This
> does require recompiling users of the structure, but it doesn't force
> old, stable programs to be recoded unless they want to take advantage
> of the new structure's features.

   The thing that is hard in Ada[tm] was dealing with a structure
which was *later* than the one you expected. Normal Ada
scope/recompilation rules keep versioned structures in a linked
executable from getting out of date with respect to themselves (with
due care, of course), but don't deal well with a structure which is
sent to it from something external.
   Since this technique came from the ARPAnaut world, you might
guess that the data structures can be packets...

   My work-around is to make sure I pass pointers to things, and not
depend on the `size attribute (which is not exactly what I'd call
good practice... I wonder if its even ethical?).

 --dave c-b
-- 
 David Collier-Brown.                 {mnetor yunexus utgpu}!geac!daveb
 Geac Computers International Inc.,   |  Computer Science loses its
 350 Steelcase Road,Markham, Ontario, |  memory (if not its mind) 
 CANADA, L3R 1B3 (416) 475-0525 x3279 |  every 6 months.