* Question about generics
@ 1989-05-29 7:02 "Jonathan B. Owen"
0 siblings, 0 replies; 25+ messages in thread
From: "Jonathan B. Owen" @ 1989-05-29 7:02 UTC (permalink / raw)
A while back I tried to define the following:
generic
Max_len : Integer;
package VSTR is
subtype Len_range is integer range 0..Max_len;
type V_string( len : Len_range := 0 ) is
record
data : String(1..len);
end record;
----------------------
-- VSTRING SERVICES --
----------------------
function V_string_of( str : in String ) return V_string;
Etc...
end VSTR;
The purpose of such a package is obviously to use Variable length
strings having different maximum lengths.
Using the about definition after an instantiation on Vax-Ada worked fine
(Such as "package VSTR80 is new VSTR(80)" ).
When transferring the code to Verdix Ada 3.0, on the apollo (SR 10.1),
I received a compiler warning on a variable of such a type, saying
that there is not enough storage for such a variable.
It seems that the upper limit of len_range is not considered constant...
Any ideas to overcome this difficulty?
Thank you,
JB
______________________________________________________________________________
(--) /--) /-(\ Email: gdau100@bguvm (bitnet)
\ / /--K | \|/\ /\/) /|-\ Snail: 55 Hovevei Zion
_/_/o /L__)_/o \/\__/ \X/ \_/ | |_/ Tel-Aviv, 63346 ISRAEL
(/ Jonathan B. Owen Voice: (03) 281-422
Point of view: A chicken is the means by which an egg reproduces an egg.
______________________________________________________________________________
^ permalink raw reply [flat|nested] 25+ messages in thread
* Question about generics
@ 1989-05-29 20:54 "14827_DAVID PAPAY"
0 siblings, 0 replies; 25+ messages in thread
From: "14827_DAVID PAPAY" @ 1989-05-29 20:54 UTC (permalink / raw)
In issue #141, Jonathan Owen presents the following generic unit and asks:
> generic
> Max_len : Integer;
> package VSTR is
>
> subtype Len_range is integer range 0..Max_len;
>
> type V_string( len : Len_range := 0 ) is
> record
> data : String(1..len);
> end record;
>
> ----------------------
> -- VSTRING SERVICES --
> ----------------------
>
> function V_string_of( str : in String ) return V_string;
>
> Etc...
>end VSTR;
>
>
>When transferring the code to Verdix Ada 3.0, on the apollo (SR 10.1),
>I received a compiler warning on a variable of such a type, saying
>that there is not enough storage for such a variable.
>It seems that the upper limit of len_range is not considered constant...
>
>Any ideas to overcome this difficulty?
First of all, the difficulty has nothing to do with the fact the generics are
being used. Instead, it arises because of the use of a default expression
for the discriminant in the record type definition:
>
> type V_string( len : Len_range := 0 ) is
> record
> data : String(1..len);
> end record;
>
Since the discriminant has a default expression, it becomes possible to declare
record objects in two ways:
MY_STRING_A : V_STRING (20); -- with an explicit discriminant constraint
MY_STRING_B : V_STRING; -- using the default expression of 0.
The first record object is said to be CONSTRAINED. The value of the
discriminant LEN cannot be changed, (not even with a complete record
assignment), and evaluation of the attribute MY_STRING_A'CONSTRAINED will be
TRUE. Because the language rules do not allow MY_STRING_A.LEN to be changed,
the compiler can allocate the exact (minimum) storage space needed to represent
the object.
In the other hand, the record object MY_STRING_B is UNCONSTRAINED (evaluation
of MY_STRING_B'CONSTRAINED will be FALSE). Because no explicit discriminant
constraint was given, the value of MY_STRING_B.LEN can be changed during program
execution (using a complete record assignment only). Since the discriminant can
change, the compiler cannot allocate the exact storage space needed, but must
allocate enough space to accommodate the largest possible value of LEN. In
this case, the record object must be large enough to hold a component DATA of
type STRING with a range of 1..INTEGER'LAST. Depending on the value of
INTEGER'LAST, this can be quite a long string!
If you wish to use this package on Verdix Ada, I would suggest that you remove
the default expression " := 0 " from your record type definition and always
declare record objects of this type with explicit discriminant constraints.
David Papay papayd@gtewd.af.mil
GTE Government Systems
PO Box 7188 M/S 5G09 voice: (415) 694-1522
Mountain View, Ca 94039
^ permalink raw reply [flat|nested] 25+ messages in thread
* question about generics
@ 1993-08-09 21:29 Kenneth Anderson
0 siblings, 0 replies; 25+ messages in thread
From: Kenneth Anderson @ 1993-08-09 21:29 UTC (permalink / raw)
I need a generic package to do some initialization based on an array of
strings.
I.E. I need to be able to say something like
package Generic_Package_Support is
Viewers : array (1 .. 5) of STRING := ("How", "Would", "I", "Do", "This?");
end Generic_Package_Support;
with Generic_Package;
with Generic_Package_Support;
package my_instantiated_package is
new Generic_Package(Generic_Package_Support.Viewers);
Then in my generic package, I would loop through the members of the
array and do some initialization.
My problem : How do I specify Generic_Package to accept something like
this?
Ideas/Suggestions welcome...
Thanks in advance,
Ken Anderson
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: question about generics
@ 1993-08-10 15:53 Robert I. Eachus
0 siblings, 0 replies; 25+ messages in thread
From: Robert I. Eachus @ 1993-08-10 15:53 UTC (permalink / raw)
In article <9308091429.aa12174@Paris.ics.uci.edu> kanderso@mabillon.ICS.UCI.EDU
(Kenneth Anderson) writes:
> I need a generic package to do some initialization based on an array of
> strings.
> I.E. I need to be able to say something like
> package Generic_Package_Support is
> Viewers : array (1 .. 5) of STRING := ("How", "Would", "I", "Do", "This
?");
> end Generic_Package_Support;
You apparently need a package which implements varying strings in
a way that allows you to have arrays of differently sized strings.
This can be done in Ada by having a type which has a discriminated
record (with defaults) wrapped in a record with no discriminants.
I can send you such a package if you don't have one...
--
Robert I. Eachus
with Standard_Disclaimer;
use Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: question about generics
@ 1993-08-11 0:25 agate!howland.reston.ans.net!europa.eng.gtefsd.com!darwin.sura.net!seas.g
0 siblings, 0 replies; 25+ messages in thread
From: agate!howland.reston.ans.net!europa.eng.gtefsd.com!darwin.sura.net!seas.g @ 1993-08-11 0:25 UTC (permalink / raw)
In article <EACHUS.93Aug10105310@spectre.mitre.org> eachus@spectre.mitre.org (R
obert I. Eachus) writes:
[deleted]
>
> > I.E. I need to be able to say something like
>
> > package Generic_Package_Support is
> > Viewers : array (1 .. 5) of STRING := ("How", "Would", "I", "Do", "Thi
s?");
> > end Generic_Package_Support;
>
> You apparently need a package which implements varying strings in
>a way that allows you to have arrays of differently sized strings.
>This can be done in Ada by having a type which has a discriminated
>record (with defaults) wrapped in a record with no discriminants.
>I can send you such a package if you don't have one...
>
If I understand this right, we'd better put a warning here to be REAL
careful what the range of the discriminant is, for reasons that we
explain here every couple of months.
To do this ragged array in a more natural way, you'll need 9X.
Mike Feldman
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: question about generics
@ 1993-08-11 18:48 cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.
0 siblings, 0 replies; 25+ messages in thread
From: cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland. @ 1993-08-11 18:48 UTC (permalink / raw)
In article <EACHUS.93Aug10105310@spectre.mitre.org> eachus@spectre.mitre.org (R
obert I. Eachus) writes:
>In article <9308091429.aa12174@Paris.ics.uci.edu> kanderso@mabillon.ICS.UCI.ED
U (Kenneth Anderson) writes:
> > I.E. I need to be able to say something like
> > package Generic_Package_Support is
> > Viewers : array (1 .. 5) of STRING := ("How", "Would", "I", "Do", "Thi
s?");
> > end Generic_Package_Support;
>
> You apparently need a package which implements varying strings in
>a way that allows you to have arrays of differently sized strings.
>This can be done in Ada by having a type which has a discriminated
>record (with defaults) wrapped in a record with no discriminants.
>I can send you such a package if you don't have one...
Why the wrapper? Why won't the following work?
package VAR_STRING is
Max : constant := 1000; -- substitute your idea of reasonable value
type STRING_LENGTH_TYPE is range 0 .. Max;
subtype STRING_INDEX_TYPE is STRING_LENGTH_TYPE range 1 .. Max;
type TEXT ( Size : STRING_LENGTH_TYPE := 0 ) is private;
-- all the operations, including conversion functions to and from
-- STRING, concatenation, etc. two of which are
function TEXT_OF ( Str : STRING ) return TEXT;
function STRING_OF ( Txt : TEXT ) return STRING;
--
end VAR_STRING;
-----------
......
package INIT is new I_FORGOT_THE_NAME
( ( VAR_STRING.TEXT_OF ( "first" ),
VAR_STRING.TEXT_OF ( "second" ),
...
VAR_STRING.TEXT_OF ( "last" ) ) )
.......
Inside the generic you have
for I in Formal'RANGE loop
DO_SOMETHING_WITH ( VAR_STRING.STRING_OF ( Formal ( I ) ) );
end loop;
Although I have not used the VAR_STRING package to pass OBJECTS to a
generic (except indirectly), I don't see why there should be a problem.
Wes G.
P.S. I have seen several "free" variable string packages that have what is
to me a RIDICULOUS usage of access types, requiring all sorts of contortions
by client packages to avoid dangling pointers and heap exhaustion. My package
and the ones I'm complaining about make the type private. In my case, no one
needs to know the fact that it's a simple discriminated record, and "garbage
collection" is done by leaving scope (of a TEXT object). In the other(s),
the type is private in name only, because the client has to understand the
implementation to protect itself )or carefully follow VERY detailed rules
in the comments in the package spec. Why are such packages popular, while my
approach is unpopular enough that I couldn't find anything similar in the
repositories and books I checked? Is there something I don't know?
I have yet to find a disadvantage to it, having used it in user interfaces
and a primitive language parser. I'm admittedly a bit egotistical when it
comes to my programming skills, but not so much so that I don't get nervous
when it appears that no one else has thought of something so simple.
Robert Eachus's post is the first hint I've seen that I'm not alone.
Huh? A P.S. longer than the "main" ?
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: question about generics
@ 1993-08-12 15:18 Robert I. Eachus
0 siblings, 0 replies; 25+ messages in thread
From: Robert I. Eachus @ 1993-08-12 15:18 UTC (permalink / raw)
If you want to create a (ragged) array of strings, you need to
have different discriminant values. But the rule in 3.7.2(8) says
that the default discriminant is used if there is no constraint in the
subtype indication for the array component, so all components of an
array have the same constraints. (There is a note at 3.6.1(16)
pointing this out, but it really should be in 3.6...) In any case by
having a nesting of record types, the discriminant (of the record
inside) of each array component can be different.
An alternate approach is to have a record containing a current
size and a string. The disadvantage is that now equality for objects
of the type doesn't work right unless you explicitly set the "unused"
bytes to a specific value. I prefer the first approach as there is no
distributed cost.
--
Robert I. Eachus
with Standard_Disclaimer;
use Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: question about generics
@ 1993-09-02 17:45 Stef Van Vlierberghe
0 siblings, 0 replies; 25+ messages in thread
From: Stef Van Vlierberghe @ 1993-09-02 17:45 UTC (permalink / raw)
In article <CBLyxC.64x@crdnns.crd.ge.com> groleau@e7sa.crd.ge.com (Wes
Groleau x1240 C73-8) writes:
> P.S. I have seen several "free" variable string packages that have
> what is to me a RIDICULOUS usage of access types, requiring all sorts
> of contortions by client packages to avoid dangling pointers and heap
> exhaustion. My package and the ones I'm complaining about make the
> type private.
Wes, I believe that introducing an arbitrary limit is perfectly o.k.
in many situations, but it's just a technique that (in practice) won't
scale up. If, rather than a simple string, you are implementing a
multi-file screen editor, you might want a list of buffers, each
buffer is a list of lines and each line is a list of characters. My
favorite editor has a 64K line length limit, I have already opened
files of 160_000 lines, and often I had more than 20 files open at the
time (of course I *never* hit all limits in all lists at the same
time).
The technique you describe is elegant and simple, but I don't think
your environment is strong enough to implement my favorite editor
(although theoretically speaking, it could). The packages with those
contortions (typically) will scale up ! As most of the cost of these
contortions is getting to understand the mechanism, one might as well
bite the bullet.
The *real* fun will obviusly start when we get a 9X compiler, then we
will be able to use the User-Defined assignment and Finalization
(which I am so terribly fond of) to transparently manage all the
memory without any help from the client code (it just sees a
non-limited private type).
Where I live, we have terribly suffered from this difficult choice
between simple techniques that don't scale up and contortions that do,
but are quite complex indeed. That's why I did my share of
complaining when the Mapping Specification didn't include User-Defined
Assignment, and also why I can't praize the Mapping Team enough, now
that this feature is in !
Halleluia ! God save the Mapping Team !
Thanks Tuck and Bob, you're heroes !
--------------------------------------------------------------------------
Stef VAN VLIERBERGHE Eurocontrol - Central Flow Management Unit
stef@cfmu.eurocontrol.be Avenue des Arts 19H
Tel: +32 2 729 33 42 B-1040 BRUSSELS
Fax: +32 2 729 32 16 Belgium
^ permalink raw reply [flat|nested] 25+ messages in thread
* Question about generics.
@ 2006-07-02 16:08 Peter C. Chapin
2006-07-02 18:49 ` Martin Krischik
` (3 more replies)
0 siblings, 4 replies; 25+ messages in thread
From: Peter C. Chapin @ 2006-07-02 16:08 UTC (permalink / raw)
Here is what I'm trying to do
generic
Size : in Integer;
package Xyzzy is
type Index is mod Size;
-- etc.
end Xyzzy;
The compiler (gnat) complains about the modular type definition, saying
that Size is a non-static expression and that's no good. I understand
what this error means and I understand the rationale behind it (thanks
to looking in Cohen's book "Ada As a Second Language"). My question is:
how can I get the effect I'm looking for? My plan is to only instantiate
the package with constants so all the necessary information should be
available at compile time. For example
package Fizzle is new Xyzzy(Size => 10);
I realize that I could make the Index type a generic parameter but that
would require me to define the type at the point of instantiation and
that seems unnatural. Conceptually I'm trying to parameterize the
package on a (static) size. It doesn't seem like I should have to define
a type to express that concept.
I admit that I'm a C++ programmer and I'm used to using non-type
parameters in templates to obtain this effect.
template< int size >
class Xyzzy {
char array[size]; // size is a compile time constant.
};
Peter
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-02 16:08 Question " Peter C. Chapin
@ 2006-07-02 18:49 ` Martin Krischik
2006-07-03 6:30 ` Jeffrey R. Carter
` (2 subsequent siblings)
3 siblings, 0 replies; 25+ messages in thread
From: Martin Krischik @ 2006-07-02 18:49 UTC (permalink / raw)
Peter C. Chapin wrote:
> ere is what I'm trying to do
>
> generic
> Size : in Integer;
> package Xyzzy is
> type Index is mod Size;
> -- etc.
> end Xyzzy;
>
> The compiler (gnat) complains about the modular type definition, saying
> that Size is a non-static expression and that's no good.
Do it the other way round:
generic
type Index is mod <>;
package Xyzzy is
Size : Integer := Index'Last + 1;
-- etc.
end Xyzzy;
Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-02 16:08 Question " Peter C. Chapin
2006-07-02 18:49 ` Martin Krischik
@ 2006-07-03 6:30 ` Jeffrey R. Carter
2006-07-03 10:33 ` Peter C. Chapin
2006-07-03 9:46 ` Martin Krischik
2006-07-04 13:29 ` Stephen Leake
3 siblings, 1 reply; 25+ messages in thread
From: Jeffrey R. Carter @ 2006-07-03 6:30 UTC (permalink / raw)
Peter C. Chapin wrote:
>
> generic
> Size : in Integer;
> package Xyzzy is
> type Index is mod Size;
> -- etc.
> end Xyzzy;
What does a Size of -7 mean?
As you've discovered, generic formal parameters are not static in Ada,
and so cannot be used in a type declaration.
You can do
generic
type Index is mod <>;
package P is
Size : constant Positive := Index'Modulus;
end P;
> I realize that I could make the Index type a generic parameter but that
> would require me to define the type at the point of instantiation and
> that seems unnatural. Conceptually I'm trying to parameterize the
> package on a (static) size. It doesn't seem like I should have to define
> a type to express that concept.
Except there's no way to do that.
> template< int size >
> class Xyzzy {
> char array[size]; // size is a compile time constant.
> };
This is a different kettle of fish. You can't declare new integer types
in C++, so you can't compare what you're trying to do with C++. Array
subtype constraints don't have to be static, or even constant:
generic
Size : Positive; -- or Natural, if zero is acceptable
package P is
subtype S is String (1 .. Size); -- Size is a non-static constant
end P;
declare
N : Positive := Get;
begin
if N rem 2 /= 0 then
N := N + 1;
end if;
declare
subtype S is String (1 .. N);
begin
null;
end;
end;
Note that Integer is perfectly OK for using type String, for exponents,
and for sizes and counts that will fit in a 16-bit signed integer, but
for just about everything else you should be using user-defined types
based on the requirements of the application. Whichever you use, if
negative values are not meaningful, you should explicitly exclude them
by using an appropriate subtype.
--
Jeff Carter
"We'll make Rock Ridge think it's a chicken
that got caught in a tractor's nuts!"
Blazing Saddles
87
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-02 16:08 Question " Peter C. Chapin
2006-07-02 18:49 ` Martin Krischik
2006-07-03 6:30 ` Jeffrey R. Carter
@ 2006-07-03 9:46 ` Martin Krischik
2006-07-04 13:29 ` Stephen Leake
3 siblings, 0 replies; 25+ messages in thread
From: Martin Krischik @ 2006-07-03 9:46 UTC (permalink / raw)
Peter C. Chapin wrote:
> template< int size >
> class Xyzzy {
> char array[size]; // size is a compile time constant.
> };
in Ada you could use:
generic
type Array_Type is array (Integer range <>) of Character.
package Xyzzy is
Size : constant Positive := Array_Type'Lenght;
end Xyzzy;
Martin
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-03 6:30 ` Jeffrey R. Carter
@ 2006-07-03 10:33 ` Peter C. Chapin
2006-07-03 11:42 ` Jean-Pierre Rosen
` (2 more replies)
0 siblings, 3 replies; 25+ messages in thread
From: Peter C. Chapin @ 2006-07-03 10:33 UTC (permalink / raw)
"Jeffrey R. Carter" <spam.not.jrcarter@acm.not.spam.org> wrote in
news:7Q2qg.812592$084.507058@attbi_s22:
> Peter C. Chapin wrote:
>>
>> generic
>> Size : in Integer;
>> package Xyzzy is
>> type Index is mod Size;
>> -- etc.
>> end Xyzzy;
>
> What does a Size of -7 mean?
Why not a compile time error when the instantiation is attempted?
> As you've discovered, generic formal parameters are not static in Ada,
> and so cannot be used in a type declaration.
>
> You can do
>
> generic
> type Index is mod <>;
> package P is
> Size : constant Positive := Index'Modulus;
> end P;
Yes. However, this means at the point of instantiation I have to do
something like
type Dummy_Type is mod 8;
package Fizzle is new Xyzzy(Index => Dummy_Type);
The Dummy_Type will never be used in the context of the instantiation;
it only exists to pass size information into the generic package. That
seems pretty counter-intuitive and awkward. I guess I was hoping Ada
would have a nicer solution... generic parameters that are named numbers
or something.
>> template< int size >
>> class Xyzzy {
>> char array[size]; // size is a compile time constant.
>> };
>
> This is a different kettle of fish. You can't declare new integer
> types in C++, so you can't compare what you're trying to do with C++.
> Array subtype constraints don't have to be static, or even constant:
Don't be distracted by the fact that I used an array declaration above
to demonstrate the "static-ness" of size. I realize that array bounds
don't have to be static in Ada. However, my point was that C++ templates
do allow non-type parameters to be used in contexts where compile time
constants are required.
For my particular situation I believe I could declare a subtype inside
the generic package instead of a modular type. Alas, that means I'll
have to manually wrap around values of my type. It's not the end of the
world but, as I said, I was hoping for a nicer solution.
Peter
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-03 10:33 ` Peter C. Chapin
@ 2006-07-03 11:42 ` Jean-Pierre Rosen
2006-07-03 16:44 ` Pascal Obry
2006-07-03 20:03 ` Jeffrey R. Carter
2 siblings, 0 replies; 25+ messages in thread
From: Jean-Pierre Rosen @ 2006-07-03 11:42 UTC (permalink / raw)
Peter C. Chapin a �crit :
> "Jeffrey R. Carter" <spam.not.jrcarter@acm.not.spam.org> wrote in
> news:7Q2qg.812592$084.507058@attbi_s22:
>
>> Peter C. Chapin wrote:
>>> generic
>>> Size : in Integer;
>>> package Xyzzy is
>>> type Index is mod Size;
>>> -- etc.
>>> end Xyzzy;
>> What does a Size of -7 mean?
>
> Why not a compile time error when the instantiation is attempted?
>
Because in Ada, legality is checked when the generic is compiled.
*If* the generic compiles, and *if* parameters in the instantiation
match the formals, *then* the instantiation is guaranteed to be correct,
without rechecking the expanded form.
This is a useful and important difference from C++ templates.
--
---------------------------------------------------------
J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-03 10:33 ` Peter C. Chapin
2006-07-03 11:42 ` Jean-Pierre Rosen
@ 2006-07-03 16:44 ` Pascal Obry
2006-07-04 1:09 ` Peter C. Chapin
2006-07-03 20:03 ` Jeffrey R. Carter
2 siblings, 1 reply; 25+ messages in thread
From: Pascal Obry @ 2006-07-03 16:44 UTC (permalink / raw)
To: Peter C. Chapin
Peter C. Chapin a �crit :
> Yes. However, this means at the point of instantiation I have to do
> something like
>
> type Dummy_Type is mod 8;
> package Fizzle is new Xyzzy(Index => Dummy_Type);
>
> The Dummy_Type will never be used in the context of the instantiation;
Why won't it be used? If you pass it to the generic there lot of chances
that some function/procedure will be passed such a type, no ? And so
the type will be used on the client code too...
Pascal.
--
--|------------------------------------------------------
--| Pascal Obry Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-03 10:33 ` Peter C. Chapin
2006-07-03 11:42 ` Jean-Pierre Rosen
2006-07-03 16:44 ` Pascal Obry
@ 2006-07-03 20:03 ` Jeffrey R. Carter
2006-07-03 20:18 ` Dmitry A. Kazakov
2 siblings, 1 reply; 25+ messages in thread
From: Jeffrey R. Carter @ 2006-07-03 20:03 UTC (permalink / raw)
Peter C. Chapin wrote:
>
> Why not a compile time error when the instantiation is attempted?
Because the legality of a generic unit is determined when the generic is
compiled, not when it is instantiated. That the generic formal of Size
allows non-positive values is yet another reason this specific example
would be illegal, even if Size were static.
> Yes. However, this means at the point of instantiation I have to do
> something like
>
> type Dummy_Type is mod 8;
> package Fizzle is new Xyzzy(Index => Dummy_Type);
>
> The Dummy_Type will never be used in the context of the instantiation;
> it only exists to pass size information into the generic package. That
> seems pretty counter-intuitive and awkward. I guess I was hoping Ada
> would have a nicer solution... generic parameters that are named numbers
> or something.
I find this hard to accept. The reason to declare a visible type in the
generic is so that the client can use the type from an instantiation to
call operations of the type provided by the generic.
--
Jeff Carter
"Now go away or I shall taunt you a second time."
Monty Python & the Holy Grail
07
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-03 20:03 ` Jeffrey R. Carter
@ 2006-07-03 20:18 ` Dmitry A. Kazakov
2006-07-04 0:08 ` Randy Brukardt
2006-07-04 0:43 ` Jeffrey R. Carter
0 siblings, 2 replies; 25+ messages in thread
From: Dmitry A. Kazakov @ 2006-07-03 20:18 UTC (permalink / raw)
On Mon, 03 Jul 2006 20:03:21 GMT, Jeffrey R. Carter wrote:
> Peter C. Chapin wrote:
>>
>> Why not a compile time error when the instantiation is attempted?
>
> Because the legality of a generic unit is determined when the generic is
> compiled, not when it is instantiated. That the generic formal of Size
> allows non-positive values is yet another reason this specific example
> would be illegal, even if Size were static.
Right. But it is the problem of Ada, that "static Positive" cannot be
expressed as a [formal] type. If that were possible then the generic unit
could be checked before instantiation. There are also other cases where
that would be useful. For example, for forcing constant folding at compile
time (necessary for statically dimensioned values.)
>> Yes. However, this means at the point of instantiation I have to do
>> something like
>>
>> type Dummy_Type is mod 8;
>> package Fizzle is new Xyzzy(Index => Dummy_Type);
>>
>> The Dummy_Type will never be used in the context of the instantiation;
>> it only exists to pass size information into the generic package. That
>> seems pretty counter-intuitive and awkward. I guess I was hoping Ada
>> would have a nicer solution... generic parameters that are named numbers
>> or something.
>
> I find this hard to accept. The reason to declare a visible type in the
> generic is so that the client can use the type from an instantiation to
> call operations of the type provided by the generic.
I don't think it is a good argument. The types declared in the visible part
of Xyzzy can be referred as Fizzle.Type_Declared_Inside.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-03 20:18 ` Dmitry A. Kazakov
@ 2006-07-04 0:08 ` Randy Brukardt
2006-07-04 7:48 ` Dmitry A. Kazakov
2006-07-04 0:43 ` Jeffrey R. Carter
1 sibling, 1 reply; 25+ messages in thread
From: Randy Brukardt @ 2006-07-04 0:08 UTC (permalink / raw)
"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message
news:dv4t30a6porw.1r47tofvyu5ws$.dlg@40tude.net...
...
> Right. But it is the problem of Ada, that "static Positive" cannot be
> expressed as a [formal] type. If that were possible then the generic unit
> could be checked before instantiation. There are also other cases where
> that would be useful. For example, for forcing constant folding at compile
> time (necessary for statically dimensioned values.)
It's not a problem, it's a feature. Repeat after me: a generic is not a
macro. A generic is not a macro. A generic is not a macro. Got it yet?? ;-)
The legality of a generic body never, ever depends on the actual values used
in an instantiation. The legality of a generic spec rarely depends on the
actual values.
In any case, allowing some sort of static generic parameter would completely
prevent any possibility of generic code sharing. It's unlikely that the
language would be changed in such a way.
(If you don't understand why, just consider putting the bit locations for
various types as formal parameters. How would you implement sharing then?)
Randy.
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-03 20:18 ` Dmitry A. Kazakov
2006-07-04 0:08 ` Randy Brukardt
@ 2006-07-04 0:43 ` Jeffrey R. Carter
1 sibling, 0 replies; 25+ messages in thread
From: Jeffrey R. Carter @ 2006-07-04 0:43 UTC (permalink / raw)
Dmitry A. Kazakov wrote:
>
> I don't think it is a good argument. The types declared in the visible part
> of Xyzzy can be referred as Fizzle.Type_Declared_Inside.
Apparently I was not clear. Such types not only can be referred to, they
*will* be referred to. The OP intended to declare a type based on a
generic parameter. Then he says that if the type is declared by the
instantiating unit (to serve as the generic actual parameter) that it
will never be referred to.
--
Jeff Carter
"Now go away or I shall taunt you a second time."
Monty Python & the Holy Grail
07
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-03 16:44 ` Pascal Obry
@ 2006-07-04 1:09 ` Peter C. Chapin
2006-07-04 6:17 ` M E Leypold
0 siblings, 1 reply; 25+ messages in thread
From: Peter C. Chapin @ 2006-07-04 1:09 UTC (permalink / raw)
Pascal Obry <pascal@obry.net> wrote in news:44A9495A.7020106@obry.net:
>> Yes. However, this means at the point of instantiation I have to do
>> something like
>>
>> type Dummy_Type is mod 8;
>> package Fizzle is new Xyzzy(Index => Dummy_Type);
>>
>> The Dummy_Type will never be used in the context of the
>> instantiation;
>
> Why won't it be used? If you pass it to the generic there lot of
> chances that some function/procedure will be passed such a type, no ?
> And so the type will be used on the client code too...
Well, in my situation the type is an implementation detail. It does not
appear in the interface to the generic package. The user of the generic
package can't do anything useful with the type.
Actually the Size parameter isn't part of the (functional) interface
either. However it will impact the performance characteristics of the
package and thus Size is interesting to the clients. That is why I'd
like to parameterize it. I can see how to accomplish this, but I don't
think what I tried originally is particularly odd or unreasonable.
I guess the issue, as has been pointed out by others, is that Ada wants
to be sure the generic package will instantiate correctly if the
arguments provided obey the constraints that have been specified in the
generic parameters. However, the generic parameter "language" isn't
quite expressive enough to say what I want to say ("only allow positive
constants") and so my first approach isn't accepted.
I can see that the C++ approach, which defers the detection of errors
until instantiation time, allows a bit more flexibility in this
respect... at the expense of cryptic error messages when something goes
wrong. In any case I've found this discussion interesting and
educational. I'd like to thank all of those who have participated.
Peter
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-04 1:09 ` Peter C. Chapin
@ 2006-07-04 6:17 ` M E Leypold
2006-07-04 10:48 ` Peter C. Chapin
0 siblings, 1 reply; 25+ messages in thread
From: M E Leypold @ 2006-07-04 6:17 UTC (permalink / raw)
"Peter C. Chapin" <pchapin@sover.net> writes:
> Pascal Obry <pascal@obry.net> wrote in news:44A9495A.7020106@obry.net:
>
> >> Yes. However, this means at the point of instantiation I have to do
> >> something like
> >>
> >> type Dummy_Type is mod 8;
> >> package Fizzle is new Xyzzy(Index => Dummy_Type);
> >>
> >> The Dummy_Type will never be used in the context of the
> >> instantiation;
> >
> > Why won't it be used? If you pass it to the generic there lot of
> > chances that some function/procedure will be passed such a type, no ?
> > And so the type will be used on the client code too...
>
> Well, in my situation the type is an implementation detail. It does not
> appear in the interface to the generic package. The user of the generic
> package can't do anything useful with the type.
>
> Actually the Size parameter isn't part of the (functional) interface
> either. However it will impact the performance characteristics of the
> package and thus Size is interesting to the clients. That is why I'd
> like to parameterize it. I can see how to accomplish this, but I don't
> think what I tried originally is particularly odd or unreasonable.
>
> I guess the issue, as has been pointed out by others, is that Ada wants
> to be sure the generic package will instantiate correctly if the
> arguments provided obey the constraints that have been specified in the
> generic parameters. However, the generic parameter "language" isn't
> quite expressive enough to say what I want to say ("only allow positive
> constants") and so my first approach isn't accepted.
Wouldn't it do to check the parameter in the body of the package
during elaboration? Admittedly there is overhead and usage errors are
not caught by the compiler then, but it would help to avoid more
obscure errors later.
Regards -- Markus
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-04 0:08 ` Randy Brukardt
@ 2006-07-04 7:48 ` Dmitry A. Kazakov
0 siblings, 0 replies; 25+ messages in thread
From: Dmitry A. Kazakov @ 2006-07-04 7:48 UTC (permalink / raw)
On Mon, 3 Jul 2006 19:08:07 -0500, Randy Brukardt wrote:
> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message
> news:dv4t30a6porw.1r47tofvyu5ws$.dlg@40tude.net...
> ...
>> Right. But it is the problem of Ada, that "static Positive" cannot be
>> expressed as a [formal] type. If that were possible then the generic unit
>> could be checked before instantiation. There are also other cases where
>> that would be useful. For example, for forcing constant folding at compile
>> time (necessary for statically dimensioned values.)
>
> It's not a problem, it's a feature. Repeat after me: a generic is not a
> macro. A generic is not a macro. A generic is not a macro. Got it yet?? ;-)
No, they are, even if bodies are shared! (:-))
> The legality of a generic body never, ever depends on the actual values used
> in an instantiation. The legality of a generic spec rarely depends on the
> actual values.
Yes, therefore Size cannot be just Positive. Legality of "type X mod Size"
does not depend on the value of Size, it does on whether the value is
static. [ OK, that could be changed as well, but it is another story. ]
> In any case, allowing some sort of static generic parameter would completely
> prevent any possibility of generic code sharing. It's unlikely that the
> language would be changed in such a way.
>
> (If you don't understand why, just consider putting the bit locations for
> various types as formal parameters. How would you implement sharing then?)
It is definitely possible. Though it might become utterly inefficient.
Anything that determines legality must be in the contract. So the
constraints have to be passed from *outside*. For example, if the static
parameters are used in case within the body:
case Something is
when Param1 =>
when Param2 =>
when Param3 =>
end case
then the constraint that Params do not intersect and cover all choices
shall be in some other formal parameter of the generic unit. [Universal
sets, range would probably be required to express such things] Without such
parameter+constraint generic would spit out an error, that choices are
wrong.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-04 6:17 ` M E Leypold
@ 2006-07-04 10:48 ` Peter C. Chapin
0 siblings, 0 replies; 25+ messages in thread
From: Peter C. Chapin @ 2006-07-04 10:48 UTC (permalink / raw)
M E Leypold <development-2006-8ecbb5cc8aREMOVETHIS@ANDTHATm-e-leypold.de>
wrote in news:x6ac7pyjud.fsf@hod.lan.m-e-leypold.de:
> Wouldn't it do to check the parameter in the body of the package
> during elaboration? Admittedly there is overhead and usage errors are
> not caught by the compiler then, but it would help to avoid more
> obscure errors later.
I was hoping to make use of the automatic wrap-around facility of modular
types.
Peter
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-02 16:08 Question " Peter C. Chapin
` (2 preceding siblings ...)
2006-07-03 9:46 ` Martin Krischik
@ 2006-07-04 13:29 ` Stephen Leake
2006-07-05 12:08 ` Dmitry A. Kazakov
3 siblings, 1 reply; 25+ messages in thread
From: Stephen Leake @ 2006-07-04 13:29 UTC (permalink / raw)
"Peter C. Chapin" <pchapin@sover.net> writes:
> Here is what I'm trying to do
>
> generic
> Size : in Integer;
> package Xyzzy is
> type Index is mod Size;
> -- etc.
> end Xyzzy;
>
> The compiler (gnat) complains about the modular type definition, saying
> that Size is a non-static expression and that's no good. I understand
> what this error means and I understand the rationale behind it (thanks
> to looking in Cohen's book "Ada As a Second Language").
For others, I'll summarize that rationale here. The generic above
might be instantiated in a procedure:
procedure Foo (User_Size : in Integer)
is
package Bar is new Xyzzy (Size => User_Size);
begin
...
end Foo;
In this case, Size is _not_ static, not even in an informal sense. So
the compiler doesn't know how many bits to use for Index.
> My question is: how can I get the effect I'm looking for? My plan is
> to only instantiate the package with constants so all the necessary
> information should be available at compile time. For example
>
> package Fizzle is new Xyzzy(Size => 10);
>
> I realize that I could make the Index type a generic parameter but that
> would require me to define the type at the point of instantiation and
> that seems unnatural. Conceptually I'm trying to parameterize the
> package on a (static) size. It doesn't seem like I should have to define
> a type to express that concept.
That does seem like a reasonable goal. But I think you need to modify
the language to get there. Perhaps a new class of generic parameters:
generic
Size : static Integer;
package Xyzzy_Static is
type Index is mod Size;
-- etc.
end Xyzzy_Static;
Then Xyzzy_Static could only be instantiated with truly static Size
(in the Ada meaning of 'static').
Short of that, I think you have to declare the type outside the
generic package, as you say.
I often find it better to declare the type outside the package,
because I want it for things the package doesn't declare. But if you
intend to put everything into the generic, it would be nice to do it
your way.
It would be interesting to raise the 'static' proposal on the Ada
comment mailing list (where they discuss future changes to the Ada
language). But to do that right, I'd need more information about why
you want to do this. Their mostly likely response will be "it's not
hard to declare the type outside the package". So we need some more
ammunition about why that's not a good answer.
--
-- Stephe
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Question about generics.
2006-07-04 13:29 ` Stephen Leake
@ 2006-07-05 12:08 ` Dmitry A. Kazakov
0 siblings, 0 replies; 25+ messages in thread
From: Dmitry A. Kazakov @ 2006-07-05 12:08 UTC (permalink / raw)
On Tue, 04 Jul 2006 09:29:40 -0400, Stephen Leake wrote:
> That does seem like a reasonable goal. But I think you need to modify
> the language to get there. Perhaps a new class of generic parameters:
>
> generic
> Size : static Integer;
> package Xyzzy_Static is
> type Index is mod Size;
> -- etc.
> end Xyzzy_Static;
>
> Then Xyzzy_Static could only be instantiated with truly static Size
> (in the Ada meaning of 'static').
>
> Short of that, I think you have to declare the type outside the
> generic package, as you say.
>
> I often find it better to declare the type outside the package,
> because I want it for things the package doesn't declare. But if you
> intend to put everything into the generic, it would be nice to do it
> your way.
>
> It would be interesting to raise the 'static' proposal on the Ada
> comment mailing list (where they discuss future changes to the Ada
> language). But to do that right, I'd need more information about why
> you want to do this. Their mostly likely response will be "it's not
> hard to declare the type outside the package". So we need some more
> ammunition about why that's not a good answer.
It is more interesting for cases unrelated to generics:
User-defined static functions:
function Factorial (N : static Natural) return static Natural;
type My_Strange_Type is mod Factorial (5);
function C_Compiler (X : static String) return static Float;
C : constant Float :=
C_Compiler ("/* some C program */ float x=2.0; for(;;){...");
-- Evaluated by the compiler at compile-time.
function Number_Quarks_Requred_On_This_Mashine
( OS : static String;
CPU : static String;
...
) return static Quark_Number;
function To_UTF8 (X : static String) return static String;
while Current_Line = To_UTF8 ("...") loop
...; -- Manual constant folding is always possible, though
end loop;
type EEPROM is array (...) of Storage_Elements;
Boot : static EEPROM := Link (C_Compiler (...));
for Boot'Address use ...;
if Pure were a qualifier instead of pragma, then static result could be
made implied from static parameters. I have difficulty to classify "static"
as a type qualifier. Is static X is subtype, new type or what.
Dimensioned arithmetic:
type Unit is mod 2**28; -- Encoded powers of base units
type Dimensioned (SI : Unit) record
Gain : Float;
end record;
function "+" (Left, Right : Dimensioned) return Dimensioned;
function "-" (Left, Right : Dimensioned) return Dimensioned;
function "*" (Left, Right : Dimensioned) return Dimensioned;
function "/" (Left, Right : Dimensioned) return Dimensioned;
...
type Static_Dimensioned (SI : static Unit) is new Dimensioned (SI);
-- Statically constrained Dimensioned, the compiler is *required* to
-- drop the discriminant in the representation of. When converted
-- back to Dimensioned, the discriminant is reconstructed. Forward
-- conversion is compile-time type error, when the discriminant
-- isn't static.
function "+" (Left, Right : Static_Dimensioned)
return Static_Dimensioned;
function "-" (Left, Right : Static_Dimensioned)
return Static_Dimensioned;
function "*" (Left, Right : Static_Dimensioned)
return Static_Dimensioned;
function "/" (Left, Right : Static_Dimensioned)
return Static_Dimensioned;
...
I think you've got the idea. This gives you dimensioned arithmetic fully
statically checkable, space/time penalty free, which also can handle
unknown dimensions at run-time.
But there are too many problems with this. Note how important are partially
static types like Static_Dimensioned. Just one type qualifier "static" does
not solve the problem. It must be much finer.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
^ permalink raw reply [flat|nested] 25+ messages in thread
end of thread, other threads:[~2006-07-05 12:08 UTC | newest]
Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-08-11 18:48 question about generics cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.
-- strict thread matches above, loose matches on Subject: below --
2006-07-02 16:08 Question " Peter C. Chapin
2006-07-02 18:49 ` Martin Krischik
2006-07-03 6:30 ` Jeffrey R. Carter
2006-07-03 10:33 ` Peter C. Chapin
2006-07-03 11:42 ` Jean-Pierre Rosen
2006-07-03 16:44 ` Pascal Obry
2006-07-04 1:09 ` Peter C. Chapin
2006-07-04 6:17 ` M E Leypold
2006-07-04 10:48 ` Peter C. Chapin
2006-07-03 20:03 ` Jeffrey R. Carter
2006-07-03 20:18 ` Dmitry A. Kazakov
2006-07-04 0:08 ` Randy Brukardt
2006-07-04 7:48 ` Dmitry A. Kazakov
2006-07-04 0:43 ` Jeffrey R. Carter
2006-07-03 9:46 ` Martin Krischik
2006-07-04 13:29 ` Stephen Leake
2006-07-05 12:08 ` Dmitry A. Kazakov
1993-09-02 17:45 question " Stef Van Vlierberghe
1993-08-12 15:18 Robert I. Eachus
1993-08-11 0:25 agate!howland.reston.ans.net!europa.eng.gtefsd.com!darwin.sura.net!seas.g
1993-08-10 15:53 Robert I. Eachus
1993-08-09 21:29 Kenneth Anderson
1989-05-29 20:54 Question " "14827_DAVID PAPAY"
1989-05-29 7:02 "Jonathan B. Owen"
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox