comp.lang.ada
 help / color / mirror / Atom feed
* Need help for constrained type
@ 2014-02-06 16:13 Gerd
  2014-02-06 17:30 ` Niklas Holsti
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Gerd @ 2014-02-06 16:13 UTC (permalink / raw)


Hello,

can someone help me? 

This is my problem:

I have a record containing a constrainted array. I can write it like this:


  type Mem_Array is array (Natural range <>) of Short_Integer;

  type Memory_Block (len : Natural) is
    record
      Start_Address : Mem_Addr;
      Block_Size : Natural;
      Usage_Info : Mem_Array (1..len);
    end record;

  mem : Memory_Block (2**16);  --  for example


But i would like the indices of Usage_Info running from 0..len - 1 (C-style). But when I write

  type Memory_Block (len : Natural) is
    record
      Start_Address : Mem_Addr;
      Block_Size : Natural;
      Usage_Info : Mem_Array (0..len-1);
    end record;

the compiler tells me that this is not allowed.

Is it possible to allocate "len" units, but address them from 0 .. len - 1?

How can I do this?

Thanks.


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

* Re: Need help for constrained type
  2014-02-06 16:13 Need help for constrained type Gerd
@ 2014-02-06 17:30 ` Niklas Holsti
  2014-02-06 18:03   ` adambeneschan
  2014-02-06 17:37 ` G.B.
  2014-02-07  0:04 ` Jeffrey Carter
  2 siblings, 1 reply; 9+ messages in thread
From: Niklas Holsti @ 2014-02-06 17:30 UTC (permalink / raw)


On 14-02-06 18:13 , Gerd wrote:

>   type Memory_Block (len : Natural) is
>     record
>       Start_Address : Mem_Addr;
>       Block_Size : Natural;
>       Usage_Info : Mem_Array (0..len-1);
>     end record;
> 
> the compiler tells me that this is not allowed.
> 
> Is it possible to allocate "len" units, but address them from 0 .. len - 1?
> 
> How can I do this?

This restriction sometimes annoys me too. You can pass a record
discriminant to a component, but you cannot alter it in any way, such as
by adding or subtracting something. So the only possibility is to change
the meaning of the discriminant to be "len - 1":

  type Memory_Block (last : Natural) is
    record
      Start_Address : Mem_Addr;
      Block_Size : Natural;
      Usage_Info : Mem_Array (0..last);
    end record;

(If you want to allow an empty Usage_Info, indexed 0..-1 for example,
you must of course declare Memory_Block.last using some range which
includes negative numbers, for example Integer.)

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
      .      @       .

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

* Re: Need help for constrained type
  2014-02-06 16:13 Need help for constrained type Gerd
  2014-02-06 17:30 ` Niklas Holsti
@ 2014-02-06 17:37 ` G.B.
  2014-02-07 14:11   ` Mike H
  2014-02-07  0:04 ` Jeffrey Carter
  2 siblings, 1 reply; 9+ messages in thread
From: G.B. @ 2014-02-06 17:37 UTC (permalink / raw)


On 06.02.14 17:13, Gerd wrote:
> Is it possible to allocate "len" units, but address them from 0 .. len - 1?
>
> How can I do this?

One approximation can make use of generics. The immediate
mention of the Len with the object declaration will not be
present. But otherwise the approximation preserves the
"interface" of Memory_Block, except that the .Len component
is no longer constant, and not guaranteed to be in sync with
the length of .Usage_Info, if someone messes with it.

    generic
       Len : Natural;
    package Memory_Blocks is

       C_Style_Len : constant Integer := Len - 1;

       type Memory_Block is
          record
             Len : Natural := Memory_Blocks.Len;
             Start_Address : Mem_Addr;
             Block_Size : Natural;
             Usage_Info : Mem_Array (0..C_Style_Len);
          end record;

    end Memory_Blocks;

    package C_Style_Mem is new Memory_Blocks (2**16);

    mem : C_Style_Mem.Memory_Block;


(A different approach might instead choose a function,
C_Length (a renaming of Integer'Pred), such that

    mem : Memory_Block (C_Length (2**16));

will in effect let the index of Mem_Array be 0 .. Len.)


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

* Re: Need help for constrained type
  2014-02-06 17:30 ` Niklas Holsti
@ 2014-02-06 18:03   ` adambeneschan
  2014-02-06 18:37     ` Niklas Holsti
  0 siblings, 1 reply; 9+ messages in thread
From: adambeneschan @ 2014-02-06 18:03 UTC (permalink / raw)


On Thursday, February 6, 2014 9:30:32 AM UTC-8, Niklas Holsti wrote:

> (If you want to allow an empty Usage_Info, indexed 0..-1 for example,
> you must of course declare Memory_Block.last using some range which
> includes negative numbers, for example Integer.)

That actually isn't true.  The bounds of an empty range do *not* need to be within the subrange.  Here, Natural is a subrange 0 .. Integer'Last, but Integer is the base type, and as long as the bounds belong to the base type, it's OK.  (Another example: String is declared as array (Positive range <>) of Character, but String(1..N) is fine even if N=0, and I'm sure we've all done that.)

                                -- Adam

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

* Re: Need help for constrained type
  2014-02-06 18:03   ` adambeneschan
@ 2014-02-06 18:37     ` Niklas Holsti
  2014-02-06 18:57       ` adambeneschan
  0 siblings, 1 reply; 9+ messages in thread
From: Niklas Holsti @ 2014-02-06 18:37 UTC (permalink / raw)


On 14-02-06 20:03 , adambeneschan@gmail.com wrote:
> On Thursday, February 6, 2014 9:30:32 AM UTC-8, Niklas Holsti wrote:
> 
>> (If you want to allow an empty Usage_Info, indexed 0..-1 for
>> example, you must of course declare Memory_Block.last using some
>> range which includes negative numbers, for example Integer.)
> 
> That actually isn't true.

It is here, I think, because the upper bound comes from a discriminant,
where subtypes are checked more strictly. And the lower bound is fixed
to zero, so only a negative upper bound makes an empty range.

> The bounds of an empty range do *not* need
> to be within the subrange.  Here, Natural is a subrange 0 ..
> Integer'Last, but Integer is the base type, and as long as the bounds
> belong to the base type, it's OK.

Agreed. But if I have

  type Memory_Block (last : Natural) is
    record
      ...
      Usage_Info : Mem_Array (0..last);
    end record;

then I'm pretty sure that the declaration

   M : Memory_Block (last => -1);

will be rejected, because -1 is not in Natural. GNAT says:

mb.adb:15:30: warning: static value out of range of type "Standard.Natural"
mb.adb:15:30: warning: "Constraint_Error" will be raised at run time

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
      .      @       .

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

* Re: Need help for constrained type
  2014-02-06 18:37     ` Niklas Holsti
@ 2014-02-06 18:57       ` adambeneschan
  0 siblings, 0 replies; 9+ messages in thread
From: adambeneschan @ 2014-02-06 18:57 UTC (permalink / raw)


On Thursday, February 6, 2014 10:37:30 AM UTC-8, Niklas Holsti wrote:

> >> (If you want to allow an empty Usage_Info, indexed 0..-1 for
> >> example, you must of course declare Memory_Block.last using some
> >> range which includes negative numbers, for example Integer.)
> 
> > That actually isn't true. 
> 
> It is here, I think, because the upper bound comes from a discriminant,
> where subtypes are checked more strictly.

Sorry, I misunderstood your point.  Yes, you'd need to make sure the discriminant subtype can handle the upper bound.  But the array *index* subtype can still be "Natural range <>".  Now that I rechecked, I see that I just didn't read your previous post carefully.

                                 -- Adam

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

* Re: Need help for constrained type
  2014-02-06 16:13 Need help for constrained type Gerd
  2014-02-06 17:30 ` Niklas Holsti
  2014-02-06 17:37 ` G.B.
@ 2014-02-07  0:04 ` Jeffrey Carter
  2 siblings, 0 replies; 9+ messages in thread
From: Jeffrey Carter @ 2014-02-07  0:04 UTC (permalink / raw)


On 02/06/2014 09:13 AM, Gerd wrote:
>
> But i would like the indices of Usage_Info running from 0..len - 1 (C-style). But when I write
>
>    type Memory_Block (len : Natural) is
>      record
>        Start_Address : Mem_Addr;
>        Block_Size : Natural;
>        Usage_Info : Mem_Array (0..len-1);
>      end record;

That's what happens when you try to do C in Ada.

Technically, you can't use a discriminant to determine the length of an array, 
only to determine a bound. The 2 are the same only when the lower bound is 1 
(the Ada way). Since what you're supplying is not the length, but the 'Last, so 
you should name it that.

-- 
Jeff Carter
"When Bell Labs were invited to evaluate C against the DoD requirements
[for Ada], they said that there was no chance of C meeting the
requirements of readability, safety, etc. for which we were striving,
and that it should not even be on the list of evaluated languages."
William Whitaker
116


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

* Re: Need help for constrained type
  2014-02-06 17:37 ` G.B.
@ 2014-02-07 14:11   ` Mike H
  2014-02-10 11:40     ` G.B.
  0 siblings, 1 reply; 9+ messages in thread
From: Mike H @ 2014-02-07 14:11 UTC (permalink / raw)


In message <52f3c83c$0$6559$9b4e6d93@newsspool4.arcor-online.net>, G.B. 
<rm-dash-bau-haus@dash.futureapps.de> writes
>One approximation can make use of generics. The immediate
>mention of the Len with the object declaration will not be
>present. But otherwise the approximation preserves the
>"interface" of Memory_Block, except that the .Len component
>is no longer constant, and not guaranteed to be in sync with
>the length of .Usage_Info, if someone messes with it.
Cannot sync be guaranteed by placing the relevant bits and pieces as 
private types within a package such that they may only be manipulated by 
functions and procedures provided by that package? Or have I 
misunderstood something?

B.T.W for those who have come to Ada from other places it is not always 
easy to feel comfortable with the higher levels of abstraction that the 
language supplies. One consequence is that not only should one not be 
surprised when there seems to be little correlation between the number 
of lines of source code and amount of code generated, one should also 
not be surprised when any such correlation is negative.

-- 
The thing I like best about the Internet is that no one
knows that, in reality, I am just another old dog!
Mike


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

* Re: Need help for constrained type
  2014-02-07 14:11   ` Mike H
@ 2014-02-10 11:40     ` G.B.
  0 siblings, 0 replies; 9+ messages in thread
From: G.B. @ 2014-02-10 11:40 UTC (permalink / raw)


On 07.02.14 15:11, Mike H wrote:
> In message <52f3c83c$0$6559$9b4e6d93@newsspool4.arcor-online.net>, G.B.
> <rm-dash-bau-haus@dash.futureapps.de> writes
>> One approximation can make use of generics. The immediate
>> mention of the Len with the object declaration will not be
>> present. But otherwise the approximation preserves the
>> "interface" of Memory_Block, except that the .Len component
>> is no longer constant, and not guaranteed to be in sync with
>> the length of .Usage_Info, if someone messes with it.
> Cannot sync be guaranteed by placing the relevant bits and pieces as
> private types within a package such that they may only be manipulated by
> functions and procedures provided by that package? Or have I
> misunderstood something?

I suppose the .Len component could be removed from the record
entirely, and be replaced with a constant function Len .IFF.
the record does not need to be some skinny collection of names
to be used without functions, taking pointers and overwriting
memory locations with index values/offset values.
(Used from both the Ada side and from the C side.)



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

end of thread, other threads:[~2014-02-10 11:40 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-06 16:13 Need help for constrained type Gerd
2014-02-06 17:30 ` Niklas Holsti
2014-02-06 18:03   ` adambeneschan
2014-02-06 18:37     ` Niklas Holsti
2014-02-06 18:57       ` adambeneschan
2014-02-06 17:37 ` G.B.
2014-02-07 14:11   ` Mike H
2014-02-10 11:40     ` G.B.
2014-02-07  0:04 ` Jeffrey Carter

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