generics using generics (long) 
Author Message
 generics using generics (long)

        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<>;

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

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

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

        fx    : 1DArrayHandle;

        -- ...etc...

        -- code to do lots of things with the 1D Array fx, among them calculating
        -- its hartley transform

end FHTMAIN;

I can do the FHT package like,

with G1DArray;

generic
RealNumber is digits<>;

Package Hartley is
        package FHT_Array is new G1DArray(RealNumber);
        use FHT_Array;

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

        But now the array fx in the main procedure is a FHT_Array.
        So that EVERYTHING that uses the array fx has to be defined to work
        with that type.  This is backwards from my intent, I want the FHT package
        to accomodate the demands of the main procedure NOT the other way around.

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

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

--
 Everett (Skip) Carter        Phone:  408-656-3318 FAX: 408-656-2712

 Dept. of Oceanography, Code OC/CR  UUCP:     ...!uunet!taygeta!skip
 Monterey, CA. 93943          TELEMAIL: s.carter/omnet
                  WWW: http://www.*-*-*.com/



Sun, 23 Feb 1997 07:37:27 GMT  
 generics using generics (long)

    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...



Sun, 23 Feb 1997 17:38:51 GMT  
 generics using generics (long)

Quote:

>    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

Quote:
>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);

Quote:
>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);

Quote:
>    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.

Quote:
>    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").

Quote:
> Everett (Skip) Carter        Phone:  408-656-3318 FAX: 408-656-2712





Mon, 24 Feb 1997 00:54:53 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. generics defined in terms of generics?

2. Ada '0x generics brain-fartz (long)

3. ASIS problem with generics (Long)

4. instantiation using generics

5. Errors when synthesizing using generics but not during synthessi

6. Generics used in Case statement

7. Using generics

8. Using generics to define expandable logic.

9. new C# language features - Generics, Anonymous Methods - Partial Types

10. help with generics

11. Conformance of constrained formal generics

12. conformance of generics

 

 
Powered by phpBB® Forum Software