comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <nick.roberts@acm.org>
Subject: Re: Discriminant computation problem
Date: Mon, 15 Nov 2004 22:11:16 -0000
Date: 2004-11-15T22:11:16+00:00	[thread overview]
Message-ID: <2vsns5F2p6vbpU1@uni-berlin.de> (raw)
In-Reply-To: NmTld.122$rc.97840@news20.bellglobal.com

"Sandro Magi" <smagi@naasking.homeip.net> wrote in message 
news:NmTld.122$rc.97840@news20.bellglobal.com...

> Just learning Ada and I came across this little issue. I can't perform a
> computation on a discriminant. Suppose I want to create a bit vector type
> that uses an array of Integers as its underlying storage type (just an
> example). I want the user to provide the length of the bit vector in bits,
> but I need to define the upper bound on the Integer array in Integer'Size
> units.
>
> type Int_Array is array (Positive range <>) of Integer;
>
> type Bit_Vector (Max_Bit_Size : Positive) is
> record
> Series : Int_Array (Positive'First .. Max_Bit_Size/Integer'Size);
> end record;
>
> The compiler error gnat produces: "discriminant in constraint must appear
> alone". Is there something I'm doing wrong, or is the above simply not
> possible?

You are not doing anything wrong, as such. It's unfortunately necessary for 
Ada to
restrict the usage of discriminants in component constraints, so that all 
compilers
can be assured of being able to compute the size of a record from its 
discriminants
at any point without having to perform a computation of (potentially) 
unlimited
complexity or which might have side effects.

> I can solve this either by breaking the encapsulation of my type so that 
> the
> user must perform the computation ahead of time (not preferable), or by
> creating a special function which computes the real upper bound. So the
> user would have to instantiate:
>
> bv : Bit_Vector(Real_Upper_Bound(1024)); --1024 bits
>
> Instead of:
>
> bv : Bit_Vector(1024); --1024 bits
>
> Does anyone have a better answer?

I don't have a better answer. I can only additionally suggest that you could 
declare
an access type, and then an allocator function, as well as interrogation 
functions.
For example:

   Bits_Per_Element: constant := Integer'Size; -- say
   Max_Element_Index: constant := 1023; -- say
   Max_Bit_Number: constant := Max_Element_Index*Bits_Per_Element-1;

   subtype Bit_Number is Integer range 0..Max_Bit_Number;

   type Bit_Vector_Element is mod 2**Bits_Per_Element;
   subtype Element_Index is Integer range 0..Max_Element_Index;

   type Element_Array is array (Element_Index range <>) of 
Bit_Vector_Element;

   type Bit_Vector (Max: Element_Index) is
      record
         Max_Bit: Bit_Number;
         Elements: Element_Array(0..Max);
      end record;

   type Dynamic_Bit_Vector is access Bit_Vector;

   function New_Bit_Vector (Max_Bit: in Bit_Number)
         return Dynamic_Bit_Vector;

   ...

   function New_Bit_Vector (Max_Bit: in Bit_Number)
         return Dynamic_Bit_Vector is
      Max: constant Element_Index := Max_Bit/Bits_Per_Element;
   begin
      return new Bit_Vector'(
         Max      => Max,
         Max_Bit  => Max_Bit,
         Elements => (0..Max => 0) );
   end New_Bit_Vector;

I've found this problem to be a pain myself, in the past. You just just have 
to
'program around it'.

-- 
Nick Roberts





  parent reply	other threads:[~2004-11-15 22:11 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-15  1:11 Discriminant computation problem Sandro Magi
2004-11-15  3:24 ` Jim Rogers
2004-11-15 14:02   ` Sandro Magi
2004-11-15 15:16     ` Martin Krischik
2004-11-15 16:02     ` Dmitry A. Kazakov
2004-11-15 22:11 ` Nick Roberts [this message]
2004-11-15 23:25   ` tmoran
2004-11-16 20:00     ` Nick Roberts
2004-11-16 20:14       ` tmoran
replies disabled

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