comp.lang.ada
 help / color / mirror / Atom feed
* Re: null unconstrained arrays vs generic formal types
@ 1993-09-23 22:04 Tucker Taft
  0 siblings, 0 replies; 4+ messages in thread
From: Tucker Taft @ 1993-09-23 22:04 UTC (permalink / raw)


In article <23SEP199311043613@bambam.gsfc.nasa.gov> 
  nbssal@bambam.gsfc.nasa.gov (Stephe Leake) writes:

>I've recently run across an problem with unconstrained arrays and generic
>parameters, due to LRM 12.3.4(5). I use the following convention for index
>types, to allow representing a null array:
>
>    type ZERO_INDEX_TYPE is range 0 .. Index_Last;
>    subtype INDEX_TYPE is ZERO_INDEX_TYPE range 1 .. Index_Last;
>    type INDEX_ARRAY_ELEMENT_TYPE is array (INDEX_TYPE range <>)
>	of ELEMENT_TYPE;
>
>A generic array IO package looks like:
>
>with Text_IO;
>generic
>    type ELEMENT_TYPE is range <>;
>    type INDEX_TYPE is (<>);
>    type INDEX_ARRAY_ELEMENT_TYPE is array (INDEX_TYPE range <>)
>	    of ELEMENT_TYPE;
>    with procedure Element_Get
>	(Item : out ELEMENT_TYPE; Width : in Text_IO.FIELD);
>    with procedure Element_Put (
>	Item : in ELEMENT_TYPE;
>	Width : in Text_IO.FIELD;
>	Base  : in Text_IO.NUMBER_BASE);
>package Unconstrained_Integer_1D is
>    procedure Get (Item : out INDEX_ARRAY_ELEMENT_TYPE; Last : out INDEX_TYPE)
;
>    procedure Put (Item : in INDEX_ARRAY_ELEMENT_TYPE);
>end Unconstrained_Integer_1D;
>
>I would like to instantiate this:
>
>    package Foo is new Unconstrained_Integer_1d
>	(...
>	 INDEX_TYPE => ZERO_INDEX_TYPE,        	-- CONSTRAINT_ERROR!
>	 INDEX_ARRAY_ELEMENT_TYPE => INDEX_ARRAY_ELEMENT_TYPE,
>	 ...
>	);
>
>Giving the Get procedure the parameter profile:
>
>    procedure Get (Item : out INDEX_ARRAY_ELEMENT_TYPE;
>	Last : out ZERO_INDEX_TYPE);
>
>Then Last can be less than Item'first to indicate a null array. But this raise
s
>CONSTRAINT_ERROR, because INDEX_TYPE has different bounds than ZERO_INDEX_TYPE
.
>
>One fix is to declare two different type parameters for the generic
>package:
>
>generic
>    ...
>    type INDEX_TYPE is (<>);
>    type ZERO_INDEX_TYPE is (<>);
>    type INDEX_ARRAY_ELEMENT_TYPE is array (INDEX_TYPE range <>)
>	    of ELEMENT_TYPE;
>	    ...
>package Unconstrained_Integer_1D is
>    procedure Get (Item : out INDEX_ARRAY_ELEMENT_TYPE;
>	    Last : out ZERO_INDEX_TYPE);
>    ...
>end Unconstrained_Integer_1D;
>
>But this does not express the convention that INDEX_TYPE is a subtype of
>ZERO_INDEX_TYPE.
>
>So, does Ada 9x fix this by relaxing LRM 12.3.4(5)? Does anyone have an
>alternate solution?

In Ada 9X, one fix would be to define a subtype using the 'Base attribute.
Hence:

    generic
        ...
        type Index_Subtype is (<>);   -- Note the name change
        type Index_Array_Element_Type is array(Index_Subtype range <>)
            of Element_Type;
        ...
    package Unconstrained_Integer_1D is
        subtype Last_Index_Subtype is Index_Subtype'Base range
          Index_Subtype'Prev(Index_Subtype'First) .. Index_Subtype'Last;
        procedure Get(Item : out Index_Array_Element_Type; 
           Last : out Last_Index_Subtype);
        ...

I have changed the name of the formal type to "Index_Subtype" to reflect
the fact that it is presumably a constrained subtype of its type.
Note that in Ada 83, the 'Base attribute cannot be used in this way;
'Base may only be used as a prefix for other attributes in Ada 83.
In Ada 9X 'Base is fully general for scalar types.

Even simpler (and somewhat more general) than the above would be to just
declare procedure Get as:

      ...
      procedure Get(Item : out Index_Array_Element_Type;  
         Last : out Index_Subtype'Base);

The advantage of this is that it works even if it so happens that
Index_Subtype'First = Index_Subtype'Base'First.  Hence, defining the
subtype Last_Index_Subtype is probably more trouble than it is worth.

>Stephen Leake	NASA Goddard Robotics Lab
>internet : nbssal@robots.gsfc.nasa.gov

S. Tucker Taft     stt@inmet.com
Ada 9X Mapping/Revision Team
Intermetrics, Inc.
Cambridge, MA  02138

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: null unconstrained arrays vs generic formal types
@ 1993-09-23 23:51 Robert I. Eachus
  0 siblings, 0 replies; 4+ messages in thread
From: Robert I. Eachus @ 1993-09-23 23:51 UTC (permalink / raw)


In article <23SEP199311043613@bambam.gsfc.nasa.gov> nbssal@bambam.gsfc.nasa.gov
 (Stephe Leake) writes:


   > So, does Ada 9x fix this by relaxing LRM 12.3.4(5)?  Does anyone
   > have an alternate solution?

   It took me a while to recognize why you had a problem. (Even if you
quoted my favorite paragraph number in Ada 83.)  Ada 9X retains the
same rule, although you could submit this as a comment on the standard
during the balloting.  Of course, the "right" solution might be to
have generic formal subtypes.  But I don't think you need any of that.

   In Ada 9X you could make the array type a generic formal derived
type, but the real solution is to reorganize things so that you have a
generic formal package:

   generic
      type Index_Base_Type is range <>;
      type Element_Type is range <>; -- why not private?
      Index_Last: Index_Base_Type;
   package Element_Arrays is
       subtype ZERO_INDEX_TYPE is range 0 .. Index_Last;
       subtype INDEX_TYPE is ZERO_INDEX_TYPE range 1 .. Index_Last;
       type INDEX_ARRAY_ELEMENT_TYPE is array (INDEX_TYPE range <>)
	   of ELEMENT_TYPE;
        procedure Element_Get
	   (Item : out ELEMENT_TYPE; Width : in Text_IO.FIELD);
        procedure Element_Put (
	   Item : in ELEMENT_TYPE;
	   Width : in Text_IO.FIELD;
	   Base  : in Text_IO.NUMBER_BASE);
        ...
    end Element_Arrays;

    package Int_30_Arrays is new Element_Arrays(Integer, Integer, 30);
    -- for example...

    Now your generic array IO package looks like:

   with Text_IO;
   generic
      with package Elem_Arrays is new Element_Arrays(<>);
   package Unconstrained_Integer_1D is
       use Elem_Arrays; -- depending on your religion. :-)
       procedure Get (Item : out INDEX_ARRAY_ELEMENT_TYPE; Last : out ZERO_INDE
X_TYPE);
       procedure Put (Item : in INDEX_ARRAY_ELEMENT_TYPE);
   end Unconstrained_Integer_1D;

   Of course you could put Element_Get and Element_Put elsewhere, but
the principle is that you can pass two subtypes of the same type as
generic parameters in Ada 9X by wrapping them in a generic package
parameter.
--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: null unconstrained arrays vs generic formal types
@ 1993-09-24 17:50 Stephe Leake
  0 siblings, 0 replies; 4+ messages in thread
From: Stephe Leake @ 1993-09-24 17:50 UTC (permalink / raw)


In article <CDtunJ.Jt0@inmet.camb.inmet.com>, stt@spock.camb.inmet.com (Tucker 
Taft) writes...
>In article <23SEP199311043613@bambam.gsfc.nasa.gov> 
>  nbssal@bambam.gsfc.nasa.gov (Stephe Leake) writes:
> 
>>I've recently run across an problem with unconstrained arrays and generic
>>parameters, due to LRM 12.3.4(5).
>>
	... complicated problem statement deleted; see original post ...

>>So, does Ada 9x fix this by relaxing LRM 12.3.4(5)? Does anyone have an
>>alternate solution?
> 
	... some of Tucker's response deleted ...

>Even simpler (and somewhat more general) than the above would be to just
>declare procedure Get as:
> 
>      ...
>      procedure Get(Item : out Index_Array_Element_Type;  
>         Last : out Index_Subtype'Base);
> 
>The advantage of this is that it works even if it so happens that
>Index_Subtype'First = Index_Subtype'Base'First.  Hence, defining the
>subtype Last_Index_Subtype is probably more trouble than it is worth.
> 

Thanks! That will do just fine. I guess I've finally hit the big time; I posted
a problem that Tucker Taft responded to!

> 
>S. Tucker Taft     stt@inmet.com
>Ada 9X Mapping/Revision Team
>Intermetrics, Inc.
>Cambridge, MA  02138

Stephen Leake	NASA Goddard Robotics Lab
internet : nbssal@robots.gsfc.nasa.gov

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: null unconstrained arrays vs generic formal types
@ 1993-09-24 17:56 Stephe Leake
  0 siblings, 0 replies; 4+ messages in thread
From: Stephe Leake @ 1993-09-24 17:56 UTC (permalink / raw)


In article <EACHUS.93Sep23185154@spectre.mitre.org>, eachus@spectre.mitre.org (
Robert I. Eachus) writes...
>In article <23SEP199311043613@bambam.gsfc.nasa.gov> nbssal@bambam.gsfc.nasa.go
v (Stephe Leake) writes:
> 
> 
>   > So, does Ada 9x fix this by relaxing LRM 12.3.4(5)?  Does anyone
>   > have an alternate solution?
> 
> 	... stuff deleted ...
>
>   In Ada 9X you could make the array type a generic formal derived
>type, but the real solution is to reorganize things so that you have a
>generic formal package:
> 
>   generic
>      type Index_Base_Type is range <>;
>      type Element_Type is range <>; -- why not private?
>      Index_Last: Index_Base_Type;
>   package Element_Arrays is
>       subtype ZERO_INDEX_TYPE is range 0 .. Index_Last;
>       subtype INDEX_TYPE is ZERO_INDEX_TYPE range 1 .. Index_Last;
>       type INDEX_ARRAY_ELEMENT_TYPE is array (INDEX_TYPE range <>)
>	   of ELEMENT_TYPE;
>        procedure Element_Get
>	   (Item : out ELEMENT_TYPE; Width : in Text_IO.FIELD);
>        procedure Element_Put (
>	   Item : in ELEMENT_TYPE;
>	   Width : in Text_IO.FIELD;
>	   Base  : in Text_IO.NUMBER_BASE);
>        ...
>    end Element_Arrays;
> 
>    package Int_30_Arrays is new Element_Arrays(Integer, Integer, 30);
>    -- for example...
> 
>    Now your generic array IO package looks like:
> 
>   with Text_IO;
>   generic
>      with package Elem_Arrays is new Element_Arrays(<>);
>   package Unconstrained_Integer_1D is
>       use Elem_Arrays; -- depending on your religion. :-)
>       procedure Get (Item : out INDEX_ARRAY_ELEMENT_TYPE; Last : out ZERO_IND
EX_TYPE);
>       procedure Put (Item : in INDEX_ARRAY_ELEMENT_TYPE);
>   end Unconstrained_Integer_1D;
> 
>   Of course you could put Element_Get and Element_Put elsewhere, but
>the principle is that you can pass two subtypes of the same type as
>generic parameters in Ada 9X by wrapping them in a generic package
>parameter.

That's really cool! I would put the Element_Get and Element_Put in a daughter
package, so programs that don't need them can't see them. But the ability to
group subtypes (and other stuff) in a generic formal package looks really
powerful. I guess I'll have to take a hard-copy of the Ada 9X manual home for
entertainment reading :-)

>--
> 
>					Robert I. Eachus
> 

Stephen Leake	NASA Goddard Robotics Lab
internet : nbssal@robots.gsfc.nasa.gov

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1993-09-24 17:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-09-24 17:50 null unconstrained arrays vs generic formal types Stephe Leake
  -- strict thread matches above, loose matches on Subject: below --
1993-09-24 17:56 Stephe Leake
1993-09-23 23:51 Robert I. Eachus
1993-09-23 22:04 Tucker Taft

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox