comp.lang.ada
 help / color / mirror / Atom feed
* index check failure - constraint error
@ 2011-04-01 10:53 tonyg
  2011-04-01 11:07 ` tonyg
                   ` (2 more replies)
  0 siblings, 3 replies; 18+ messages in thread
From: tonyg @ 2011-04-01 10:53 UTC (permalink / raw)



I'm getting a constraint error and I cannot see why...

   function String_To_Integer (The_String : String) return Integer is
      String_Length : Integer := The_String'length;
      Return_Value : Integer := 0;
      subtype Number_Character  is character range '0'..'9';
   begin
	Int_Io.Put(String_Length);
      Ada.Text_IO.Put_Line (The_String);
      if String_Length > 0 then
      for count in 1..String_Length loop
         case The_String(count) is
            when Number_Character =>
               Return_Value := Return_Value +
                 ((Character'pos(The_String(count)) - 48) *
(10**(count-1)));
            when others =>
               raise Conversion_Error_Exception;
         end case;
         end loop;
      else
         Return_Value := 0;
      end if;

      return Return_Value;

   end String_To_Integer;

I checked to see there was a string going in and it had a length. The
actual error was 'index check failure' but as far as I can see
everything is present and correct. Can anyone see what it is ?



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

* Re: index check failure - constraint error
  2011-04-01 10:53 index check failure - constraint error tonyg
@ 2011-04-01 11:07 ` tonyg
  2011-04-01 11:27   ` Alex Mentis
  2011-04-01 11:12 ` Egil Høvik
  2011-04-01 17:39 ` Jeffrey Carter
  2 siblings, 1 reply; 18+ messages in thread
From: tonyg @ 2011-04-01 11:07 UTC (permalink / raw)


On Apr 1, 11:53 am, tonyg <tonytheg...@gmail.com> wrote:
> I'm getting a constraint error and I cannot see why...
>
>    function String_To_Integer (The_String : String) return Integer is
>       String_Length : Integer := The_String'length;
>       Return_Value : Integer := 0;
>       subtype Number_Character  is character range '0'..'9';
>    begin
>         Int_Io.Put(String_Length);
>       Ada.Text_IO.Put_Line (The_String);
>       if String_Length > 0 then
>       for count in 1..String_Length loop
>          case The_String(count) is
>             when Number_Character =>
>                Return_Value := Return_Value +
>                  ((Character'pos(The_String(count)) - 48) *
> (10**(count-1)));
>             when others =>
>                raise Conversion_Error_Exception;
>          end case;
>          end loop;
>       else
>          Return_Value := 0;
>       end if;
>
>       return Return_Value;
>
>    end String_To_Integer;
>
> I checked to see there was a string going in and it had a length. The
> actual error was 'index check failure' but as far as I can see
> everything is present and correct. Can anyone see what it is ?

The index check failure occurred at

  case The_String(count) is

btw



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

* Re: index check failure - constraint error
  2011-04-01 10:53 index check failure - constraint error tonyg
  2011-04-01 11:07 ` tonyg
@ 2011-04-01 11:12 ` Egil Høvik
  2011-04-01 11:17   ` tonyg
  2011-04-01 17:39 ` Jeffrey Carter
  2 siblings, 1 reply; 18+ messages in thread
From: Egil Høvik @ 2011-04-01 11:12 UTC (permalink / raw)




In your loop you assume The_String'First=1,
that may not always be the case, especially if
you call this function on array slices.
Try "for count in The_String'Range loop" instead

-- 
~egilhh



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

* Re: index check failure - constraint error
  2011-04-01 11:12 ` Egil Høvik
@ 2011-04-01 11:17   ` tonyg
  2011-04-01 12:13     ` Ludovic Brenta
  2011-04-01 15:02     ` Adam Beneschan
  0 siblings, 2 replies; 18+ messages in thread
From: tonyg @ 2011-04-01 11:17 UTC (permalink / raw)


On Apr 1, 12:12 pm, Egil Høvik <egilho...@hotmail.com> wrote:
> In your loop you assume The_String'First=1,
> that may not always be the case, especially if
> you call this function on array slices.
> Try "for count in The_String'Range loop" instead
>
> --
> ~egilhh

Worked a treat - still don't understand why the first character in the
string is not 1 though,
Thanks for the help



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

* Re: index check failure - constraint error
  2011-04-01 11:07 ` tonyg
@ 2011-04-01 11:27   ` Alex Mentis
  2011-04-01 11:33     ` Alex Mentis
                       ` (2 more replies)
  0 siblings, 3 replies; 18+ messages in thread
From: Alex Mentis @ 2011-04-01 11:27 UTC (permalink / raw)


tonyg wrote:

> On Apr 1, 11:53�am, tonyg <tonytheg...@gmail.com> wrote:
> > I'm getting a constraint error and I cannot see why...
> > 
> > � �function String_To_Integer (The_String : String) return Integer
> > is � � � String_Length : Integer := The_String'length;
> > � � � Return_Value : Integer := 0;
> > � � � subtype Number_Character �is character range '0'..'9';
> > � �begin
> > � � � � Int_Io.Put(String_Length);
> > � � � Ada.Text_IO.Put_Line (The_String);
> > � � � if String_Length > 0 then
> > � � � for count in 1..String_Length loop
> > � � � � �case The_String(count) is
> > � � � � � � when Number_Character =>
> > � � � � � � � �Return_Value := Return_Value +
> > � � � � � � � � �((Character'pos(The_String(count)) - 48) *
> > (10**(count-1)));
> > � � � � � � when others =>
> > � � � � � � � �raise Conversion_Error_Exception;
> > � � � � �end case;
> > � � � � �end loop;
> > � � � else
> > � � � � �Return_Value := 0;
> > � � � end if;
> > 
> > � � � return Return_Value;
> > 
> > � �end String_To_Integer;
> > 
> > I checked to see there was a string going in and it had a length.
> > The actual error was 'index check failure' but as far as I can see
> > everything is present and correct. Can anyone see what it is ?
> 
> The index check failure occurred at
> 
>   case The_String(count) is
> 
> btw


Any particular reason you aren't using the Ada-provided Get procedure
that converts a String to an Integer? It operates just like the usual
Get procedure, consuming all Integer-valid characters and stopping when
it gets to a non-digit, but you can make it detect and throw your
constraint with something like this:

with Ada.Text_Io, Ada.Integer_Text_Io;
use  Ada.Text_Io;

Procedure Main is
   
   Conversion_Error_Exception : exception;   
   
   Input1 : String := "123";
   Input2 : String := "1E3";
   Input3 : String := "12ab3";
   My_Int : Integer;
   Length : Positive;
   
begin
      
   Ada.Integer_Text_Io.Get (Input1, My_Int, Length);
   if Length /= Input1'Length then
      raise Conversion_Error_Exception;
   else
      Ada.Integer_Text_Io.Put (My_Int, 0);
      New_Line;
   end if;
   
   Ada.Integer_Text_Io.Get (Input2, My_Int, Length);
   if Length /= Input2'Length then
      raise Conversion_Error_Exception;
   else
      Ada.Integer_Text_Io.Put (My_Int, 0);
      New_Line;
   end if;
   
   Ada.Integer_Text_Io.Get (Input3, My_Int, Length);
   if Length /= Input3'Length then
      raise Conversion_Error_Exception;
   else
      Ada.Integer_Text_Io.Put (My_Int, 0);
      New_Line;
   end if;   
   
end Main;



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

* Re: index check failure - constraint error
  2011-04-01 11:27   ` Alex Mentis
@ 2011-04-01 11:33     ` Alex Mentis
  2011-04-01 11:36     ` Alex Mentis
  2011-04-01 12:18     ` Georg Bauhaus
  2 siblings, 0 replies; 18+ messages in thread
From: Alex Mentis @ 2011-04-01 11:33 UTC (permalink / raw)


Alex Mentis wrote:

>  It operates just like the usual
> Get procedure, consuming all Integer-valid characters and stopping
> when it gets to a non-digit

** Edit: stopping when it gets to a character that is not valid for an
Integer (some non-digit characters, such as 'E', are allowed in Integer
representations)




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

* Re: index check failure - constraint error
  2011-04-01 11:27   ` Alex Mentis
  2011-04-01 11:33     ` Alex Mentis
@ 2011-04-01 11:36     ` Alex Mentis
  2011-04-01 12:18     ` Georg Bauhaus
  2 siblings, 0 replies; 18+ messages in thread
From: Alex Mentis @ 2011-04-01 11:36 UTC (permalink / raw)


Alex Mentis wrote:

> you can make it detect and throw your
> constraint

Edit 2: that should be "but you can make it detect and raise your
exception"



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

* Re: index check failure - constraint error
  2011-04-01 11:17   ` tonyg
@ 2011-04-01 12:13     ` Ludovic Brenta
  2011-04-01 15:02     ` Adam Beneschan
  1 sibling, 0 replies; 18+ messages in thread
From: Ludovic Brenta @ 2011-04-01 12:13 UTC (permalink / raw)


tonyg wrote on comp.lang.ada:
> On Apr 1, 12:12 pm, Egil Høvik <egilho...@hotmail.com> wrote:
>
>> In your loop you assume The_String'First=1,
>> that may not always be the case, especially if
>> you call this function on array slices.
>> Try "for count in The_String'Range loop" instead

Well spotted.

> Worked a treat - still don't understand why the first character in the
> string is not 1 though,
> Thanks for the help

   S : constant String := "***123***";
   I : constant Integer := String_To_Integer (S (4 .. 6));

Inside String_To_Integer, The_String'First=4.  S (4 .. 6) is called a
"slice".

--
Ludovic Brenta.





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

* Re: index check failure - constraint error
  2011-04-01 11:27   ` Alex Mentis
  2011-04-01 11:33     ` Alex Mentis
  2011-04-01 11:36     ` Alex Mentis
@ 2011-04-01 12:18     ` Georg Bauhaus
  2011-04-01 22:20       ` tonyg
  2 siblings, 1 reply; 18+ messages in thread
From: Georg Bauhaus @ 2011-04-01 12:18 UTC (permalink / raw)


On 01.04.11 13:27, Alex Mentis wrote:

> Any particular reason you aren't using the Ada-provided Get procedure
> that converts a String to an Integer?

I'll guess that it is has to do with the good idea to
do the exercises from chapter 1 of K&R.



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

* Re: index check failure - constraint error
  2011-04-01 11:17   ` tonyg
  2011-04-01 12:13     ` Ludovic Brenta
@ 2011-04-01 15:02     ` Adam Beneschan
  1 sibling, 0 replies; 18+ messages in thread
From: Adam Beneschan @ 2011-04-01 15:02 UTC (permalink / raw)


On Apr 1, 4:17 am, tonyg <tonytheg...@gmail.com> wrote:
> On Apr 1, 12:12 pm, Egil Høvik <egilho...@hotmail.com> wrote:
>
> > In your loop you assume The_String'First=1,
> > that may not always be the case, especially if
> > you call this function on array slices.
> > Try "for count in The_String'Range loop" instead
>
> > --
> > ~egilhh
>
> Worked a treat - still don't understand why the first character in the
> string is not 1 though,
> Thanks for the help

A String is an array (of Character), and like any array, it can have
any bounds you give it.  (Well, not quite: a non-empty String's bounds
have to be 1 or greater.  But you can declare a string like:

   S : String (10 .. 20);

and if you pass S to this routine, S'First will be 10 and S'Last will
be 20, and S'Length will be 11.  Does that make more sense?  Also,
others have mentioned the ability to take a slice of an array,
including a String.)

                        -- Adam




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

* Re: index check failure - constraint error
  2011-04-01 10:53 index check failure - constraint error tonyg
  2011-04-01 11:07 ` tonyg
  2011-04-01 11:12 ` Egil Høvik
@ 2011-04-01 17:39 ` Jeffrey Carter
  2011-04-01 22:16   ` tonyg
  2 siblings, 1 reply; 18+ messages in thread
From: Jeffrey Carter @ 2011-04-01 17:39 UTC (permalink / raw)


On 04/01/2011 03:53 AM, tonyg wrote:
>
> I'm getting a constraint error and I cannot see why...

In addition to the Get procedures from Ada.Text_IO.*_IO (Integer_IO, Modular_IO, 
Float_IO, and Fixed_IO), there's also <type>'Value to convert a String 
representation to a numeric type. 'Value is less flexible than Get, but is a 
function rather than a procedure. Both will handle values like "1E4".

As others have pointed out, and as I noticed as soon as I looked at your code, 
not all Strings have a lower bound of 1, and that is probably why this raises 
Constraint_Error. If you showed the calling code we could explain it more fully. 
Using 'Range avoids this; that's why it exists.

In addition, I would comment that The_String'Length cannot be negative, so 
String_Length should be Natural rather than Integer to indicate this, and since 
its value never changes, it should be a constant:

String_Length : constant Natural := The_String'Length;

(Personally, I'd call this

function Value (Image : in String) return Integer;

and just use Image'Length.)

There's no need for the "if" checking the length; the loop will execute zero 
times if the String is null, giving a result of zero (which I think is incorrect).

Finally, I think you have a logic error. Given input of "123" with subtype 
String (1 .. 3), it returns

1 * 10 ** 0 + 2 * 10 ** 1 + 3 * 10 ** 2 /= 123.

-- 
Jeff Carter
"Saving keystrokes is the job of the text editor,
not the programming language."
Preben Randhol
64



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

* Re: index check failure - constraint error
  2011-04-01 17:39 ` Jeffrey Carter
@ 2011-04-01 22:16   ` tonyg
  2011-04-01 22:31     ` Jeffrey Carter
  0 siblings, 1 reply; 18+ messages in thread
From: tonyg @ 2011-04-01 22:16 UTC (permalink / raw)


On Apr 1, 6:39 pm, Jeffrey Carter <spam.jrcarter....@spam.not.acm.org>
wrote:
> On 04/01/2011 03:53 AM, tonyg wrote:
>
>
>
> > I'm getting a constraint error and I cannot see why...
>
> In addition to the Get procedures from Ada.Text_IO.*_IO (Integer_IO, Modular_IO,
> Float_IO, and Fixed_IO), there's also <type>'Value to convert a String
> representation to a numeric type. 'Value is less flexible than Get, but is a
> function rather than a procedure. Both will handle values like "1E4".
>
> As others have pointed out, and as I noticed as soon as I looked at your code,
> not all Strings have a lower bound of 1, and that is probably why this raises
> Constraint_Error. If you showed the calling code we could explain it more fully.
> Using 'Range avoids this; that's why it exists.
>
> In addition, I would comment that The_String'Length cannot be negative, so
> String_Length should be Natural rather than Integer to indicate this, and since
> its value never changes, it should be a constant:
>
> String_Length : constant Natural := The_String'Length;
>
> (Personally, I'd call this
>
> function Value (Image : in String) return Integer;
>
> and just use Image'Length.)
>
> There's no need for the "if" checking the length; the loop will execute zero
> times if the String is null, giving a result of zero (which I think is incorrect).
>
> Finally, I think you have a logic error. Given input of "123" with subtype
> String (1 .. 3), it returns
>
> 1 * 10 ** 0 + 2 * 10 ** 1 + 3 * 10 ** 2 /= 123.
>
> --
> Jeff Carter
> "Saving keystrokes is the job of the text editor,
> not the programming language."
> Preben Randhol
> 64

It so so does :) Thanks for that Preben. I hadn't got that far to
discover the godzilla sized error that would of created. Thanks as
well for the help off everyone else.



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

* Re: index check failure - constraint error
  2011-04-01 12:18     ` Georg Bauhaus
@ 2011-04-01 22:20       ` tonyg
  2011-04-01 23:37         ` Ludovic Brenta
  0 siblings, 1 reply; 18+ messages in thread
From: tonyg @ 2011-04-01 22:20 UTC (permalink / raw)


On Apr 1, 1:18 pm, Georg Bauhaus <rm.dash-bauh...@futureapps.de>
wrote:
> On 01.04.11 13:27, Alex Mentis wrote:
>
> > Any particular reason you aren't using the Ada-provided Get procedure
> > that converts a String to an Integer?
>
> I'll guess that it is has to do with the good idea to
> do the exercises from chapter 1 of K&R.

Whats K$R? sounds like I need it :)



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

* Re: index check failure - constraint error
  2011-04-01 22:16   ` tonyg
@ 2011-04-01 22:31     ` Jeffrey Carter
  2011-04-01 22:49       ` Robert A Duff
  0 siblings, 1 reply; 18+ messages in thread
From: Jeffrey Carter @ 2011-04-01 22:31 UTC (permalink / raw)


On 04/01/2011 03:16 PM, tonyg wrote:
>
> It so so does :) Thanks for that Preben. I hadn't got that far to
> discover the godzilla sized error that would of created. Thanks as
> well for the help off everyone else.

Preben Randhol is the person I'm quoting in my signature, not me.

The idea that Strings always have a lower bound of 1 is quite common among 
beginners. I recall noticing it causing others problems when I first learned Ada 
in 1984. For some reason I never made that assumption.

In SPARK all Strings do have a lower bound of 1.

-- 
Jeff Carter
"Saving keystrokes is the job of the text editor,
not the programming language."
Preben Randhol
64



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

* Re: index check failure - constraint error
  2011-04-01 22:31     ` Jeffrey Carter
@ 2011-04-01 22:49       ` Robert A Duff
  2011-04-01 23:00         ` Adam Beneschan
  0 siblings, 1 reply; 18+ messages in thread
From: Robert A Duff @ 2011-04-01 22:49 UTC (permalink / raw)


Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org> writes:

> The idea that Strings always have a lower bound of 1 is quite common
> among beginners.

Whereas experts understand that it OUGHT to be true that
"Strings always have a lower bound of 1", but it isn't true.
It's a design flaw in the Ada language.

> In SPARK all Strings do have a lower bound of 1.

As they should.  It really makes things simpler.
And (could be) more efficient.

- Bob



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

* Re: index check failure - constraint error
  2011-04-01 22:49       ` Robert A Duff
@ 2011-04-01 23:00         ` Adam Beneschan
  2011-04-01 23:29           ` Robert A Duff
  0 siblings, 1 reply; 18+ messages in thread
From: Adam Beneschan @ 2011-04-01 23:00 UTC (permalink / raw)


On Apr 1, 3:49 pm, Robert A Duff <bobd...@shell01.TheWorld.com> wrote:
> Jeffrey Carter <spam.jrcarter....@spam.not.acm.org> writes:
> > The idea that Strings always have a lower bound of 1 is quite common
> > among beginners.
>
> Whereas experts understand that it OUGHT to be true that
> "Strings always have a lower bound of 1", but it isn't true.
> It's a design flaw in the Ada language.
>
> > In SPARK all Strings do have a lower bound of 1.
>
> As they should.  It really makes things simpler.
> And (could be) more efficient.

I dunno.  I kind of like it that if I'm parsing a string, and I call

     Next_Item := Ada.Strings.Fixed.Index_Non_Blank (S
(Current_Index .. S'Last));

then Next_Item is the index of the actual character in S that I need,
and I don't need to add Current_Index to it and then think about
whether I need to add 1 or subtract 1 from the sum, as I'd have to do
something like that if Index_Non_Blank saw its Source parameter as a
string with a lower bound of 1.

                          -- Adam



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

* Re: index check failure - constraint error
  2011-04-01 23:00         ` Adam Beneschan
@ 2011-04-01 23:29           ` Robert A Duff
  0 siblings, 0 replies; 18+ messages in thread
From: Robert A Duff @ 2011-04-01 23:29 UTC (permalink / raw)


Adam Beneschan <adam@irvine.com> writes:

> On Apr 1, 3:49�pm, Robert A Duff <bobd...@shell01.TheWorld.com> wrote:
>> Jeffrey Carter <spam.jrcarter....@spam.not.acm.org> writes:
>> > The idea that Strings always have a lower bound of 1 is quite common
>> > among beginners.
>>
>> Whereas experts understand that it OUGHT to be true that
>> "Strings always have a lower bound of 1", but it isn't true.
>> It's a design flaw in the Ada language.
>>
>> > In SPARK all Strings do have a lower bound of 1.
>>
>> As they should. �It really makes things simpler.
>> And (could be) more efficient.
>
> I dunno.  I kind of like it that if I'm parsing a string, and I call
>
>      Next_Item := Ada.Strings.Fixed.Index_Non_Blank (S
> (Current_Index .. S'Last));
>
> then Next_Item is the index of the actual character in S that I need,
> and I don't need to add Current_Index to it and then think about
> whether I need to add 1 or subtract 1 from the sum, as I'd have to do
> something like that if Index_Non_Blank saw its Source parameter as a
> string with a lower bound of 1.

Good point.  But I think that's what cursors are for.
I still think indices into Strings ought to be 1-based.

- Bob



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

* Re: index check failure - constraint error
  2011-04-01 22:20       ` tonyg
@ 2011-04-01 23:37         ` Ludovic Brenta
  0 siblings, 0 replies; 18+ messages in thread
From: Ludovic Brenta @ 2011-04-01 23:37 UTC (permalink / raw)


tonyg writes on comp.lang.ada:
> On Apr 1, 1:18 pm, Georg Bauhaus <rm.dash-bauh...@futureapps.de>
> wrote:
>> On 01.04.11 13:27, Alex Mentis wrote:
>>
>> > Any particular reason you aren't using the Ada-provided Get procedure
>> > that converts a String to an Integer?
>>
>> I'll guess that it is has to do with the good idea to
>> do the exercises from chapter 1 of K&R.
>
> Whats K$R? sounds like I need it :)

No you don't :)

Brian Kernighan and Dennis Ritchie were the authors of "The C
Programming Language"...

-- 
Ludovic Brenta.



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

end of thread, other threads:[~2011-04-01 23:37 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-01 10:53 index check failure - constraint error tonyg
2011-04-01 11:07 ` tonyg
2011-04-01 11:27   ` Alex Mentis
2011-04-01 11:33     ` Alex Mentis
2011-04-01 11:36     ` Alex Mentis
2011-04-01 12:18     ` Georg Bauhaus
2011-04-01 22:20       ` tonyg
2011-04-01 23:37         ` Ludovic Brenta
2011-04-01 11:12 ` Egil Høvik
2011-04-01 11:17   ` tonyg
2011-04-01 12:13     ` Ludovic Brenta
2011-04-01 15:02     ` Adam Beneschan
2011-04-01 17:39 ` Jeffrey Carter
2011-04-01 22:16   ` tonyg
2011-04-01 22:31     ` Jeffrey Carter
2011-04-01 22:49       ` Robert A Duff
2011-04-01 23:00         ` Adam Beneschan
2011-04-01 23:29           ` Robert A Duff

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