comp.lang.ada
 help / color / mirror / Atom feed
* Using a generic instance to implement a public subprogram?
@ 2008-02-03 18:35 Peter C. Chapin
  2008-02-03 19:27 ` Gautier
  2008-02-05  1:58 ` Randy Brukardt
  0 siblings, 2 replies; 5+ messages in thread
From: Peter C. Chapin @ 2008-02-03 18:35 UTC (permalink / raw)


I'm trying to implement a public subprogram (actually several) by 
instantiating a generic. I'm getting an ambiguous (to me) error message 
from GNAT GPL 2007. Here is a complete, compilable example that contains 
the essence of what I'm trying to do:

package Check is
    type Numeric_Type is digits 12;
    type Vector is private;

    function Sqrt(V : Vector) return Vector;
    -- Imagine other operations like Sqrt.

private
    type Vector is array(0..15) of Numeric_Type;

end Check;

The package body defines a generic function for applying an operation to 
a vector and then tries to instantiate it in order to implement function 
Sqrt.

with Ada.Numerics.Generic_Elementary_Functions;

package body Check is

    package Elementary_Functions is
       new Ada.Numerics.Generic_Elementary_Functions(Numeric_Type);

    generic
       with function Operation(Value : Numeric_Type) return Numeric_Type;
    function Unary_Operation(V : Vector) return Vector;

    function Unary_Operation(V : Vector) return Vector is
       Result : Vector;
    begin
       for I in 0 .. 15 loop
          Result(I) := Operation(V(I));
       end loop;
       return Result;
    end;

    function Sqrt is
       new Unary_Operation(Operation => Elementary_Functions.Sqrt);
end Check;

GNAT says this:

check.adb:3:14: missing body for "Sqrt" declared at check.ads:5
check.adb:21:13: "Sqrt" conflicts with declaration at check.ads:5
check.adb:21:13: instantiation cannot provide body for it

The first message I understand. The last two I'm not clear about. Is 
GNAT telling me that my declarations conflict in some way and because of 
that it can't do the instantiation, or is the instantiation the *cause* 
of the conflict? Is it possible to do what I'm trying to do here?

Thanks!

Peter



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

* Re: Using a generic instance to implement a public subprogram?
  2008-02-03 18:35 Using a generic instance to implement a public subprogram? Peter C. Chapin
@ 2008-02-03 19:27 ` Gautier
  2008-02-03 19:46   ` Peter C. Chapin
  2008-02-05  1:58 ` Randy Brukardt
  1 sibling, 1 reply; 5+ messages in thread
From: Gautier @ 2008-02-03 19:27 UTC (permalink / raw)


Peter C. Chapin:

...
> GNAT says this:
> 
> check.adb:3:14: missing body for "Sqrt" declared at check.ads:5
> check.adb:21:13: "Sqrt" conflicts with declaration at check.ads:5
> check.adb:21:13: instantiation cannot provide body for it
> 
> The first message I understand. The last two I'm not clear about. Is 
> GNAT telling me that my declarations conflict in some way and because of 
> that it can't do the instantiation, or is the instantiation the *cause* 
> of the conflict? Is it possible to do what I'm trying to do here?

No idea whether GNAT is right or not, but the following works (I came across the 
same problem at least one time...):

    function Sqrt_internal is
       new Unary_Operation(Operation => Elementary_Functions.Sqrt);

    function Sqrt(V : Vector) return Vector
       renames Sqrt_internal;

Note that the compilation of your code with ObjectAda 7.2.2 fails too; it says 
the following:
"
check.adb: Error: line 21 col 13 LRM:8.3(26), Illegal to override declaration in 
same region, Introducing new declaration anyway

check.adb: Error: line 27 col 1 LRM:3.11.1(6), Completion required for 
specification 'Sqrt', Continuing

Front end of ..\..\check.adb failed with 2 errors.
"
______________________________________________________________
Gautier         -- http://www.mysunrise.ch/users/gdm/index.htm
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



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

* Re: Using a generic instance to implement a public subprogram?
  2008-02-03 19:27 ` Gautier
@ 2008-02-03 19:46   ` Peter C. Chapin
  2008-02-03 20:37     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 5+ messages in thread
From: Peter C. Chapin @ 2008-02-03 19:46 UTC (permalink / raw)


Gautier wrote:


> No idea whether GNAT is right or not, but the following works (I came 
> across the same problem at least one time...):
> 
>    function Sqrt_internal is
>       new Unary_Operation(Operation => Elementary_Functions.Sqrt);
> 
>    function Sqrt(V : Vector) return Vector
>       renames Sqrt_internal;

Cool. I'll give that a try.

> Note that the compilation of your code with ObjectAda 7.2.2 fails too; 
> it says the following:
> "
> check.adb: Error: line 21 col 13 LRM:8.3(26), Illegal to override 
> declaration in same region, Introducing new declaration anyway

So it seems like the instantiation is regarded as a different thing than 
the subprogram mentioned in the specification. Yet because it has the 
same profile as the subprogram in the specification it "conflicts" with 
that earlier declaration. ObjectAda's use of the word "override" seems 
confusing; I don't think this has anything to do with overriding 
primitive operations. Or does it? Hmmm.

Thanks for your suggestion.

Peter



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

* Re: Using a generic instance to implement a public subprogram?
  2008-02-03 19:46   ` Peter C. Chapin
@ 2008-02-03 20:37     ` Dmitry A. Kazakov
  0 siblings, 0 replies; 5+ messages in thread
From: Dmitry A. Kazakov @ 2008-02-03 20:37 UTC (permalink / raw)


On Sun, 03 Feb 2008 14:46:23 -0500, Peter C. Chapin wrote:

> Gautier wrote:
> 
>> No idea whether GNAT is right or not, but the following works
[...]
> Cool. I'll give that a try.

Providing an implementation through renaming is 100% legal.
 
> So it seems like the instantiation is regarded as a different thing than 
> the subprogram mentioned in the specification.

Yes

"A generic_instantiation declares an instance; it is equivalent to the
instance declaration (a package_declaration or subprogram_declaration)
immediately followed by the instance body, both at the place of the
instantiation." (RM 12.3)

Because an instantiation is both a declaration and the implementation of,
it cannot serve as an implementation of anything else.

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



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

* Re: Using a generic instance to implement a public subprogram?
  2008-02-03 18:35 Using a generic instance to implement a public subprogram? Peter C. Chapin
  2008-02-03 19:27 ` Gautier
@ 2008-02-05  1:58 ` Randy Brukardt
  1 sibling, 0 replies; 5+ messages in thread
From: Randy Brukardt @ 2008-02-05  1:58 UTC (permalink / raw)


"Peter C. Chapin" <pchapin@sover.net> wrote in message
news:47a609b8$0$23664$4d3efbfe@news.sover.net...
...
> GNAT says this:
>
> check.adb:3:14: missing body for "Sqrt" declared at check.ads:5
> check.adb:21:13: "Sqrt" conflicts with declaration at check.ads:5
> check.adb:21:13: instantiation cannot provide body for it
>
> The first message I understand. The last two I'm not clear about. Is
> GNAT telling me that my declarations conflict in some way and because of
> that it can't do the instantiation, or is the instantiation the *cause*
> of the conflict? Is it possible to do what I'm trying to do here?

An instantiation cannot complete another declaration. You have to use a body
renaming for that, as someone else suggested.

Formally, an instantiation cannot be a completion. (See 3.11.1 for a
definition of completion.)

                    Randy.





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

end of thread, other threads:[~2008-02-05  1:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-03 18:35 Using a generic instance to implement a public subprogram? Peter C. Chapin
2008-02-03 19:27 ` Gautier
2008-02-03 19:46   ` Peter C. Chapin
2008-02-03 20:37     ` Dmitry A. Kazakov
2008-02-05  1:58 ` Randy Brukardt

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