comp.lang.ada
 help / color / mirror / Atom feed
* Recursive parameters in generics
@ 2007-07-23 18:20 Dmitry A. Kazakov
  2007-07-23 19:32 ` Maciej Sobczak
  2007-07-23 21:51 ` Adam Beneschan
  0 siblings, 2 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2007-07-23 18:20 UTC (permalink / raw)


Hi there,

out of curiosity, recently experimenting with workarounds for generics, I
invented a construct, which puzzles me. Is the following legal:

generic
   S : String;
package A is
   S_Of : String renames S;
end A;
-----------------------------------
with A;
generic
   S : String;
   with package This_A is new A (S => S);
package B is
end B;
-----------------------------------
with A;
package My_A is new A (S => "whatever");
-----------------------------------
with My_A;
with B;
package My_B is
   new B (This_A => My_A, S => My_A.S_Of);
-----------------------------------
One could think that My_B would take S_Of from an instance of A and then
pass it back to A.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Recursive parameters in generics
  2007-07-23 18:20 Recursive parameters in generics Dmitry A. Kazakov
@ 2007-07-23 19:32 ` Maciej Sobczak
  2007-07-23 19:46   ` Dmitry A. Kazakov
  2007-07-23 21:51 ` Adam Beneschan
  1 sibling, 1 reply; 8+ messages in thread
From: Maciej Sobczak @ 2007-07-23 19:32 UTC (permalink / raw)


On 23 Lip, 20:20, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:

> out of curiosity, recently experimenting with workarounds for generics, I
> invented a construct, which puzzles me.

No way! Forget this immediately! There are no puzzles in Ada, period.
Puzzles are strictly reserved only for C and C++.

Sorry, could not resist... ;-)

--
Maciej Sobczak
http://www.msobczak.com/




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

* Re: Recursive parameters in generics
  2007-07-23 19:32 ` Maciej Sobczak
@ 2007-07-23 19:46   ` Dmitry A. Kazakov
  0 siblings, 0 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2007-07-23 19:46 UTC (permalink / raw)


On Mon, 23 Jul 2007 12:32:36 -0700, Maciej Sobczak wrote:

> On 23 Lip, 20:20, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
> wrote:
> 
>> out of curiosity, recently experimenting with workarounds for generics, I
>> invented a construct, which puzzles me.
> 
> No way! Forget this immediately! There are no puzzles in Ada, period.
> Puzzles are strictly reserved only for C and C++.
> 
> Sorry, could not resist... ;-)

Note, I always wished to eliminate generics in Ada... (:-))

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Recursive parameters in generics
  2007-07-23 18:20 Recursive parameters in generics Dmitry A. Kazakov
  2007-07-23 19:32 ` Maciej Sobczak
@ 2007-07-23 21:51 ` Adam Beneschan
  2007-07-24  8:09   ` Dmitry A. Kazakov
  1 sibling, 1 reply; 8+ messages in thread
From: Adam Beneschan @ 2007-07-23 21:51 UTC (permalink / raw)


On Jul 23, 11:20 am, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:
> Hi there,
>
> out of curiosity, recently experimenting with workarounds for generics, I
> invented a construct, which puzzles me. Is the following legal:
>
> generic
>    S : String;
> package A is
>    S_Of : String renames S;
> end A;
> -----------------------------------
> with A;
> generic
>    S : String;
>    with package This_A is new A (S => S);
> package B is
> end B;
> -----------------------------------
> with A;
> package My_A is new A (S => "whatever");
> -----------------------------------
> with My_A;
> with B;
> package My_B is
>    new B (This_A => My_A, S => My_A.S_Of);
> -----------------------------------
> One could think that My_B would take S_Of from an instance of A and then
> pass it back to A.

I don't see why this would be any different from

package My_B is new B (S => "whatever", This_A => My_A);

If you're thinking about My_B passing something back to A, you're
probably thinking about the whole problem sideways.  My_B doesn't
instantiate A at all, it uses an already-existing instance of A.

                 -- Adam




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

* Re: Recursive parameters in generics
  2007-07-23 21:51 ` Adam Beneschan
@ 2007-07-24  8:09   ` Dmitry A. Kazakov
  2007-07-24 16:00     ` Adam Beneschan
  0 siblings, 1 reply; 8+ messages in thread
From: Dmitry A. Kazakov @ 2007-07-24  8:09 UTC (permalink / raw)


On Mon, 23 Jul 2007 14:51:10 -0700, Adam Beneschan wrote:

> On Jul 23, 11:20 am, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
> wrote:
>> Hi there,
>>
>> out of curiosity, recently experimenting with workarounds for generics, I
>> invented a construct, which puzzles me. Is the following legal:
>>
>> generic
>>    S : String;
>> package A is
>>    S_Of : String renames S;
>> end A;
>> -----------------------------------
>> with A;
>> generic
>>    S : String;
>>    with package This_A is new A (S => S);
>> package B is
>> end B;
>> -----------------------------------
>> with A;
>> package My_A is new A (S => "whatever");
>> -----------------------------------
>> with My_A;
>> with B;
>> package My_B is
>>    new B (This_A => My_A, S => My_A.S_Of);
>> -----------------------------------
>> One could think that My_B would take S_Of from an instance of A and then
>> pass it back to A.
> 
> I don't see why this would be any different from
> 
> package My_B is new B (S => "whatever", This_A => My_A);

So you think it is legal?

> If you're thinking about My_B passing something back to A, you're
> probably thinking about the whole problem sideways.  My_B doesn't
> instantiate A at all, it uses an already-existing instance of A.

I mean the process of matching parameters. I.e. what happens at the
instantiation point. At least it perplexes GNAT, provided the thingy is
legal.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Recursive parameters in generics
  2007-07-24  8:09   ` Dmitry A. Kazakov
@ 2007-07-24 16:00     ` Adam Beneschan
  2007-07-25  6:43       ` AW: " Grein, Christoph (Fa. ESG)
  0 siblings, 1 reply; 8+ messages in thread
From: Adam Beneschan @ 2007-07-24 16:00 UTC (permalink / raw)


On Jul 24, 1:09 am, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:
> On Mon, 23 Jul 2007 14:51:10 -0700, Adam Beneschan wrote:
> > On Jul 23, 11:20 am, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
> > wrote:
> >> Hi there,
>
> >> out of curiosity, recently experimenting with workarounds for generics, I
> >> invented a construct, which puzzles me. Is the following legal:
>
> >> generic
> >>    S : String;
> >> package A is
> >>    S_Of : String renames S;
> >> end A;
> >> -----------------------------------
> >> with A;
> >> generic
> >>    S : String;
> >>    with package This_A is new A (S => S);
> >> package B is
> >> end B;
> >> -----------------------------------
> >> with A;
> >> package My_A is new A (S => "whatever");
> >> -----------------------------------
> >> with My_A;
> >> with B;
> >> package My_B is
> >>    new B (This_A => My_A, S => My_A.S_Of);
> >> -----------------------------------
> >> One could think that My_B would take S_Of from an instance of A and then
> >> pass it back to A.
>
> > I don't see why this would be any different from
>
> > package My_B is new B (S => "whatever", This_A => My_A);
>
> So you think it is legal?

It should be.  However, I'm taking a second look at 12.7(5-6).

> > If you're thinking about My_B passing something back to A, you're
> > probably thinking about the whole problem sideways.  My_B doesn't
> > instantiate A at all, it uses an already-existing instance of A.
>
> I mean the process of matching parameters. I.e. what happens at the
> instantiation point. At least it perplexes GNAT, provided the thingy is
> legal.

The first formal parameter to B is "S : String", and you're matching
it with My_A.S_Of, which is the string "whatever".

The second formal parameter is "with package This_A is new A (S =>
S)";  This matches My_A as long as the S parameter of This_A and My_A
match.  The S parameter of This_A is the actual for S, which is
My_A.S_Of.  The "actual parameter of the actual instance" is the
actual parameter you gave when instantiating My_A, which is the string
literal "whatever".  So the question now is whether My_A.S_Of and
"whatever" statically denote the same constant, or are static
expressions with the same value (see 12.7(6)).

And this is where things get yucky.  12.3(13) says that an instance is
a copy of the text in the template, where each "use of a formal
parameter" becomes a "use of the actual"; but does this mean that in
the instance My_A, the use of "S" *statically denotes* (see
4.9(14-17)) the string literal "whatever"?  (If it did, then S_Of
would also denote the same string literal, by 4.9(17).)  If it does,
then I think the example is probably legal---but maybe not, since a
constant is an object, and a literal is a value, not a constant, and
I'm not clear on just what "object" would be denoted by My_A.S or
My_A.S_Of.

Just to complicate things, I tried this on GNAT, and also tried the
same example replacing String by Integer and the string literal by an
integer literal.  GNAT rejected the version with String but accepted
the version with Integer.  Offhand, I can't see any reason why the two
cases would be different, so I suspect there's a GNAT bug involved---
but whether it should be accepting the String case, or rejecting the
Integer case, I don't know.  (Also, I may not be using the latest
version of GNAT.)

To summarize my conclusions: (1) Your example *should* be legal, since
it seems like the actual parameter to the actual instance My_A, and
the actual parameter to the formal instance This_A, do refer to the
exact same object.  (2) The definitions in the RM seem too ambiguous
to know for certain whether the example is legal or not.  I may need
to examine this more carefully, though.  It may be that language needs
to be added to 4.9 to deal with generic formals in an instance (when
they're referred to outside of the instance).  Or it may be that the
RM language is already sufficient to deal with this, and I need to dig
deeper and find it.  (3) If it turns out that it isn't legal, this
seems like a hole in the language, since there doesn't seem to be any
reason why this shouldn't be allowed.  (4) GNAT probably is not
handling this correctly.

                           -- Adam






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

* AW: Recursive parameters in generics
  2007-07-24 16:00     ` Adam Beneschan
@ 2007-07-25  6:43       ` Grein, Christoph (Fa. ESG)
  2007-07-25 15:13         ` Adam Beneschan
  0 siblings, 1 reply; 8+ messages in thread
From: Grein, Christoph (Fa. ESG) @ 2007-07-25  6:43 UTC (permalink / raw)
  To: comp.lang.ada

Adam wrote a fair exegesis:
> The second formal parameter is "with package This_A is new A (S =>
> S)";  This matches My_A as long as the S parameter of This_A and My_A
> match... So the question now is whether My_A.S_Of and "whatever"
> statically denote the same constant, or are static expressions with
the
> same value (see 12.7(6)).
> ...
> To summarize my conclusions: (1) Your example *should* be legal, ...

I'm not so sure since generic parameters are never static. The actual
could be an expression like

S => Function_Call & Another_Non_Static;

But then the puzzle remains why the version with Integer replacing
String is legal. S_Of is never static as far as I can see.


Eurocopter Deutschland GmbH
Sitz der Gesellschaft/Registered Office: Donauwoerth
Registergericht/Registration Court: Amtsgericht Augsburg HRB 16508
Vorsitzender des Aufsichtsrates/Chairman of the Supervisory Board: Dr. Lutz Bertling
Geschaeftsfuehrung/Board of Management:
Dr. Wolfgang Schoder, Vorsitzender/CEO; Friedrich-Wilhelm Hormel; Ralf Barnscheidt

CONFIDENTIALITY NOTICE 

This communication and the information it contains is intended for the addressee(s) named above and for no other persons or organizations. It is confidential and may be legally privileged and protected by law. The unauthorized use, copying or disclosure of this communication or any part of it is prohibited and may be unlawful. 
If you have received this communication in error, kindly notify us by return e-mail and discard and/or delete the communication. Thank you very much. 
It is possible for e-mails to be intercepted or affected by viruses. Whilst we maintain virus checks on our e-mails, we accept no liability for viruses or other material which might be introduced with this message. 




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

* Re: Recursive parameters in generics
  2007-07-25  6:43       ` AW: " Grein, Christoph (Fa. ESG)
@ 2007-07-25 15:13         ` Adam Beneschan
  0 siblings, 0 replies; 8+ messages in thread
From: Adam Beneschan @ 2007-07-25 15:13 UTC (permalink / raw)


On Jul 24, 11:43 pm, "Grein, Christoph (Fa. ESG)"
<Christoph.Gr...@eurocopter.com> wrote:
> Adam wrote a fair exegesis:
>
> > The second formal parameter is "with package This_A is new A (S =>
> > S)";  This matches My_A as long as the S parameter of This_A and My_A
> > match... So the question now is whether My_A.S_Of and "whatever"
> > statically denote the same constant, or are static expressions with
> the
> > same value (see 12.7(6)).
> > ...
> > To summarize my conclusions: (1) Your example *should* be legal, ...
>
> I'm not so sure since generic parameters are never static. The actual
> could be an expression like
>
> S => Function_Call & Another_Non_Static;

That's one of the reasons the phrase "when they're referred to outside
the generic" showed up in my previous post.  Inside the generic, I
think the *intent* is that when the generic refers to S, the rules
apply without reference to what the actual for S might be.  But
outside the instance (and, in some cases, inside the visible part of
the instance), the rules apply as if S had been replaced by the
actual, and the properties of the actual can be used.  I think that's
the very general principle involved; I'm not saying that the rules as
written entirely implement this principle.  But here's an example of
what I mean:

   generic
        type T1 is private;
   package Pak1 is
        subtype T2 is T1;

...

   package New_Pak1 is new Pak1 (Integer);

Inside Pak1, you can't refer to T2'Succ, since the generic isn't
"allowed to know" that T2 will be an integer subtype; but outside the
generic, New_Pak1.T2'Succ is certainly legal.  Following the same
general principle, my thought was that if A is instantiated (as My_A)
with S=>"string literal", then while the code inside A would not be
allowed to treat S as static, for code *outside* the generic, My_A.S
(or in this case My_A.S_Of, which renames it) would be considered
static since the actual parameter is a static expression.  However,
I'm not saying that the actual rules about generic objects and static
stuff (4.9) support the general principle in this case.  It may be
that My_A.S_Of is not static and doesn't statically denote anything;
and it may be that the rules are too ambiguous as currently written.
I'm not sure, and I may need to study the RM a bit more closely.

                       -- Adam




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

end of thread, other threads:[~2007-07-25 15:13 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-23 18:20 Recursive parameters in generics Dmitry A. Kazakov
2007-07-23 19:32 ` Maciej Sobczak
2007-07-23 19:46   ` Dmitry A. Kazakov
2007-07-23 21:51 ` Adam Beneschan
2007-07-24  8:09   ` Dmitry A. Kazakov
2007-07-24 16:00     ` Adam Beneschan
2007-07-25  6:43       ` AW: " Grein, Christoph (Fa. ESG)
2007-07-25 15:13         ` Adam Beneschan

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