comp.lang.ada
 help / color / mirror / Atom feed
* Questions:
@ 2001-03-03  1:00 WM
  2001-03-03  3:30 ` Questions: Robert Love
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: WM @ 2001-03-03  1:00 UTC (permalink / raw)


Hi, I am an ada beginner. Can anyone kindly tell me what's the difference
between TYPE and SUBTYPE? And what's a PACKAGE?
Thank you very much :-)





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

* Re: Questions:
  2001-03-03  1:00 Questions: WM
@ 2001-03-03  3:30 ` Robert Love
  2001-03-03 11:09 ` Questions: David C. Hoos, Sr.
  2001-03-07 23:05 ` Questions: Mark Lundquist
  2 siblings, 0 replies; 13+ messages in thread
From: Robert Love @ 2001-03-03  3:30 UTC (permalink / raw)


>>>>> "WM" == WM  <wwminirl@hotmail.com> writes:

    WM> Hi, I am an ada beginner. Can anyone kindly tell me what's the
    WM> difference between TYPE and SUBTYPE? And what's a PACKAGE?
    WM> Thank you very much :-)

What does your text book say?

-- 
=============================================================
| Support Signature Minimalism                              |
=============================================================



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

* Re: Questions:
  2001-03-03  1:00 Questions: WM
  2001-03-03  3:30 ` Questions: Robert Love
@ 2001-03-03 11:09 ` David C. Hoos, Sr.
  2001-03-07 23:05 ` Questions: Mark Lundquist
  2 siblings, 0 replies; 13+ messages in thread
From: David C. Hoos, Sr. @ 2001-03-03 11:09 UTC (permalink / raw)


May I suggest you refer to the excellent Ada tutorial which can be
downloaded from
ftp://ftp.ada95.com/pub/lovelace.tgz or
ftp://ftp.ada95.com/pub/lovelace.tar.gz
and viewed with a Web browser.

Just make a new directory on your computer, download the file from the above
site, and extract the files into the new directory you've created.

You will find these two subjects mentioned in The master outline of all the
lessons

"WM" <wwminirl@hotmail.com> wrote in message
news:97pfmt$ll30@tech.port.ac.uk...
> Hi, I am an ada beginner. Can anyone kindly tell me what's the difference
> between TYPE and SUBTYPE? And what's a PACKAGE?
> Thank you very much :-)
>
>




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

* Re: Questions:
  2001-03-03  1:00 Questions: WM
  2001-03-03  3:30 ` Questions: Robert Love
  2001-03-03 11:09 ` Questions: David C. Hoos, Sr.
@ 2001-03-07 23:05 ` Mark Lundquist
  2001-03-08  1:14   ` Questions: Robert A Duff
  2001-03-12  7:41   ` Types, subtypes and ranges Anders Wirzenius
  2 siblings, 2 replies; 13+ messages in thread
From: Mark Lundquist @ 2001-03-07 23:05 UTC (permalink / raw)


Sorry for the late response (I lost access to news for a while...)

WM <wwminirl@hotmail.com> wrote in message
news:97pfmt$ll30@tech.port.ac.uk...
> Hi, I am an ada beginner. Can anyone kindly tell me what's the difference
> between TYPE and SUBTYPE?

This is one of the cool things about Ada, IMHO.  It's also very fundamental
and one of the keys to "thinking in Ada".  It's not well understood by those
who learn the language in a "paint-by-numbers" way, but it's really not that
complicated, and it's well worth understanding.

First of all, what it *doesn't* mean... "subtype" isn't a relationship, it's
a kind of entity (so you don't say, "type Foo is a subtype of type Bar").
Nor is a subtype a "kind of type".  Also, subtypes have nothing to do with
inheritance.  The mechanism for inheritance is type *derivation* (in OO
literature, the terms "subtype" and "derivation" are both used to denote
inheritance; Ada uses the term "derivation" to denote inheritance, and it
uses the term "subtype" to mean a very different thing...)

Type and subtype are distinctly different entities.  But every subtype is
associated with some type, which is called "the type of the subtype".

Every object has (is "of") a subtype! (and thus also a type).

But a *value* does not have a subtype!  It has only a type!

A type is a set of values, combined with a set of operations.  A subtype
combines a type with a *constraint*, which describes a subset of the type's
values that objects of the subtype are allowed to have.

Got that?  Type = values + operations.  Subtype = type + constraint.

A "null constraint" is a constraint that doesn't restrict the set of values.

Now then... every type declaration actually declares *two* things: a type
and a subtype!  This subtype is called the "first subtype" of the type.
Technically, types are anonymous in Ada; what is usually thought of as the
"type name" is really the name of the first subtype.

A subtype declaration declares only a subtype.  The type of the new subtype
is the same as the type of the subtype named in the declaration, e.g. given

    subtype S is T;

declares a new subtype S of the type of subtype T.  This is also an example
of a subtype declaration that does not include a constraint, so the
constraint of S is the same as the constraint of T.  (This is how you
"rename" or create an alias for a type).  What a subtype declaration looks
like that adds a constraint depends on the type of the subtypes, e.g. for an
integer type you might have

    subtype S is T range 1 .. 10;


Hope this helps,

Mark Lundquist
Rational Software






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

* Re: Questions:
  2001-03-07 23:05 ` Questions: Mark Lundquist
@ 2001-03-08  1:14   ` Robert A Duff
  2001-03-12  7:41   ` Types, subtypes and ranges Anders Wirzenius
  1 sibling, 0 replies; 13+ messages in thread
From: Robert A Duff @ 2001-03-08  1:14 UTC (permalink / raw)


"Mark Lundquist" <mark@rational.com> writes:

> But a *value* does not have a subtype!  It has only a type!

That's true for elementary types (eg integers), but composite *values*
have a subtype.  Eg, the string literal:

    "Hello, world."

usually has subtype String(1..13).

- Bob



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

* Types, subtypes and ranges
  2001-03-07 23:05 ` Questions: Mark Lundquist
  2001-03-08  1:14   ` Questions: Robert A Duff
@ 2001-03-12  7:41   ` Anders Wirzenius
  2001-03-12 11:57     ` David C. Hoos, Sr.
                       ` (2 more replies)
  1 sibling, 3 replies; 13+ messages in thread
From: Anders Wirzenius @ 2001-03-12  7:41 UTC (permalink / raw)



Mark Lundquist wrote in message ...
>Sorry for the late response (I lost access to news for a while...)
>
>WM <wwminirl@hotmail.com> wrote in message
>news:97pfmt$ll30@tech.port.ac.uk...
>> Hi, I am an ada beginner. Can anyone kindly tell me what's the difference
>> between TYPE and SUBTYPE?
>
...
>    subtype S is T;
>
>declares a new subtype S of the type of subtype T.  This is also an example
>of a subtype declaration that does not include a constraint, so the
>constraint of S is the same as the constraint of T.  (This is how you
>"rename" or create an alias for a type).  What a subtype declaration looks
>like that adds a constraint depends on the type of the subtypes, e.g. for
an
>integer type you might have

May I continue with two more questions:
1.
given
>
>    subtype S is T range 1 .. 10;

what is the the difference between the above declaration and
type S is new T range 1..10;

2.
Why don't I get a constraint_error in the following code where I violate the
range of type TTT in the Put statement?

with Ada.Text_IO;
procedure Why_Not_Constraint_Error is
   type T is new Integer;
   subtype TT is T range -2..-1;
   subtype TTT is T range -1..-1;
   anders : TT := -2;
   bnders : TTT := -1;
   use Ada.Text_IO;
begin
   Put ( TTT'Image ( anders+bnders ) );
end Why_Not_Constraint_Error;

Anders
another beginner






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

* Re: Types, subtypes and ranges
  2001-03-12  7:41   ` Types, subtypes and ranges Anders Wirzenius
@ 2001-03-12 11:57     ` David C. Hoos, Sr.
  2001-03-12 16:06       ` Tucker Taft
  2001-03-13  6:40       ` Anders Wirzenius
  2001-03-12 16:57     ` Scott Ingram
  2001-03-18 22:28     ` Lao Xiao Hai
  2 siblings, 2 replies; 13+ messages in thread
From: David C. Hoos, Sr. @ 2001-03-12 11:57 UTC (permalink / raw)


Because TTT'Image is inherited from its base type T.

If you had declared TT and TTT as new T types, the constraint
error would have occurred, but then you wouldn't have been
able to add anders to banders without type conversion, because
the types would have been distinct.

"Anders Wirzenius" <anders.wirzenius@pp.qnet.fi> wrote in message
news:V4%q6.26$OG2.2999@read2.inet.fi...
>
> Mark Lundquist wrote in message ...
> >Sorry for the late response (I lost access to news for a while...)
> >
> >WM <wwminirl@hotmail.com> wrote in message
> >news:97pfmt$ll30@tech.port.ac.uk...
> >> Hi, I am an ada beginner. Can anyone kindly tell me what's the
difference
> >> between TYPE and SUBTYPE?
> >
> ...
> >    subtype S is T;
> >
> >declares a new subtype S of the type of subtype T.  This is also an
example
> >of a subtype declaration that does not include a constraint, so the
> >constraint of S is the same as the constraint of T.  (This is how you
> >"rename" or create an alias for a type).  What a subtype declaration
looks
> >like that adds a constraint depends on the type of the subtypes, e.g. for
> an
> >integer type you might have
>
> May I continue with two more questions:
> 1.
> given
> >
> >    subtype S is T range 1 .. 10;
>
> what is the the difference between the above declaration and
> type S is new T range 1..10;
>
> 2.
> Why don't I get a constraint_error in the following code where I violate
the
> range of type TTT in the Put statement?
>
> with Ada.Text_IO;
> procedure Why_Not_Constraint_Error is
>    type T is new Integer;
>    subtype TT is T range -2..-1;
>    subtype TTT is T range -1..-1;
>    anders : TT := -2;
>    bnders : TTT := -1;
>    use Ada.Text_IO;
> begin
>    Put ( TTT'Image ( anders+bnders ) );
> end Why_Not_Constraint_Error;
>
> Anders
> another beginner
>
>
>





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

* Re: Types, subtypes and ranges
  2001-03-12 11:57     ` David C. Hoos, Sr.
@ 2001-03-12 16:06       ` Tucker Taft
  2001-03-13  6:40       ` Anders Wirzenius
  1 sibling, 0 replies; 13+ messages in thread
From: Tucker Taft @ 2001-03-12 16:06 UTC (permalink / raw)


"David C. Hoos, Sr." wrote:
> 
> Because TTT'Image is inherited from its base type T.
> 
> If you had declared TT and TTT as new T types, the constraint
> error would have occurred, but then you wouldn't have been
> able to add anders to banders without type conversion, because
> the types would have been distinct.

This isn't quite right.  The 'Image and 'Value attributes deal
with all values in the "base range" of the type, even if the
prefix (e.g., the "S" in S'Image) is a subtype with a more constrained
range.  The "base range" of a scalar type is chosen by the implementation
so that it is, if signed, symmetric around zero (+/- 1 value in negative
direction), and generally corresponds to a range that is directly
supported by the hardware.  And of course, the base range must cover
all the values specified in the original type definition.

So don't expect 'Image or 'Value to do any useful constraint checking.
Of course, when you assign the result of 'Value you will get constraint
checking.  The same goes for the 'Pos and 'Val attributes, FWIW.

To answer your first question, declaring a subtype vs. declaring
a new type -- all subtypes of the same type are freely interconvertible,
whereas converting between distinct types requires an explicit
conversion.  Furthermore, all subtypes of the same type share the
same set of operators/operations, and you can mix and match between
subtypes for operands.  In fact, "values" or "expressions" of an 
elementary type don't really have a subtype, they only have a "type."
Subtypes are essentially subsets of types, whereas derived types
represent a completely new universe of values, that happens to have the 
same number of values as the parent "universe," but the values are
distinguishable because they have a different type.

If you remember "Venn" diagrams from math class, subtypes are potentially
overlapping circles on the Venn diagram representing a "universe" of
the values of a type.  On the other hand, a derived type gets its
own separate Venn diagram, with its own possibly overlapping circles
representing different subtypes.


> 
> "Anders Wirzenius" <anders.wirzenius@pp.qnet.fi> wrote in message
> news:V4%q6.26$OG2.2999@read2.inet.fi...
> >
> > Mark Lundquist wrote in message ...
> > >Sorry for the late response (I lost access to news for a while...)
> > >
> > >WM <wwminirl@hotmail.com> wrote in message
> > >news:97pfmt$ll30@tech.port.ac.uk...
> > >> Hi, I am an ada beginner. Can anyone kindly tell me what's the
> difference
> > >> between TYPE and SUBTYPE?
> > >
> > ...
> > >    subtype S is T;
> > >
> > >declares a new subtype S of the type of subtype T.  This is also an
> example
> > >of a subtype declaration that does not include a constraint, so the
> > >constraint of S is the same as the constraint of T.  (This is how you
> > >"rename" or create an alias for a type).  What a subtype declaration
> looks
> > >like that adds a constraint depends on the type of the subtypes, e.g. for
> > an
> > >integer type you might have
> >
> > May I continue with two more questions:
> > 1.
> > given
> > >
> > >    subtype S is T range 1 .. 10;
> >
> > what is the the difference between the above declaration and
> > type S is new T range 1..10;
> >
> > 2.
> > Why don't I get a constraint_error in the following code where I violate
> the
> > range of type TTT in the Put statement?
> >
> > with Ada.Text_IO;
> > procedure Why_Not_Constraint_Error is
> >    type T is new Integer;
> >    subtype TT is T range -2..-1;
> >    subtype TTT is T range -1..-1;
> >    anders : TT := -2;
> >    bnders : TTT := -1;
> >    use Ada.Text_IO;
> > begin
> >    Put ( TTT'Image ( anders+bnders ) );
> > end Why_Not_Constraint_Error;
> >
> > Anders
> > another beginner
> >
> >
> >

-- 
-Tucker Taft   stt@avercom.net   http://www.averstar.com/~stt/
Chief Technology Officer, AverCom Corporation (A Titan Company) 
Burlington, MA  USA (AverCom was formerly the Commercial Division of AverStar:
http://www.averstar.com/services/ebusiness_applications.html)



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

* Re: Types, subtypes and ranges
  2001-03-12  7:41   ` Types, subtypes and ranges Anders Wirzenius
  2001-03-12 11:57     ` David C. Hoos, Sr.
@ 2001-03-12 16:57     ` Scott Ingram
  2001-03-18 22:28     ` Lao Xiao Hai
  2 siblings, 0 replies; 13+ messages in thread
From: Scott Ingram @ 2001-03-12 16:57 UTC (permalink / raw)


Anders Wirzenius wrote:

> 
> May I continue with two more questions:
> 1.
> given
> >
> >    subtype S is T range 1 .. 10;
> 
> what is the the difference between the above declaration and
> type S is new T range 1..10;

> Anders
> another beginner

subtype S belongs to type T, whereas type S is a new type
distict from type T.

and David Hoos answers your other question in a different post.

-- 
Scott Ingram
Vice-Chair, Baltimore SIGAda
Sonar Processing and Analysis Laboratory
Johns Hopkins University Applied Physics Laboratory



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

* Re: Types, subtypes and ranges
  2001-03-12 11:57     ` David C. Hoos, Sr.
  2001-03-12 16:06       ` Tucker Taft
@ 2001-03-13  6:40       ` Anders Wirzenius
  1 sibling, 0 replies; 13+ messages in thread
From: Anders Wirzenius @ 2001-03-13  6:40 UTC (permalink / raw)


David C. Hoos, Sr. wrote in message <98idi9$66g$1@hobbes2.crc.com>...
>Because TTT'Image is inherited from its base type T.
>
>If you had declared TT and TTT as new T types, the constraint
>error would have occurred, but then you wouldn't have been
>able to add anders to banders without type conversion, because
>the types would have been distinct.

Thanks

I deleted my former code but include my further studies on the subject. It
showed me that the 'Image attribute is inherited all the way up as long as
you don't break the chain of subtypes:

with Ada.Text_IO;
procedure Why_Not_Constraint_Error is
   type T is new Integer;
   subtype TT is T range -2..-1;
   subtype TTT is T range -1..+1;
   subtype TTTT is TTT range -1..0;
   subtype TTTTT is TTTT range 0..0;
   anders : TT := -2;
   bnders : TTTT := 0;
   use Ada.Text_IO;
begin
   Put ( TTTTT'Image ( anders+bnders ) );
end Why_Not_Constraint_Error;

Anders





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

* Re: Types, subtypes and ranges
  2001-03-12  7:41   ` Types, subtypes and ranges Anders Wirzenius
  2001-03-12 11:57     ` David C. Hoos, Sr.
  2001-03-12 16:57     ` Scott Ingram
@ 2001-03-18 22:28     ` Lao Xiao Hai
  2001-03-19 13:22       ` Marc A. Criley
  2 siblings, 1 reply; 13+ messages in thread
From: Lao Xiao Hai @ 2001-03-18 22:28 UTC (permalink / raw)




>
> >WM <wwminirl@hotmail.com> wrote in message
> >news:97pfmt$ll30@tech.port.ac.uk...
> >> Hi, I am an ada beginner. Can anyone kindly tell me what's the difference
> >> between TYPE and SUBTYPE?

This is a confusing issue for a newcomer to Ada.   One can give a strict
language-lawyer answer that a computer science purist will appreciate,
or one can give an answer that is useful to a working programmer.  So,
even though my answer may offend some who like to take the ALRM
seriously, I sometimes prefer an answer that can be used by someone
who just wants to grind out practical Ada code rather than argue about
how many subtypes can dance on the head of a pin.  Consequently, for
good or evil, here is another approach to answering this question.

A type has a:

          type name,
          set of values,
          set of associated operations,
          "wall" between objects of its type and objects of some other type

Under some circumstances, the "wall" may be breached through a type conversion.
The
rules for type conversion are appropriately strict.   One can also breach the
wall through
unsafe mechanisms such as type conversion, though these are often not
recommended.

A subtype

         has a subtype name,
         inherits the set of values of its parent type (or parent subtype)
         inherits the set of operations of its parent type (or parent subtype)
         has no wall between itself and its parent type or any subtype of its
parent.

Operations between subtypes with the same parent (ancestor) are legal without
the
need for type conversion.   Operations between a subtype and its ancestor are
also
legal without type conversion.  This means that subtypes need to be used with
care
because the compiler is not required to check for correct ranges.   It weakens
the
type system slightly, so many regard it as unsafe.

In addition, a subtype may have additional operations defined for it; and, in
some cases,
it may have a smaller set (range) of values.

The key difference between a type and subtype, for the practical programmer, is
the
absence of a wall between the subtype and its parent and sibling/cousin
subtypes.

I am fully aware of the more rigorous definition in the ALRM, but that
definition is
useful mainly for someone who is writing a compiler.   For an application
programmer,
the issue is the "wall" and what to do about it when designing with types and
subtypes.

Richard Riehle





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

* Re: Types, subtypes and ranges
  2001-03-18 22:28     ` Lao Xiao Hai
@ 2001-03-19 13:22       ` Marc A. Criley
  2001-03-20 16:57         ` Lao Xiao Hai
  0 siblings, 1 reply; 13+ messages in thread
From: Marc A. Criley @ 2001-03-19 13:22 UTC (permalink / raw)


Lao Xiao Hai wrote:
> 
> >
> > >WM <wwminirl@hotmail.com> wrote in message
> > >news:97pfmt$ll30@tech.port.ac.uk...
> > >> Hi, I am an ada beginner. Can anyone kindly tell me what's the difference
> > >> between TYPE and SUBTYPE?
> 

  <Nice, informal description of type/subtypes snipped>
> 
> A subtype [...]

> weakens the type system slightly, so many regard it as unsafe.
> 
> In addition, a subtype may have additional operations defined for it; and, in
> some cases, it may have a smaller set (range) of values.
> 
> The key difference between a type and subtype, for the practical programmer, is
> the absence of a wall between the subtype and its parent and sibling/cousin
> subtypes.

Hmmm, I've never really considered subtypes a "weakening" of the type
slightly, though obviously they could be ill-used to that end.  My use
of them has been in fact to tighten up the typing.

One useful use of subtypes is with indexing.  For example:

   type Item_Count is range 0 .. Max_Items;
   subtype Indexes is Item_Count range 1 .. Max_Items;

   type Item_List is array(Indexes) of Item_Type;
   
   Items : Item_List;
   Number_Of_Items : Item_Count := 0;

So now I can increment Number_Of_Items and use that as the index into
the Item_List array while ensuring that only valid indexes will be
available.

Another useful area where subtypes can increase type safety is when
you're working with discriminated records.

   type Operation is (Load, Store, Clear);

   type Command(Op : Operation) is record ... end record;

Now if you want to require that only data of a specific kind is allowed
you can subtype the record:

   subtype Store_Command is Command(Store);

In this case any variables of type Store_Command must conform to that
value of the discriminant.

An actual instance of this latter approach is used extensively in a
particular US Navy weapon control system.  Messages that came in over a
socket had their header read to determine what type of message was being
transmitted.  Then within a declare/begin/end block the Message variant
record type was subtyped with that type of message, and then the message
was read off the socket directly into the message.  Works slick, shoots
missiles! :-)

Marc A. Criley
Senior Staff Engineer
Quadrus Corporation
www.quadruscorp.com



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

* Re: Types, subtypes and ranges
  2001-03-19 13:22       ` Marc A. Criley
@ 2001-03-20 16:57         ` Lao Xiao Hai
  0 siblings, 0 replies; 13+ messages in thread
From: Lao Xiao Hai @ 2001-03-20 16:57 UTC (permalink / raw)




"Marc A. Criley" wrote:

>
> Hmmm, I've never really considered subtypes a "weakening" of the type
> slightly, though obviously they could be ill-used to that end.  My use
> of them has been in fact to tighten up the typing.

Often, some programmer will declare a lot of subtypes to avoid
some of the strict compile-time type checking on operations.  Also,
without the need for explicit type conversions, one can blithely
assign one type to another without considering the implications,
such as potential range constraints at run-time.

Granted, one must confront the same issue with derived types that
have varying ranges, but we could hope the need for explicit type
conversion makes the programmer stop and consider the possible
consequences of that action.

That being said, the examples you have presented are excellent for
demonstrating the intelligent use of subtypes.

Richard Riehle






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

end of thread, other threads:[~2001-03-20 16:57 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-03  1:00 Questions: WM
2001-03-03  3:30 ` Questions: Robert Love
2001-03-03 11:09 ` Questions: David C. Hoos, Sr.
2001-03-07 23:05 ` Questions: Mark Lundquist
2001-03-08  1:14   ` Questions: Robert A Duff
2001-03-12  7:41   ` Types, subtypes and ranges Anders Wirzenius
2001-03-12 11:57     ` David C. Hoos, Sr.
2001-03-12 16:06       ` Tucker Taft
2001-03-13  6:40       ` Anders Wirzenius
2001-03-12 16:57     ` Scott Ingram
2001-03-18 22:28     ` Lao Xiao Hai
2001-03-19 13:22       ` Marc A. Criley
2001-03-20 16:57         ` Lao Xiao Hai

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