comp.lang.ada
 help / color / mirror / Atom feed
* Incomplete types used with generics
@ 2015-05-11 22:37 Jeremiah
  2015-05-12  1:43 ` Randy Brukardt
  2015-05-12 21:49 ` Randy Brukardt
  0 siblings, 2 replies; 9+ messages in thread
From: Jeremiah @ 2015-05-11 22:37 UTC (permalink / raw)


One of the things I read about with ada2012 is the ability to use incomplete types as generic parameters.  However, I am having some trouble implementing them.  I'm using GNAT GPL 2014 and have the following dumbed down packages:

generic
   type Item_Type is tagged limited private;
   type Item_Access is access Item_Type;
package Test_Generic is
   type Test is tagged record
      Ref : Item_Access := null;
   end record;
end Test_Generic;


with Test_Generic;

package Test_Nodes is

   type Node3_ptr is null record;

   type Node1_Type is tagged record
      value : Integer := 0;
   end record;
   type Node1_Access is access Node1_Type;

   package N1 is new Test_Generic(Node1_Type,Node1_Access);

   type Node2_Type is tagged;
   type Node2_Access is access Node2_Type;

   package N2 is new Test_Generic(Node2_Type,Node2_Access);

   type Node2_Type is tagged record
      value : Integer := 1;
      Test  : N2.Test;
   end record;

end Test_Nodes;

However, the line 
package N2 is new Test_Generic(Node2_Type,Node2_Access);

fails with:  premature use of incomplete type

I *think* the generic parameter "type Item_Type is tagged limited private;" is considered incomplete as is "type Node2_Type is tagged;".

What am I missing?

Ada2012 incomplete types with generics reference:
http://www.ada-auth.org/standards/12rat/html/Rat12-4-3.html

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

* Re: Incomplete types used with generics
  2015-05-11 22:37 Incomplete types used with generics Jeremiah
@ 2015-05-12  1:43 ` Randy Brukardt
  2015-05-12  7:33   ` Simon Wright
  2015-05-12 11:46   ` Jeremiah
  2015-05-12 21:49 ` Randy Brukardt
  1 sibling, 2 replies; 9+ messages in thread
From: Randy Brukardt @ 2015-05-12  1:43 UTC (permalink / raw)


"Jeremiah" <jeremiah.breeden@gmail.com> wrote in message 
news:830c0323-a06c-4884-9214-89f3ee6f17b5@googlegroups.com...
> One of the things I read about with ada2012 is the ability to use 
> incomplete types
> as generic parameters. However, I am having some trouble implementing 
> them.

...
> What am I missing?

You missed at least 6 months of wrestling with various AdaCore people about 
this feature, specifically over some proposed ACATS tests. (Most of the 
tests showed lovely GNAT bug boxes.) They were trying very hard to find 
reasons that the tests were illegal, because there was an problem with the 
design of their implementation that made the tests very hard to implement. 
They managed to uncover some language problems that will probably be fixed 
someday, but in particular, everyone eventually agreed that the test using 
limited with (CC51010) is correct (now). I've heard that they've fixed the 
compiler so that test works, so you'll probably be able to get that to work 
at some future point (but surely not with GNAT GPL 2014, which clearly 
predates the fixes).

I suspect that you ran afoul of the fact that the freezing rules differ for 
tagged incomplete vs. untagged incomplete types, but even if you fixed that 
you'd still be very likely to run into problems with current GNAT versions 
unless you are a GNAT PRO user and can get a wavefront.

I'd suggest trying this again next year.

                                Randy.


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

* Re: Incomplete types used with generics
  2015-05-12  1:43 ` Randy Brukardt
@ 2015-05-12  7:33   ` Simon Wright
  2015-05-12 11:48     ` Jeremiah
  2015-05-12 11:46   ` Jeremiah
  1 sibling, 1 reply; 9+ messages in thread
From: Simon Wright @ 2015-05-12  7:33 UTC (permalink / raw)


"Randy Brukardt" <randy@rrsoftware.com> writes:

> I suspect that you ran afoul of the fact that the freezing rules
> differ for tagged incomplete vs. untagged incomplete types, but even
> if you fixed that you'd still be very likely to run into problems with
> current GNAT versions unless you are a GNAT PRO user and can get a
> wavefront.
>
> I'd suggest trying this again next year.

Don't bother to try with FSF GCC 5! Maybe GNAT GPL 2015 ...


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

* Re: Incomplete types used with generics
  2015-05-12  1:43 ` Randy Brukardt
  2015-05-12  7:33   ` Simon Wright
@ 2015-05-12 11:46   ` Jeremiah
  1 sibling, 0 replies; 9+ messages in thread
From: Jeremiah @ 2015-05-12 11:46 UTC (permalink / raw)


On Monday, May 11, 2015 at 9:43:55 PM UTC-4, Randy Brukardt wrote:
> You missed at least 6 months of wrestling with various AdaCore people about 
> this feature, specifically over some proposed ACATS tests. (Most of the 
> tests showed lovely GNAT bug boxes.) They were trying very hard to find 
> reasons that the tests were illegal, because there was an problem with the 
> design of their implementation that made the tests very hard to implement. 
> They managed to uncover some language problems that will probably be fixed 
> someday, but in particular, everyone eventually agreed that the test using 
> limited with (CC51010) is correct (now). I've heard that they've fixed the 
> compiler so that test works, so you'll probably be able to get that to work 
> at some future point (but surely not with GNAT GPL 2014, which clearly 
> predates the fixes).
Well that's definitely stinky.  Thanks for the info on that.

> I suspect that you ran afoul of the fact that the freezing rules differ for 
> tagged incomplete vs. untagged incomplete types, but even if you fixed that 
> you'd still be very likely to run into problems with current GNAT versions 
> unless you are a GNAT PRO user and can get a wavefront.
I'm definitely a novice at Ada compared to most here, so I will ask:  which freezing rules did I mess up on there with Node2_Type?  I am familiar with how freezing works to some extent (I usually mess it up but am able to tell when I can't override a procedure/function or use one as a primitive like I intended).  I was trying hard to not to invoke freezing too early.  I did the incomplete declaration, the access to it (should be good there?), and then assuming the compiler could do it (which you indicated it couldn't) I passed it as an incomplete type to a package instantiation.  Is there a better way to do that?  

Keep in mind my (previously unspecified) goal is to have a type that uses a smart access type (that's the generic) to reference itself.  Basically a node type that uses a smart access type to handle the references (with a weak reference to the parent of course).

Thanks again!


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

* Re: Incomplete types used with generics
  2015-05-12  7:33   ` Simon Wright
@ 2015-05-12 11:48     ` Jeremiah
  0 siblings, 0 replies; 9+ messages in thread
From: Jeremiah @ 2015-05-12 11:48 UTC (permalink / raw)


On Tuesday, May 12, 2015 at 3:33:48 AM UTC-4, Simon Wright wrote:
> Don't bother to try with FSF GCC 5! Maybe GNAT GPL 2015 ...

Thank you, I was gonna try that next, though it would have to be a lower version of FSF GCC for me.  I don't think 5.x is out for windows yet, but I haven't checked to be sure.


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

* Re: Incomplete types used with generics
  2015-05-11 22:37 Incomplete types used with generics Jeremiah
  2015-05-12  1:43 ` Randy Brukardt
@ 2015-05-12 21:49 ` Randy Brukardt
  2015-05-12 23:19   ` Jeremiah
  1 sibling, 1 reply; 9+ messages in thread
From: Randy Brukardt @ 2015-05-12 21:49 UTC (permalink / raw)


"Jeremiah" <jeremiah.breeden@gmail.com> wrote in message 
news:830c0323-a06c-4884-9214-89f3ee6f17b5@googlegroups.com...
...
> I *think* the generic parameter "type Item_Type is tagged limited 
> private;" is considered incomplete as is "type Node2_Type is tagged;".

On the the things that *I* missed is the above. No, private types aren't 
incomplete, and in fact even a private type cannot be used as the actual to 
a generic private type (the type is immediately frozen, but for a private 
type that is illegal, as per 13.14(17)).

If you want an incomplete formal parameter, you have to write it that way. 
That is:

    type Item_Type is tagged;

On the other side, *anything* can be the actual to a generic formal 
incomplete type. The type can be limited, incomplete, private, and/or 
abstract. Of course, that means there isn't much you can do with such a type 
(you'd have to pass in an allocator and deallocator subprogram, for 
instance). There's somewhat more you can do with a tagged incomplete type 
(in particular, you can make calls), but even that is very limiting. But of 
course that's the idea; if you can live with the limitations, you get the 
ability to instantiation the generic almost anywhere with any kind of type.

>> What am I missing?

That you didn't use an incomplete formal type, so OF COURSE the compiler 
rejected your instantiation with an incomplete type. Sorry about missing 
that yesterday; I was so excited that someone would actually try to use an 
incomplete formal that I failed to notice that you didn't actually do so. 
One does not think clearly when you are gloating. :-)

                                        Randy.










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

* Re: Incomplete types used with generics
  2015-05-12 21:49 ` Randy Brukardt
@ 2015-05-12 23:19   ` Jeremiah
  2015-05-14  1:14     ` Randy Brukardt
  0 siblings, 1 reply; 9+ messages in thread
From: Jeremiah @ 2015-05-12 23:19 UTC (permalink / raw)


On Tuesday, May 12, 2015 at 5:49:18 PM UTC-4, Randy Brukardt wrote:
> >> What am I missing?
> 
> That you didn't use an incomplete formal type, so OF COURSE the compiler 
> rejected your instantiation with an incomplete type. Sorry about missing 
> that yesterday; I was so excited that someone would actually try to use an 
> incomplete formal that I failed to notice that you didn't actually do so. 
> One does not think clearly when you are gloating. :-)
> 
>                                         Randy.

Do I at least get points for "trying" to use an incomplete formal?

Thanks for the explanation.  That makes better sense to me.  I didn't realize private types couldn't be incomplete types (I'm sure I read it in the RM, but I tend to learn better through trial/error then rereading than simply first pass reading).  

I've always seen the use limited private on generic parameters in most examples and tutorials.  Does removing the "limited private" from the generic parameters prevent me from having certain kinds of input types?  

I.E. does

generic
   type Item_Type(<>) is tagged limited private;

VS 

generic
   type Item_Type(<>) is tagged;

prevent me from using any specific types as input types to the package?  

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

* Re: Incomplete types used with generics
  2015-05-12 23:19   ` Jeremiah
@ 2015-05-14  1:14     ` Randy Brukardt
  2015-05-14 20:48       ` Jeremiah
  0 siblings, 1 reply; 9+ messages in thread
From: Randy Brukardt @ 2015-05-14  1:14 UTC (permalink / raw)


"Jeremiah" <jeremiah.breeden@gmail.com> wrote in message 
news:783ee7a2-e831-4e64-b736-abab0c7d71b7@googlegroups.com...
> On Tuesday, May 12, 2015 at 5:49:18 PM UTC-4, Randy Brukardt wrote:
>> >> What am I missing?
>>
>> That you didn't use an incomplete formal type, so OF COURSE the compiler
>> rejected your instantiation with an incomplete type. Sorry about missing
>> that yesterday; I was so excited that someone would actually try to use 
>> an
>> incomplete formal that I failed to notice that you didn't actually do so.
>> One does not think clearly when you are gloating. :-)
>>
> Do I at least get points for "trying" to use an incomplete formal?

Of course. That's what caused my gloating. :-)

> Thanks for the explanation.  That makes better sense to me.  I didn't 
> realize
>private types couldn't be incomplete types (I'm sure I read it in the RM, 
>but
>I tend to learn better through trial/error then rereading than simply first 
>pass
>reading).

Private types have more capabilities than incomplete types, so it follows 
that the actual could not be incomplete.

> I've always seen the use limited private on generic parameters in most 
> examples
> and tutorials.  Does removing the "limited private" from the generic 
> parameters
> prevent me from having certain kinds of input types?

That's backwards.

> I.E. does
>
> generic
>   type Item_Type(<>) is tagged limited private;
>
> VS
>
> generic
>   type Item_Type(<>) is tagged;
>
> prevent me from using any specific types as input types to the package?

No, it's the other way around. "is tagged limited private" requires the 
actual type to be a full type, a concrete type (not abstract) and a tagged 
type. "is tagged" only requires a tagged type (it could be private or 
incomplete, or an abstract type).

For Ada before Ada 2012, formal "limited private" was the most general thing 
available for a generic (thus the use in books and the like), but formal 
incomplete types are more general than that. Of course, they can only be 
used like an incomplete type, and that is very limiting. It's not yet clear 
that much beyond signature packages can be created with them.

                                           Randy.


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

* Re: Incomplete types used with generics
  2015-05-14  1:14     ` Randy Brukardt
@ 2015-05-14 20:48       ` Jeremiah
  0 siblings, 0 replies; 9+ messages in thread
From: Jeremiah @ 2015-05-14 20:48 UTC (permalink / raw)


On Wednesday, May 13, 2015 at 9:14:27 PM UTC-4, Randy Brukardt wrote:
> For Ada before Ada 2012, formal "limited private" was the most general thing 
> available for a generic (thus the use in books and the like), but formal 
> incomplete types are more general than that. Of course, they can only be 
> used like an incomplete type, and that is very limiting. It's not yet clear 
> that much beyond signature packages can be created with them.
> 
>                                            Randy.

That definitely makes sense.  When I moved from my simple example (modified to use actual incomplete types) to something I had that was more complex I already saw that in action.  I had a method that was used to deallocate the access type under certain conditions, which required the complete type for Unchecked_Deallocation.  I remodeled the generic package to account for that a different way.  Luckily that was my only thing that tried to improperly use the incomplete type.

Thanks again!

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

end of thread, other threads:[~2015-05-14 20:48 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-11 22:37 Incomplete types used with generics Jeremiah
2015-05-12  1:43 ` Randy Brukardt
2015-05-12  7:33   ` Simon Wright
2015-05-12 11:48     ` Jeremiah
2015-05-12 11:46   ` Jeremiah
2015-05-12 21:49 ` Randy Brukardt
2015-05-12 23:19   ` Jeremiah
2015-05-14  1:14     ` Randy Brukardt
2015-05-14 20:48       ` Jeremiah

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