Need help with Ada95 language aspect 
Author Message
 Need help with Ada95 language aspect



Quote:
>  T1 : T := Initialize (No_Values => 3);
>  T2 : T;    -- GNAT won't allow this which I did NOT expect
>             -- the error message seems to indicate I must declare it as below

From what you said, it looks like a compiler bug.  You should send a bug
report, with full sources and the error message printed by gnat, to

You should also check what version you're using.  If it's old, try using
the latest version (2.04, or wait a few days, and get 2.05, which should
be available soon).

Quote:
>  T3 : T(5); -- GNAT won't allow this which I would expect

Right.

- Bob



Sat, 01 Nov 1997 03:00:00 GMT  
 Need help with Ada95 language aspect
I'm really puzzled by the following aspect of Ada95. The Ada95 Rationale
states on page II-32 that another '95 improvement is that a private type
can now have as its full type a discriminated type with defaults, i.e. one
need not show that a private type is implemented as a discriminated type.
I figured this is just what I want for the package I'm working on and so
I tried it. Below is a short test code that I compiled with GNAT (v2.04)
and I'm puzzled by the *error* messages.

So the question is: Am I misinterpreting this aspect or is GNAT wrong?

The behaviour I see is just like for a type with unknown discriminants,
which is NOT what I want. I want a type that a package client can only
declare as an unconstrained type, i.e. a private type that won't allow
the declaration of a constrained (by discriminants) version of it.

_________________________________________________________________________
with Text_IO;

procedure Simple_Test is

  type Integer_Vector is array (Integer range <>) of Integer;

  package Test_Pkg is

     type T is private;  -- partial view w/o discriminants

     function Initialize (No_Values : Positive) return T;

     procedure Initialize (Object : in out T; Values : in Integer_Vector);

     procedure Display (Object : in T);

  private
     subtype Index_Type is Integer range 0 .. 10;

     type T (No_Values : Index_Type := 0) is
       record
          Values : Integer_Vector(1 .. No_Values);
       end record;
  end Test_Pkg;

  package body Test_Pkg is
     function Initialize (No_Values : Positive) return T
       is
          Result : T(No_Values);
       begin
          Result.Values := (others => 5);
          return Result;
       end;

     procedure Initialize (Object : in out T; Values : in Integer_Vector)
       is
          Result : T(Values'Length);
       begin
          Object := Result;
          Object.Values := Values;
       end;

     procedure Display (Object : in T)
       is
          use Text_IO;
       begin
          Put("The values are: ");
          for i in Object.Values'Range loop
              Put (Integer'Image(Object.Values(i)));
          end loop;
          New_Line;
       end;
  end Test_Pkg;

  use Test_Pkg;

  procedure Display_Type (Object : T; Object_Name : String)
    is
       use Text_IO;
    begin
       if T'Constrained then
          Put_Line(Object_Name & " is a constrained type.");
       else
          Put_Line(Object_Name & " is an unconstrained type.");
       end if;
    end;

  T1 : T := Initialize (No_Values => 3);
  T2 : T;    -- GNAT won't allow this which I did NOT expect
             -- the error message seems to indicate I must declare it as below
  T3 : T(5); -- GNAT won't allow this which I would expect

begin

  Initialize (T2, Values => (2,4,6,8));

  Display (T1);
  Display (T2);

  Display_Type (T1, "T1");
  Display_Type (T2, "T2");

end;
_________________________________________________________________________
Christian Jaensch              Institute for Flight Mechanics and Control
Consultant                                        University of Stuttgart
Voice:(+49) 881-64947   FAX:(+49) 881-637444              Forststrasse 86

_________________________________________________________________________



Sat, 01 Nov 1997 03:00:00 GMT  
 Need help with Ada95 language aspect
in fact this error (the undefined No_Values) bug reported here, turns
out NOT to be a bug in GNAT, but a bug in the original program (implicitly
inherited disciminants used to improperly constrain components). The
GNAT error message was not great, but not terrible either!


Sun, 02 Nov 1997 03:00:00 GMT  
 Need help with Ada95 language aspect
: I'm really puzzled by the following aspect of Ada95. The Ada95 Rationale
: states on page II-32 that another '95 improvement is that a private type
: can now have as its full type a discriminated type with defaults, i.e. one
: need not show that a private type is implemented as a discriminated type.
: I figured this is just what I want for the package I'm working on and so
: I tried it. Below is a short test code that I compiled with GNAT (v2.04)
: and I'm puzzled by the *error* messages.

: So the question is: Am I misinterpreting this aspect or is GNAT wrong?

GNAT has a bug.

After commenting out the line that declares T3 in your test
program (which is illegal as written), I was able to compile,
link, and run your program (under a simulator for a 24-bit
embedded target!).  Here are the results:

    SIM9X 1.4+ (c) 1994 Intermetrics, Inc.
    -- Patriot EWCC simulator Ada program support script
    -- INIT_EWCC.SIM $Revision: 1.85 $
    sim> l "privdisc.ab"
    -- Interval Timer enabled.
    -- Program privdisc.ab successfully loaded.
    -- Type 'g' or 'c' to start
    sim> g
    -- Starting stand-alone program privdisc.ab
    --SPO | The values are:  5 5 5 |
    --SPO | The values are:  2 4 6 8 |
    --SPO | T1 is a constrained type. |
    --SPO | T2 is a constrained type. |
    -- Interval Timer disabled.
    Program privdisc.ab terminated normally
    Warning: All processors Halted
    sim>

The lines bracketed by "--SPO | " ... " |" are "simulated program output"
and correspond to what Text_IO generated.

Note that both T1 and T2 were listed as "constrained," which is
correct from your "view" outside the package Test_Pkg.
In fact, your use of T'Constrained is a bit misleading, as
its value is independent of the parameter passed to Display_Type.
Presumably you originally tried to use the attribute Object'Constrained,
but this attribute is only defined for objects that are *visibly*
discriminated.  T'Constrained returns True because T is a
nonformal private type with no visible discriminants (see J.4(2)).

If Display_Type were inside the body of package "Test_Pkg", then
Object'Constrained would be legal (though T'Constrained would
*not* be legal, since T is not private there), and both T1
and T2 would appear unconstrained.


Intermetrics, Inc.

-------------------------------------
[Program follows for those who missed the original]

: The behaviour I see is just like for a type with unknown discriminants,
: which is NOT what I want. I want a type that a package client can only
: declare as an unconstrained type, i.e. a private type that won't allow
: the declaration of a constrained (by discriminants) version of it.

: _________________________________________________________________________
: with Text_IO;

: procedure Simple_Test is

:   type Integer_Vector is array (Integer range <>) of Integer;

:   package Test_Pkg is

:      type T is private;  -- partial view w/o discriminants

:      function Initialize (No_Values : Positive) return T;

:      procedure Initialize (Object : in out T; Values : in Integer_Vector);

:      procedure Display (Object : in T);

:   private
:      subtype Index_Type is Integer range 0 .. 10;

:      type T (No_Values : Index_Type := 0) is
:        record
:           Values : Integer_Vector(1 .. No_Values);
:        end record;
:   end Test_Pkg;

:   package body Test_Pkg is
:      function Initialize (No_Values : Positive) return T
:        is
:           Result : T(No_Values);
:        begin
:           Result.Values := (others => 5);
:           return Result;
:        end;

:      procedure Initialize (Object : in out T; Values : in Integer_Vector)
:        is
:           Result : T(Values'Length);
:        begin
:           Object := Result;
:           Object.Values := Values;
:        end;

:      procedure Display (Object : in T)
:        is
:           use Text_IO;
:        begin
:           Put("The values are: ");
:           for i in Object.Values'Range loop
:               Put (Integer'Image(Object.Values(i)));
:           end loop;
:           New_Line;
:        end;
:   end Test_Pkg;

:   use Test_Pkg;

:   procedure Display_Type (Object : T; Object_Name : String)
:     is
:        use Text_IO;
:     begin
:        if T'Constrained then
:           Put_Line(Object_Name & " is a constrained type.");
:        else
:           Put_Line(Object_Name & " is an unconstrained type.");
:        end if;
:     end;

:   T1 : T := Initialize (No_Values => 3);
:   T2 : T;    -- GNAT won't allow this which I did NOT expect
:              -- the error message seems to indicate I must declare it as below
:   T3 : T(5); -- GNAT won't allow this which I would expect
*** The above line was commented out to get this to compile ***

: begin

:   Initialize (T2, Values => (2,4,6,8));

:   Display (T1);
:   Display (T2);

:   Display_Type (T1, "T1");
:   Display_Type (T2, "T2");

: end;
: _________________________________________________________________________
: Christian Jaensch              Institute for Flight Mechanics and Control
: Consultant                                        University of Stuttgart
: Voice:(+49) 881-64947   FAX:(+49) 881-637444              Forststrasse 86

: _________________________________________________________________________



Mon, 03 Nov 1997 03:00:00 GMT  
 Need help with Ada95 language aspect

Quote:

> in fact this error (the undefined No_Values) bug reported here, turns
> out NOT to be a bug in GNAT, but a bug in the original program (implicitly
> inherited disciminants used to improperly constrain components). The
> GNAT error message was not great, but not terrible either!

Sorry to correct you on this Robert, but your statement above has *nothing*
to do with the question I posted in this thread to CLA. Your statements are
referring to a *GNAT bug report* on inherited discriminants which stirred
up quite a bit of discussion. Obviously this issue (inherited discriminants)
is not as simple and straightforward as I originally thought.

For interested CLA readers, the conclusion of the above mentioned discussion
(thanks to clarifications of Bob Duff) is that one *cannot* use implicitly
inherited discriminants for constraining record components of derived types.
For example,

type A (N : Positive) is tagged
        record
                A_Constrained_Component : String (1 .. N);
        end record;

type B is new A with
        record
                Another_Component : String (1 .. N); -- this is *illegal*
        end record;                          ^

N *is* an implicitly inherited discriminant of B but *cannot* be used to
constrain any record component of B.

instead one could write:

type B (M : Positive) is new A(N => M) with
        record
                Another_Component : String (1 .. M);
        end record;

or (once supported by GNAT)

type B (N : Positive) is new A(N => N) with
        record
                Another_Component : String (1 .. N);
        end record;
_________________________________________________________________________
Christian Jaensch              Institute for Flight Mechanics and Control
Consultant                                        University of Stuttgart
Voice:(+49) 881-64947   FAX:(+49) 881-637444              Forststrasse 86

        Common sense is the best distributed commodity in the world,
        for every man is convinced that he is well supplied with it.
                                                           DESCARTES
_________________________________________________________________________



Tue, 04 Nov 1997 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Second CFP: Special Issue of JFP on Algorithmic Aspects of Functional Programming Languages

2. CFP: Special Issue of JFP on Algorithmic Aspects of Functional Programming Languages

3. Final CFP: WAAAPL'99 (Workshop on Algorithmic Aspects of Advanced Programming Languages)

4. First CFP: WAAAPL'99 (Workshop on Algorithmic Aspects of Advanced Programming Languages)

5. 4th Int'l Symp.: Practical Aspects of Declarative Languages

6. Second CFP: Special Issue of JFP on Algorithmic Aspects of Functional Programming Languages

7. CFP: Special Issue of JFP on Algorithmic Aspects of Functional Programming Languages

8. Final CFP: WAAAPL'99 (Workshop on Algorithmic Aspects of Advanced Programming Languages)

9. First CFP: WAAAPL'99 (Workshop on Algorithmic Aspects of Advanced Programming Languages)

10. Help about sorting collection by multiple aspects

11. Ada95 language design question (accessibility rules and type extensions)

12. ANNOUNCEMENT: Ada95 Language Templates for Emacs users

 

 
Powered by phpBB® Forum Software