comp.lang.ada
 help / color / mirror / Atom feed
* getting back to null range
@ 2009-07-09 23:10 anon
  2009-07-12 18:20 ` AdaMagica
  0 siblings, 1 reply; 2+ messages in thread
From: anon @ 2009-07-09 23:10 UTC (permalink / raw)




with Ada.Integer_Text_IO ;
with Ada.Text_IO ;

use Ada.Integer_Text_IO ;
use Ada.Text_IO ;

procedure t is

  -- Display String values, with a title

  procedure Put ( T : String ; B : String ) is

    begin
      Put ( T ) ;
      Put ( "'( " ) ;
      Put ( B'First ) ;
      Put ( " .. " ) ;
      Put ( B'Last ) ;
      Put ( " ) => " ) ;
      Put ( B'length ) ;
      Put ( " => " ) ;
      Put_Line ( B ) ;
    end Put ;


  N : String := "" ;
  S : String := ( "123456789" ) ;

  --  From GNAT "Feature-316" file.
  --
  --    NF-316-B412-009 Warning on values for null ranges
  --
  --      If the compiler knows that a range is null, then it knows 
  --      that no value can conform to the range and that 
  --      Constraint_Error will be raised. A warning is now generated 
  --      in this situation.

  A : Positive range -5 .. -7 ; -- Value is a nul range
                                -- Creates the compiler warning message

  --
  -- A null string
  --
  S_A : String := ( -5 .. -7 => 'Z' ) ;


  subtype B is Positive range 9 .. 7 ;
  subtype C is Positive range 7 .. 5 ;

  S_R : String := ( C => 'Y' ) ;


begin
  Put ( "N", N ) ;
  Put ( "S", S ) ;
  Put ( "null S", S ( -5 ..-7 ) ) ;
  New_Line ;

  Put ( "null S_A", S_A ) ;
  if N = S_A then 
    Put_Line ( "N equal S_A" ) ;
  else
    Put_Line ( "N not equal S_A" ) ;
  end if ;

  declare
    --  Without these pragma statements the following declaration 
    --  statement will compile but generates an C_E at run time
    pragma Suppress ( Index_Check ) ;    
    pragma Suppress ( Range_Check ) ;

    S_B : String := ( A => 'Z' ) ; -- should be equal to S_A 

  begin
    Put ( "null S_B", S_B ) ;  
    if N = S_B then                -- Gives,  N not equal S_B
      Put_Line ( "N equal S_B" ) ;
    else
      Put_Line ( "N not equal S_B" ) ;
    end if ;

    if S_A = S_B then              -- Gives, S_A not equal S_B
      Put_Line ( "S_A equal S_B" ) ;
    else
      Put_Line ( "S_A not equal S_B" ) ;
    end if ;
  end ;

  Put ( "null S_R ", S_R ) ;
  Put ( "null S_R ", S_R ( B ) ) ;

  if S_R = S_R ( B ) then 
    Put_Line ( "S_R equal S_R ( B )" ) ;
  else
    Put_Line ( "S_R not equal S_R ( B )" ) ;
  end if ;

  begin
    --
    -- will result in a C_E when ( Index - 5 ) is no longer Positive 
    -- aka ( Index - 5 ) < 1 , It seams that the upper bound can be 
    -- a legal positive or not
    --
    Put_Line ( "Scanning an String:  Upper bound Postive" ) ;
    for Index in reverse S'Range loop
      Put ( "Reverse S", S ( ( Index - 5 ).. 6 ) ) ;
    end loop ;
  exception 
    when Constraint_Error =>
     Put_Line ( "Constraint_Error: Lower bound < 1" ) ;
     null ;
  end ;
  New_Line ;

  -- checking when upper bound is not a legal member of the script type
  -- Should result in an error at the same index value as previous 
  -- block statements. but does not!
  begin
    Put_Line ( "Scanning with Upper bound swaping Negative" ) ;
    for Index in reverse S'Range loop
      begin
        Put ( "Neg S", S ( ( Index - 5 ) .. ( Index - 11 ) ) ) ;
      exception
        when Constraint_Error =>
          Put_Line ( "Constraint_Error: " & 
                   Integer'Image ( Index ) & " ( " &
                   Integer'Image ( Index - 5 ) & " .. " &
                   Integer'Image ( Index - 11 ) & " )" ) ;
          exit ;
      end ;
      --
      begin
        Put ( "    S", S ( ( Index - 5 ) .. ( Index - 4 ) ) ) ;
      exception
        when Constraint_Error =>
          Put_Line ( "Constraint_Error: " &
                   Integer'Image ( Index ) & " ( " &
                   Integer'Image ( Index - 5 ) & " .. " &
                   Integer'Image ( Index - 4 ) & " )" ) ;
          exit ;
      end ;
    end loop ;
  end ;
  New_line ;
end t ;




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

* Re: getting back to null range
  2009-07-09 23:10 getting back to null range anon
@ 2009-07-12 18:20 ` AdaMagica
  0 siblings, 0 replies; 2+ messages in thread
From: AdaMagica @ 2009-07-12 18:20 UTC (permalink / raw)


This is an excerpt (and slightly modified) of what anon proposed as a
test program.
----
with Ada.Integer_Text_IO ;
with Ada.Text_IO ;

use Ada.Integer_Text_IO ;
use Ada.Text_IO ;

procedure t is

  -- Display String values, with a title

  procedure Put ( T : String ; B : String ) is

    begin
      Put ( T ) ;
      Put ( "'( " ) ;
      Put ( B'First ) ;
      Put ( " .. " ) ;
      Put ( B'Last ) ;
      Put ( " ) => " ) ;
      Put ( B ) ;
      Put ( ", Length => " ) ;
      Put ( B'length ) ;
      New_Line;
    end Put ;

  A : Positive range -5 .. -7 ; -- Value is a nul range
                                -- Creates the compiler warning
message

begin

  declare
    --  Without these pragma statements the following declaration
    --  statement will compile but generates an C_E at run time
--      pragma Suppress ( Index_Check ) ;
--      pragma Suppress ( Range_Check ) ;

    S_B : String := ( A => 'Z' ) ; -- should be equal to S_A

  begin
    Put (A);  Put (A'Size);  New_Line;
    Put ( "null S_B", S_B ) ;
  end ;

end t ;
---
I do not understand what (s)he want's to prove with this piece of
code.

The declaration of A produces a warning, because it is read but never
assigned. His (her) assertion that the value is a null range is wrong.
A value is *never* a range. The subtype of A is a null range so that
one cannot assign any value to A. But nevertheless A exists and has a
size and an address and there is some random sequence of bits at that
address which can be interpreted as an integer.

My result is:
    4201563         32
null S_B'(     4201563 ..     4201563 ) => Z, Length =>           1

anon got an exception without pragma Suppress because in his case A
had the sequence of bit that interpreted as integer was 0, which is
not in range of Positive. So the exception is appropriate. If (s)he
suppresses the check, the program is erroneous if the check would have
failed. So what does (s)he prove with an erroneous program?

In my case, the value is in Positive, so no exception.

And of course S_B is not a null string because it is initialized with
an aggregate that has a value (albeit a random one).

For any integers A, B such that the following is legal

  X: String := (A..B => something);  -- A..B is a range (might be a
null range)

is not the same as

  X: String := (A => something);  -- an aggregate with one component
(if A in Positive)



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

end of thread, other threads:[~2009-07-12 18:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-09 23:10 getting back to null range anon
2009-07-12 18:20 ` AdaMagica

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