comp.lang.ada
 help / color / mirror / Atom feed
* Constructing a modular type based on the size of an arbitrary type
@ 1999-09-14  0:00 Mike Bates
  1999-09-14  0:00 ` Mark A Biggar
  0 siblings, 1 reply; 3+ messages in thread
From: Mike Bates @ 1999-09-14  0:00 UTC (permalink / raw)


I am writing a generic package in which I need to be able to construct
a modular type with the same size in bits as an arbitrary packed type
passed in as a generic parameter.  (This is to be able to do unchecked
conversions between the arbitrary type and modular types; so they need
to be the same size.)  This is what I tried:

generic
    type Arbitrary_Type is private;
package Foo is

    type Modular_Type is mod Arbitrary_Type'Size ** 2;

end Foo;

The definition of Modular_Type fails because the value
Arbitrary_Type'Size ** 2 is not static.  (This surprises me, because
the value is determinable at compile time.)

I also tried the following, relying on the instantiator to pass in
the value of Arbitrary_Type'Size ** 2:

generic
    Arbitrary_Modulus : Natural;
package Foo is

    type Modular_Type is mod Arbitrary_Type;

end Foo;

This also fails, again because the modulus expression is not
static, even though it seems that it should determinable at compile
time.  It fails even if I use a numeric literal as the actual
parameter in the instantiation.

Is there any way to construct a modular type to match the size of
an arbitrary type?  Or is this as impossible as trisecting the angle?

Mike Bates
FlightSafety Simulation
mikeb@ssd.fsi.com





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

* Re: Constructing a modular type based on the size of an arbitrary type
  1999-09-14  0:00 Constructing a modular type based on the size of an arbitrary type Mike Bates
@ 1999-09-14  0:00 ` Mark A Biggar
  1999-09-14  0:00   ` Robert I. Eachus
  0 siblings, 1 reply; 3+ messages in thread
From: Mark A Biggar @ 1999-09-14  0:00 UTC (permalink / raw)


Mike Bates wrote:
> 
> I am writing a generic package in which I need to be able to construct
> a modular type with the same size in bits as an arbitrary packed type
> passed in as a generic parameter.  (This is to be able to do unchecked
> conversions between the arbitrary type and modular types; so they need
> to be the same size.)  This is what I tried:
> 
> generic
>     type Arbitrary_Type is private;
> package Foo is
> 
>     type Modular_Type is mod Arbitrary_Type'Size ** 2;
> 
> end Foo;
> 
> The definition of Modular_Type fails because the value
> Arbitrary_Type'Size ** 2 is not static.  (This surprises me, because
> the value is determinable at compile time.)
> 
> I also tried the following, relying on the instantiator to pass in
> the value of Arbitrary_Type'Size ** 2:
> 
> generic
>     Arbitrary_Modulus : Natural;
> package Foo is
> 
>     type Modular_Type is mod Arbitrary_Type;
> 
> end Foo;
> 
> This also fails, again because the modulus expression is not
> static, even though it seems that it should determinable at compile
> time.  It fails even if I use a numeric literal as the actual
> parameter in the instantiation.
> 
> Is there any way to construct a modular type to match the size of
> an arbitrary type?  Or is this as impossible as trisecting the angle?

Unfortunately, I beleive the only way to handle this is to move the 
burden of defining the appropriate modular type out of the generic
and pass it in as a generic parameter along with the conversion
functions.

generic
	type Arbitrary_type is private;
	type Abritrary_mod is mod <>;
    with function To(X: Arbitrary_Type) return Arbitrary_Mod;
    with function From(X: Arbitrary_Mod) return Arbitrary_Type;
package Foo is
	...
end Foo;

If you want to make sure it is set up right you can have package Foo
check
at run time in the body.

package body Foo is
	...
begin
	if Arbitrary_Type'SIZE /= Arbitrary_Mod'SIZE then
		raise Constraint_Error;
	end if;
end Foo;

Yes, I know that this moves a lot of the provate stuff of the package
outside, but I don't beleive there is any other way.

--
Mark Biggar
mark.a.biggar@lmco.com




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

* Re: Constructing a modular type based on the size of an arbitrary type
  1999-09-14  0:00 ` Mark A Biggar
@ 1999-09-14  0:00   ` Robert I. Eachus
  0 siblings, 0 replies; 3+ messages in thread
From: Robert I. Eachus @ 1999-09-14  0:00 UTC (permalink / raw)




Mark A Biggar wrote:
 
> Unfortunately, I beleive the only way to handle this is to move the
> burden of defining the appropriate modular type out of the generic
> and pass it in as a generic parameter along with the conversion
> functions.

  You don't have to go quite that far.  There is no way to pass the
modulus as a generic formal, since generic formals are never static. 
You could declare the modulus in an outer scope, but the best solution
is probably to pass in the modulus as an attribute of a generic formal. 
Huh?  Simple example:

    generic
      type Modular is mod <>;
    package Foo is
      ...
      type Generic_Mod is new Modular;
      ...
    end Foo;

    At worst this requires declaring a "extra" modular type that is
never used elsewhere just before each generic instantiation.

-- 

                                        Robert I. Eachus

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




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

end of thread, other threads:[~1999-09-14  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-09-14  0:00 Constructing a modular type based on the size of an arbitrary type Mike Bates
1999-09-14  0:00 ` Mark A Biggar
1999-09-14  0:00   ` Robert I. Eachus

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