comp.lang.ada
 help / color / mirror / Atom feed
* Help with VAX/VMS Ada
@ 1991-08-05 19:24 James Burnell, WVU Comp/Ele Engr
  0 siblings, 0 replies; 3+ messages in thread
From: James Burnell, WVU Comp/Ele Engr @ 1991-08-05 19:24 UTC (permalink / raw)


Dear fellow Ada users,

I am taking a summer course in Ada (CompSci 2 at West Virginia 
University).  My teacher gave us an assignment to implement a Vector as 
a discriminated record in a generic package as follows (an excerpt from 
the specification she gave us):

generic
    type Vector_Element is private;
    Zero, One : Vector_Element;
    with function "+" (Left, Right : Vector_Element) return Vector_Element
       is <>;
    with function "-" (Left, Right : Vector_Element) return Vector_Element
       is <>;
    with function "*" (Left, Right : Vector_Element) return Vector_Element
       is <>;
package Vector_Package is
    type Vector (Lower_Bound, Upper_Bound : Integer := 0) is limited private;

-- Appropriate operations deleted....

private
    type Vector_Array is array (Integer range <>) of Vector_Element;
    type Vector (Lower_Bound, Upper_Bound : Integer := 0) is
       record
          The_Vector  : Vector_Array (Lower_Bound .. Upper_Bound)
	     := (others => Zero);
       end record;
end Vector_Package;

The package compiles without error.  But an instantiation, for example,

with Vector_Package;
package Int_Vect_Pack is new Vector_Package (Integer, 0, 1);

produces the following compiler warning:

    2 	package Int_Vect_Pack is new Vector_Package (Integer, 0, 1);
........1
%ADAC-I-CONS_OR_NUM_ERR, (1) (For Vector from Vector_Package at line 33) 
        CONSTRAINT_ERROR or NUMERIC_ERROR will be raised here [LRM 11.1(5-6), 
        F.9.5]
        during evaluation related to record type Vector at line 2 (from 
            Vector_Package at line 33)
%ADAC-I-ENDDIAGS, Ada compilation completed with 1 diagnostic


LRM 11.1(5-6) is simply an explanation of Constraint_Error and 
Numeric_Error; F.9.5 I have included below (used without permission):


F.9.5 Implementation Limits

 Limits       Description
-------------------------------------------------------------------------------
 32           Maximum number of formal parameters in a subprogram or entry
              declaration that are of an unconstrained type.
 120          Maximum identifier length (number of characters).
 120          Maximum number of characters in a source line.
 245          Maximum number of discriminants for a record type.
 246          Maximum number of formal parameters in an entry or subprogram
              declarations.
 255          Maximum number of dimensions in an array type.
 1000         Maximum number of library units and subunits in a program library
              or executable program.
 32757        Maximum number of objects declared with PSECT_OBJECT pragmas.
 65535        Maximum number of enumeration literals in an enumeration type
              definition.
 65535        Maximum number of characters in a value of the predefined type
              STRING.
 65535        Maximum number of frames that an exception can propagate.
 65535        Maximum number of lines in a source file.
 2^31 - 1     Maximum number of buts in any object.

As you can see, none of these is an _obvious_ reason for Constraint_Error to be
 
raised.  Furthermore, Constraint_Error is NOT raised if the default values for 
type Vector are removed.

My teacher is researching this also, but I (and she) would appreciate your 
input.  Why is Constraint_Error raised in this situation?

Please send responses to me.  Thanks for your time.

Sincerely,

Jim Burnell  

PREFERRED: UN025523@WVNVMS.WVNET.EDU     | AdaNet: jburnell@asv2.wvnet.edu
BITNET   : UN025523@WVNVMS               | -- Insert STANDARD.DISCLAIMER 

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

* Re: Help with VAX/VMS Ada
@ 1991-08-12 19:39 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!rpi!bu.edu!inmet!stt
  0 siblings, 0 replies; 3+ messages in thread
From: cis.ohio-state.edu!zaphod.mps.ohio-state.edu!rpi!bu.edu!inmet!stt @ 1991-08-12 19:39 UTC (permalink / raw)


> Re: Help with VAX/VMS Ada by UN025523@WVNVAXA.WVNET.EDU 

>     type Vector (Lower_Bound, Upper_Bound : Integer := 0) is
>        record
>           The_Vector  : Vector_Array (Lower_Bound .. Upper_Bound)
> 	     := (others => Zero);
>        end record;

By providing a default for all discriminants, you are allowing the
declaration of unconstrained instances of Vector.

E.g., the following would now be legal:
   X : Vector;

In this situation, most Ada compilers will allocate
space sufficient to hold the largest possible value.
In this case, the largest value is truly enormous,
because the range of the discriminants is Integer.
The message from the compiler indicates that a CONSTRAINT_ERROR
is likely when computing this size of this largest value.

To avoid this problem, define an appropriate subtype of Integer
which provides a reasonable upper bound on the range of indices
for your vector.  E.g.:

   subtype Vector_Index_Range is Integer range 0..100;
   type Vector(LB, UB : Vector_Index_Range := 0) is ...

Now, the largest value is when LB = 0 and UB = 100.
This value is of reasonable size.

S. Tucker Taft     stt@inmet.inmet.com
Intermetrics, Inc.
Cambridge, MA  02138 

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

* Re: Help with VAX/VMS Ada
@ 1991-08-14  4:54 Mike Feldman
  0 siblings, 0 replies; 3+ messages in thread
From: Mike Feldman @ 1991-08-14  4:54 UTC (permalink / raw)


In article <20600114@inmet> stt@inmet.inmet.com writes:
>
>By providing a default for all discriminants, you are allowing the
>declaration of unconstrained instances of Vector.
>
>E.g., the following would now be legal:
>   X : Vector;
>
>In this situation, most Ada compilers will allocate
>space sufficient to hold the largest possible value.
>In this case, the largest value is truly enormous,
>because the range of the discriminants is Integer.

[stuff deleted]

This is one of the interesting portability "gotchas" in the language.
Many compilers (at least the Janus, Meridian, and Alsys families) do
NOT allocate the maximum space, preferring to reallocate when the size of
the object changes dynamically. Meridian allocates only a pointer block
and acquires space as needed; I believe the Janus family does the same.
I am not sure of the Alsys policy, but think I heard somewhere that Alsys
uses the default as an approximation, then chains in more space as needed
(can anyone confirm?).

The time/space tradeoff here is interesting to consider. Allocating the
maximum space obviates the need to reallocate or acquire space dynamically.
Other policies save space at the cost of some time to reallocate. The best
approach, as Tucker points out, is to use a subtype of some realistic size
for the discriminant. I'm sure the original writer never intended his
vectors to have Integer'Last elements, and so ought to have considered
what the _realistic_ maximum should be. 

There's a moral here. The type system can be your friend
or your enemy, as this case points out. Using a realistic subtype is better
design and also more portable, as it will likely cause something reasonable
to be done in every implementation.
-------------------------------------------------------------------------------
Michael B. Feldman
Visiting Professor 1991-92               Professor
Dept. of Comp. Sci. and Engrg.           Dept. of Elect. Engrg. and Comp. Sci.
University of Washington FR-35           The George Washington University
Seattle, WA 98105                        Washington, DC 20052
(206) 685-1376 (voice)                   (202) 994-5253 (voice)
(206) 543-2969 (fax)                     (202) 994-5296 (fax)
-------------------------------------------------------------------------------

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

end of thread, other threads:[~1991-08-14  4:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1991-08-05 19:24 Help with VAX/VMS Ada James Burnell, WVU Comp/Ele Engr
  -- strict thread matches above, loose matches on Subject: below --
1991-08-12 19:39 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!rpi!bu.edu!inmet!stt
1991-08-14  4:54 Mike Feldman

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