comp.lang.ada
 help / color / mirror / Atom feed
* Re: generics using generics (long)
       [not found] <CvqEyG.nD@taurus.cs.nps.navy.mil>
@ 1994-09-07  9:38 ` Robert I. Eachus
  1994-09-07 16:54 ` Tucker Taft
  1 sibling, 0 replies; 2+ messages in thread
From: Robert I. Eachus @ 1994-09-07  9:38 UTC (permalink / raw)



    Try this:

  -- A Generic 1-D floating point array type
  generic
    type Real_Number is digits <>;
  package G_1D_Array is
    type One_D_Array is array(INTEGER range<>) of Real_Number;
    type One_D_Array_Handle is access One_D_Array;
    -- then a bunch of procedure specifications for I/O and
    -- reallocating the array on the fly, the usual kind of stuff such as...
    function Allocate(Arr: One_D_Array) return One_D_Array_Handle;
  end G_1D_Array;

  -- A Generic Package for fast Hartley transforms, intended to work
  -- with any float type 
  generic
    type Real_Number is digits <>;
    type FHT_Array  is array (Integer range <>) of Real_Number;
    type FHT_Handle is access FHT_Array;
    with function Allocate(Arr: FHT_Array) return FHT_Handle is <>;
    -- and so on to match G_1D_Array...
  package Hartley is
    procedure Normalize(Fx : FHT_Handle);
    procedure FHT(Fx : FHT_Handle );
  end Hartley;

  with Hartley, G_1D_Array;
  procedure FHT_Main is
    type Real is new Long_Float;
    package Real_Vector  is new G_1D_Array(Real);
    use Real_Vector; -- needed to make the line below work right.
    package Real_Hartley is new
      Hartley(Real,One_D_Array,One_D_Array_Handle);
    Fx: Real_Vector.One_D_Array_Handle;
    -- ...etc...
  begin
    -- code to do lots of things with the 1D Array fx, among
    -- them calculating its Hartley transform.
    null;     
  end FHT_Main;

    Ada9X can make things cleaner with package parameters, but this
should do the job.  And yes I agree that the "tricks" that are needed
to make things work should be taught.  However, to my knowledge no one
has ever taught a course devoted to the use of generics in Ada 83, and
this really needs to be a separate course taught six months of using
the "Pascal Superset" of Ada 83.  Maybe with Ada 9X someone will put
together such a couse as a complement to an Ada and OOP course.
(Generics should be IMHO taught first, but with Ada 9X generics are a
lot richer...)

--

					Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...



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

* Re: generics using generics (long)
       [not found] <CvqEyG.nD@taurus.cs.nps.navy.mil>
  1994-09-07  9:38 ` generics using generics (long) Robert I. Eachus
@ 1994-09-07 16:54 ` Tucker Taft
  1 sibling, 0 replies; 2+ messages in thread
From: Tucker Taft @ 1994-09-07 16:54 UTC (permalink / raw)


In article <CvqEyG.nD@taurus.cs.nps.navy.mil>,
Skip Carter <skip@taygeta.oc.nps.navy.mil> wrote:

>	I am trying to figure out how to design some generic code to work
>	with other generics as their parameters but can't figure out a clean way
>	to do it.
>
>	Here is what I am trying to do:
>
>
>-- A Generic 1-D floating point array type
>generic
>RealNumber is digits<>;
 ^^ type
>
>Package G1DArray is
>	type 1DArray is array(INTEGER range<>) of RealNumber;
>	type 1DArrayHandle is access 1DArrray;
>
>	-- then a bunch of procedure specifications 
>       -- for I/O and reallocating the array on the fly,
>	-- the usual kind of stuff...
>
>end G1DArray;
>
>
>-- A Generic Package for fast Hartley transforms, 
>-- intended to work with any float type
>with G1DArray;
>
>generic
>Type RealNumber is digits<>;
>Type FHT_Array  is new G1DArray(RealNumber);         -- this is wrong, 
>                                             -- but it is the kind of thing
>				                   -- that I have in mind

In Ada 9X, you can import an instance of a generic.  This would
allow you to replace both of the above formal parameters with
the single "formal package" parameter:

   with package FHT is new G1DArray(<>);
   use FHT;  -- This makes FHT.1DArray and FHT.1DArrayHandle directly visible

>Package Hartley is
>		procedure Normalize(fx : 1DArrayHandle);
>		procedure FHT( fx : 1DArrayHandle );
>end Hartley;

You instantiate Hartley by first instantiating G1DArray,
and then passing that instance in an instantiation of Hartley.
For example:

    package Float_Vector is new G1DArray(Float);
    package Float_Hartley is new Hartley(Float_Vector);

>So an application would look like:
>
>with Hartley, G1DArray
>
>procedure FHTMAIN is
>	type Real is new long_float;
>
>	package Real_Vector  is new G1DArray( Real );
>
>	package Real_Hartley is new Hartley(Real,Real_Vector);   
>                                  -- this is how I'd LIKE to be
>                                 -- able to instantiate the package

As indicated above, if you use a formal package parameter,
this instantiation would be:

        package Real_Hartley is new Hartley(Real_Vector);

>	fx    : 1DArrayHandle;
>
>
>	-- ...etc...
>
>	-- code to do lots of things with the 1D Array fx, 
>       -- among them calculating
>	-- its hartley transform
>
>end FHTMAIN;

> ...
>	How do I specify things to work the other way around ?

Formal packages were provided in Ada 9X to solve this problem.

>	Is there is good book out there that teaches Ada idioms, 
>       as opposed to grammar,
>	that can help with this kind of question ?

There are a number of good Ada books that include examples
of using Ada.  See the comp.lang.ada FAQ (on ajpo.sei.cmu.edu
and the EPFL WWW server).  Some of them already cover Ada 9X
features (e.g. John Barnes 4th edition of "Programming in Ada").

> Everett (Skip) Carter        Phone:  408-656-3318 FAX: 408-656-2712
> Naval Postgraduate School    INTERNET: skip@taygeta.oc.nps.navy.mil

-Tucker Taft   stt@inmet.com



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

end of thread, other threads:[~1994-09-07 16:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CvqEyG.nD@taurus.cs.nps.navy.mil>
1994-09-07  9:38 ` generics using generics (long) Robert I. Eachus
1994-09-07 16:54 ` Tucker Taft

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