comp.lang.ada
 help / color / mirror / Atom feed
* to type or subtype ? (novice question, three parts)
@ 2000-02-15  0:00 G
  2000-02-15  0:00 ` Tucker Taft
  0 siblings, 1 reply; 2+ messages in thread
From: G @ 2000-02-15  0:00 UTC (permalink / raw)


Hi all, and thanks so much for all your help. :-)
I hope this is all relevant to the newsgroup topic.
There are not as many diverse sources about Ada as there are
other languages, so it is sometimes difficult to get helpful
information.
I try to get it straight from the horse's mouth, as it were...

There are three questions in this message.
One is about types and subtypes.
Two is about signed modular integers.
Three is about a very little bit of MIL-STD-1553B.


------------------------------------------
------------------------------------------
1:
Ok, something I don't understand - when should you
make a new type, say :

type widget is new integer;

  or, say :

subtype widget is integer;

- when they are both integers.  That is -
what difference does it make with what you
can do with/to the type ?  I have been receiving
numerous mysterious errors in (yes, trivial) code I
write relating to types and subtypes and I am certain
that this is due to not knowing the range of relevance/application of
each.
(Love the LRM as I do *choke*, it doesn't explain in language
I understand :)

"A subtype of a given type is a combination of the type, a constraint on
values of the type, and certain attributes specific to the subtype. The
given type is called the type of the subtype. Similarly, the associated
constraint is called the constraint of the subtype. The set of values of
a subtype consists of the values of its type that satisfy its
constraint. Such values belong to the subtype."
    -- LRM 3.2(paragraph 8)

So, lets say, in a fever of unambiguous autodidactic ambition I desire
to manipulate
low level constructs with Boolean logic (so that I can test some general
principles
from the Art of Assembly with Ada to keep it all where I can understand
it)
I will need need some basic types to work with, do I write something
like:

   type byte is mod 2*8;
   subtype bit_mask is byte;

-- or something like:

   type byte is mod 2*8;
   type bit_mask is new byte;

-- or does it make more sense to say something like:

   type byte is mod 2*8;

   my_byte : byte;
   bit_mask : byte;

-- there just seem to be so many (& more) combinations, I don't
understand which is
most sensible/efficient.  I suspect it might depend on the context.  It
seems to me
that all of these (even displacing types into variables) allow similar
values to be manipulated.
Would you use something like    "type bit_mask is new byte;"  only when
you had a helluva lot
of bit_masks to work with ?  Would I better to make some sort of tagged
type byte and then
derive relevant masks from that ?  If that is possible.

------------------------------------------------------------
------------------------------------------------------------

2:

Modular integers and the interfaces.unsigned_32 types -
it appears as though (after some compiler experimentation)
that modular integers are always going to be signed and should
have to be the only type I can use for signed bytes, words, etc.

no ? yes ? maybe ?

(I am trying to use Ada to experiment & learn with concepts I get from
all over the place
in technical manuals, tutorials, etc.  I have found
interfaces.unsigned_xx to allow
values in the range 2*3 .. 2*6.  ) I use GNAT 3.11.
------------------------------------------------
------------------------------------------------
3.
In an implementation of MIL-STD-1553B I have read,
there is a declaration:

type Bit_Numbers is range 0 .. 15;

- I am wondering why anyone would want to work with
individual bits (that is what I interpret that as for) in this
way, because you can alter their values with logical bit_masks.
I was reading that the 80x86 works more efficiently with bytes
than with the smaller bits.  Would you want to work with individual
bits in some situations because some embedded/special-purpose
computers may have different architectures which make
it relevant to manipulate individual bits in this manner ?

-------
-------
- I am struggling to understand, sorry if this is irrelevant.

--
GM Wallace.

BDF
--






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

* Re: to type or subtype ? (novice question, three parts)
  2000-02-15  0:00 to type or subtype ? (novice question, three parts) G
@ 2000-02-15  0:00 ` Tucker Taft
  0 siblings, 0 replies; 2+ messages in thread
From: Tucker Taft @ 2000-02-15  0:00 UTC (permalink / raw)


G wrote:
> 
> Hi all, and thanks so much for all your help. :-)
> I hope this is all relevant to the newsgroup topic.
> There are not as many diverse sources about Ada as there are
> other languages, so it is sometimes difficult to get helpful
> information.
> I try to get it straight from the horse's mouth, as it were...
> 
> There are three questions in this message.
> One is about types and subtypes.
> Two is about signed modular integers.
> Three is about a very little bit of MIL-STD-1553B.
> 
> ------------------------------------------
> ------------------------------------------
> 1:
> Ok, something I don't understand - when should you
> make a new type, say :
> 
> type widget is new integer;
> 
>   or, say :
> 
> subtype widget is integer;
> 
> - when they are both integers.  That is -
> what difference does it make with what you
> can do with/to the type ?  

You should use distinct types when you don't want to mistakenly
combine things which are fundamentally different, even though they
share the same underlying representation.  In Ada, there is
no implicit conversion between user-declared types, so if you
have two variables that you want to assign back and forth, or combine
using some arithmetic or logic operator, then they should generally
be the same type.  If they represent fundamentally different things,
like age and weight, then you definitely want different types, even
though both can use "plain old integers" as the underlying representation.

In your example, you have "byte" and "bitmask."  They sound like different
things to me, presuming a byte is an 8-bit numeric value, whereas a bitmask
is essentially a vector of 8 booleans.  Of course, you could declare
it as a packed array of 8 booleans, though that might result in
pass-by-reference on some compilers, which would perhaps be undesirable.

Even "bitmask" sounds a bit generic.  Perhaps what you really want is
Blodget_Flags or Wombat_Attributes or some such thing.  It is hard to
know without knowing your application better.

> ...
> 
> ------------------------------------------------------------
> ------------------------------------------------------------
> 
> 2:
> 
> Modular integers and the interfaces.unsigned_32 types -
> it appears as though (after some compiler experimentation)
> that modular integers are always going to be signed and should
> have to be the only type I can use for signed bytes, words, etc.
> 
> no ? yes ? maybe ?

Now you have confused me.  Modular integers are *unsigned* in Ada.
That is, they have values in the range 0..modulus-1.
Signed integers are declared via "type Sgn is range -100..100;" and
include negative values in their "base" range (even if not in their
first subtype range).

> 
> (I am trying to use Ada to experiment & learn with concepts I get from
> all over the place
> in technical manuals, tutorials, etc.  I have found
> interfaces.unsigned_xx to allow
> values in the range 2*3 .. 2*6.  ) I use GNAT 3.11.
> ------------------------------------------------
> ------------------------------------------------
> 3.
> In an implementation of MIL-STD-1553B I have read,
> there is a declaration:
> 
> type Bit_Numbers is range 0 .. 15;
> 
> - I am wondering why anyone would want to work with
> individual bits (that is what I interpret that as for) in this
> way, because you can alter their values with logical bit_masks.
> I was reading that the 80x86 works more efficiently with bytes
> than with the smaller bits.  Would you want to work with individual
> bits in some situations because some embedded/special-purpose
> computers may have different architectures which make
> it relevant to manipulate individual bits in this manner ?

There are at least two reasons to specify a range.  One is to
implicity request a type of a particular size (e.g. 4 bits in this case).
The other is because the range represents the meaningful range of
values, and anything outside of that range would be an indication
of a bug.  Generally in Ada you would use the range to implicit
specify the type only when interfacing with some external device or
format.  In all other cases, you would want to specify a range that
corresponds to the logical/semantic properties of the type, and you
only care that the underlying physical representation be sufficiently
large to accommodate the full meaningful range.


> 
> -------
> -------
> - I am struggling to understand, sorry if this is irrelevant.

Not irrelevant at all.  Most programming languages are totally wedded
to the reprentation-oriented view of numeric types.  Ada is very rare
in that it encourages the programmer to think of numeric types at a higher
level, and gives the programmer the ability to attach application-specific
semantics to particular numeric types.  About the closest thing to this
in other languages is enumeration types, which we all know are represented
internally as integers, but for which the values have some non-numeric
significance.  One fairly good model is the notion of "units" from
physics, where a number rarely has any meaning on its own.  You need
to qualify it with the particular units (e.g. feet, meters, Hz, light-years,
etc.) to know what the number "means."  In Ada, the model is the same.
A numeric value has no real meaning until you know what "domain" it
comes from, or what units it has.

> --
> GM Wallace.
> 
> BDF
> --

-- 
-Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA




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

end of thread, other threads:[~2000-02-15  0:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-02-15  0:00 to type or subtype ? (novice question, three parts) G
2000-02-15  0:00 ` Tucker Taft

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