comp.lang.ada
 help / color / mirror / Atom feed
* Generic Instantiation as Subprogram Body?
@ 1992-10-08 18:00 Douglas N. Surber
  0 siblings, 0 replies; 4+ messages in thread
From: Douglas N. Surber @ 1992-10-08 18:00 UTC (permalink / raw)


The following code is what I want to write.  It doesn't work and I don't 
like that!

    generic
	type T is range <>;
    package P is

	type U is private;
	
	function "<" (l,r : U) return boolean;
	function "<=" (l,r : U) return boolean;
	function ">" (l,r : U) return boolean;
	function ">=" (l,r : U) return boolean;

    private
	
	type U is new T;    -- or whatever
    
    end P;

    package body P is

	generic
	    with function op(l,r : T) return boolean;
	function rel (l,r : U) return boolean;

	-- the following doesn't work. declared function name hides the
	-- generic actual.
	function "<" is new rel("<");
	function "<=" is new rel("<=");
	function ">" is new rel(">");
	function ">=" is new rel(">=");

	function rel (l,r : U) return boolean is
	begin
	    return op(l, r);   -- or whatever
	end rel;

    end P;

Neither of the following kludges works since neither a rename nor a generic 
instantiation is a subprogram body and a package body must have a subprogram
body for every subprogram spec in the package spec. The new function declared
in the second line is a different function from the one with the same name
and signature declared in the spec and so fails.

    function lt (l,r : T) return boolean renames "<";
    function "<" is new rel(lt);


    function lt is new rel("<");
    function "<" (l,r : U) return boolean renames lt;

You can't imagine how irritating it is to be told that there is no body for
function "<" and that function "<" has the same name and the same signature
as the previously declared function. "No kiddin'! You think that's an
accident? Stoopid machine!"

One thing that seems to work is to put one of the previous kludges in
the package spec. That sucks!  The fact that the relational operators are
instances of a generic function is strictly an implementation detail.  
What's more, I don't want the generic function rel to be part of the spec.

Of course the following works, but it's an even bigger kludge than the others
and its efficiency depends on the compiler's ability to optimize out a lot of
overhead.

    function lt is new rel("<");
    pragma inline (lt);
    function "<" (l,r : U) return boolean is
    begin
	return lt (l, r);
    end "<";

Three questions:

    1) Is there a better way to accomplish this without changing the
       package spec?

    2) If not, why not?  (What is the rationale, not what does the LRM say.)

    3) Is this fixed in 9X? (hint, hint)

Douglas Surber
Lockheed
Houston, TX

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

* Re: Generic Instantiation as Subprogram Body?
@ 1992-10-08 19:54 dog.ee.lbl.gov!hellgate.utah.edu!caen!zaphod.mps.ohio-state.edu!hobbes.ph
  0 siblings, 0 replies; 4+ messages in thread
From: dog.ee.lbl.gov!hellgate.utah.edu!caen!zaphod.mps.ohio-state.edu!hobbes.ph @ 1992-10-08 19:54 UTC (permalink / raw)


>From article <dnsurber.718567247@node_26400>,
by dnsurber@lescsse.jsc.nasa.gov (Douglas N. Surber):
> 
>     generic
> 	type T is range <>;
>     package P is
> 	type U is private;
> 	function "<" (l,r : U) return boolean;
>     private
> 	type U is new T;    -- or whatever
>     end P;
> 
>     package body P is
> 
> 	-- the following doesn't work. declared function name hides the
> 	-- generic actual.
> 	function "<" is new ...
> 
>     end P;

I've been bothered by this too.  The only solution I've found that works
is to write some long-winded boilerplate in the package body; specifically,
You have to write it like this:

      package body P is

	function "<" (l,r : U) is
	begin
	    return T'(l) < T'(r)
	end "<";

      end P;

That is, you declare a stubby procedure that explicitly coerces its two
parameters of type U to the base type T, then applies the < operation you
want for that base type.

I run into this most often when I use a generic list type in the
a package that exports list operations.  I'd like to just export the
generic instantiations of the list operations on a specific type, but
I can't do it directly; instead, I have to package the exported operations
with annoying little functions like the one shown above.

Of course, this function can be "inlined", so it should cost nothing at
run-time, but these things make for pages of unnecessary source code!

					Doug Jones
					jones@cs.uiowa.edu

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

* Re: Generic Instantiation as Subprogram Body?
@ 1992-10-14 17:03 yale.edu!jvnc.net!netnews.upenn.edu!uofs!guinness.cs.uofs.edu!beidler
  0 siblings, 0 replies; 4+ messages in thread
From: yale.edu!jvnc.net!netnews.upenn.edu!uofs!guinness.cs.uofs.edu!beidler @ 1992-10-14 17:03 UTC (permalink / raw)


In article <dnsurber.718567247@node_26400>, dnsurber@lescsse.jsc.nasa.gov (Doug
las N. Surber) writes:
|> 
|> The following code is what I want to write.  It doesn't work and I don't 
|> like that!
|> 
|>     generic
|> 	type T is range <>;
|>     package P is
|> 
|> 	type U is private;
|> 	
|> 	function "<" (l,r : U) return boolean;
|> 	function "<=" (l,r : U) return boolean;
|> 	function ">" (l,r : U) return boolean;
|> 	function ">=" (l,r : U) return boolean;
|> 
|>     private
|> 	 . . .

Since "<=" is equivalent to "not >" and ">=" is equivalent to "not <"

the package spec should be

     generic
 	type T is range <>;
     package P is
 
 	type U is private;
 	
 	function "<" (l,r : U) return boolean;
 	function ">" (l,r : U) return boolean;
 
     private
 	 . . .

Is it "Stoopid machine!" or  "Stoopid _____!"

In the package body

|> 
|> 	generic
|> 	    with function op(l,r : T) return boolean;
|> 	function rel (l,r : U) return boolean;
|> 
|> 	-- the following doesn't work. declared function name hides the
|> 	-- generic actual.
|> 	function "<" is new rel("<");
|> 	function "<=" is new rel("<=");
|> 	function ">" is new rel(">");
|> 	function ">=" is new rel(">=");
|> 
|> 	function rel (l,r : U) return boolean is
|> 	begin
|> 	    return op(l, r);   -- or whatever
|> 	end rel;
|> 
|>     end P;

since you have not shown what type "U" is, I am only guessing,
but if "U" is a record or array, "<", etc. are not defined, 
hence "<" has no meaning for type "U".  Insteaad of the contrived
generic function rel, supply the bodies of the ordering functions
for the type U,

  function "<" (l, r : U) return boolean is

     begin
        . . .

     end "<":

-- 
+------------------------------------------------------------------+
|  John (Jack) Beidler				                   |
|  Prof. of Computer Science Internet: BEIDLER@JAGUAR.UOFS.ED      |
|  University of Scranton              beidler@guinness.cs.uofs.edu|
|  Scranton, PA 18510	      Bitnet : BEIDLER@SCRANTON            |
|                                                                  |
|          Phone: (717) 941-7446	 FAX:   (717) 941-4250     |
+------------------------------------------------------------------+

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

* Re: Generic Instantiation as Subprogram Body?
@ 1992-10-15  8:50 darwin.sura.net!paladin.american.edu!news.univie.ac.at!hp4at!mcsun!uknet!
  0 siblings, 0 replies; 4+ messages in thread
From: darwin.sura.net!paladin.american.edu!news.univie.ac.at!hp4at!mcsun!uknet! @ 1992-10-15  8:50 UTC (permalink / raw)


In article <11225@platypus.uofs.uofs.edu> beidler@guinness.cs.uofs.edu (Jack Be
idler) writes:
   Since "<=" is equivalent to "not >" and ">=" is equivalent to "not <"
   the package spec should be

	generic
	   type T is range <>;
	package P is
	   type U is private;
	   function "<" (l,r : U) return boolean;
	   function ">" (l,r : U) return boolean;
	private
	    . . .

   Is it "Stoopid machine!" or  "Stoopid _____!"

If you want to reduce it to its minimum: "a < b" is equivalent to "b >
a" the package spec should be :-

  generic
    type T is range <>;
  package PARTIAL_ORDER
    type U is private;
    function "<" (l,r:U) return boolean;
  private
    ....
  end

As to what/who is stupid, I'll leave that for others to decide :-)

bevan

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

end of thread, other threads:[~1992-10-15  8:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1992-10-14 17:03 Generic Instantiation as Subprogram Body? yale.edu!jvnc.net!netnews.upenn.edu!uofs!guinness.cs.uofs.edu!beidler
  -- strict thread matches above, loose matches on Subject: below --
1992-10-15  8:50 darwin.sura.net!paladin.american.edu!news.univie.ac.at!hp4at!mcsun!uknet!
1992-10-08 19:54 dog.ee.lbl.gov!hellgate.utah.edu!caen!zaphod.mps.ohio-state.edu!hobbes.ph
1992-10-08 18:00 Douglas N. Surber

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