comp.lang.ada
 help / color / mirror / Atom feed
From: enterpoop.mit.edu!ira.uka.de!scsing.switch.ch!sicsun!disuns2!lglsun!kipfe r@ucbvax.Berkeley.EDU  (Philippe Kipfer)
Subject: Re: What's the difference between...
Date: 16 Feb 93 16:17:13 GMT	[thread overview]
Message-ID: <1993Feb16.171713@lglsun.epfl.ch> (raw)

In article <9302121052.aa23420@Paris.ics.uci.edu>, you write:
: these two type declarations:
: 
: type Placement is range 0 .. 10;
: 
: subtype Placement is INTEGER range 0 .. 10;
: 
: The reason I ask this, is that I have the duty here at U.C. Irvine of 
: teaching the students of an Intro to Software Engineering course 
: enough Ada for them to do their homework assignments.  I get two 
: 1.5 hour lectures to do this.
: 
: One of my students asked what is the difference between the two type
: declarations.

You didn't write two type declarations; you wrote one type declaration
and one subtype declaration.

Look first at the definition of a type declaration (RM 3.3)

Declaring a numerical type has the effect to define a new type AND to
implicitely define the operations on it (RM 3.5.4, 3.5.5, 4.5.3 to
4.5.5).  This type is either the "Base type" and his own subtype.

When you define a new type, you are free to add a representation
clause (RM 13.2) such as

  type Placement is range 0 .. 10;
  for Placement use 4;

The compiler must store objects on 4 bits if it accepts the
representation but the compiler is free to generate some code to move
the 4 bits objects into 8 or 32 bits registers. You can see that the
operations related to objects of type Placement can't be strictly the
same as those for predefined type Integer. This is the effect of type
definition.

Now look at the definition of a subtype declaration (RM 3.3.2)

The declaration of a subtype is in fact the declaration of new bounds
applied to a base type in order to restrict the available set of
values. The bounds can be those defined for the base type or more
restrictive.

  subtype My_1_Placement is Placement range 0 .. 10; -- Renaming
  subtype My_2_Placement is Placement range 0 ..  5; -- restriction
  
The subtype actually serves as an invariant, objects are always of base
type, the storage size and operations are always those from base type.
The key here is to know that some "operations" act on the base type,
while others act on the subtype. Operations like "+", "-" act on the
base type, some attributes like 'First, 'Last act accordingly to the
subtype bounds.  Look carefully the expression "base type" and
"subtype" in the section RM-A, in particular for 'SUCC, 'FIRST and
'BASE'FIRST.


There is one more point. Imagine you have an expression like
  Dest := A + B + C - D - E;
Each individual value of A, B, C, D, E can be inside bounds. Some
subexpressions can be out of bounds (e.g. A + B) but the result of
expression can be inside of bounds. Compiler may or may not return a
result, depending on the implementation. If it stores objects on 4 bits but
use primitive operations of processor on 8 bits, it can handle
some subexpressions out of bounds.

This is because of the main principle of assignment:

  " Assignment consist of: evaluating the right part, verifying if 
    the result is compatible with the left part; if they are compatible
    make the assignment, if not raise an exception".

Hope this helps.

			Phil.

-- 
| Swiss Federal Institute of Technology |  
| Software Engineering Laboratory       |  Clever thougth is currently
| Philippe Kipfer (PKR)                 |         proceeding ....
| E-Mail: kipfer@lglsun.epfl.ch         |

             reply	other threads:[~1993-02-16 16:17 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-02-16 16:17 enterpoop.mit.edu!ira.uka.de!scsing.switch.ch!sicsun!disuns2!lglsun!kipfe [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-02-16 17:34 What's the difference between John Bollenbacher
1993-02-16 15:03 Robert I. Eachus
1993-02-15 13:24 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!darwin.sura.net!haven.umd.ed
1993-02-15  1:55 cis.ohio-state.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!portal!cup.portal
1993-02-14 19:55 Adam Beneschan
1993-02-14  3:08 agate!spool.mu.edu!sdd.hp.com!portal!cup.portal.com!R_Tim_Coslet
1993-02-13 19:18 agate!spool.mu.edu!hri.com!noc.near.net!inmet!spock!stt
1993-02-13  5:30 Alex Blakemore
1993-02-12 23:57 Mark A Biggar
1993-02-12 23:51 kronos.arc.nasa.gov!butch!LMSC5.IS.LMSC.LOCKHEED.COM!LJ10891
1993-02-12 22:58 Robert I. Eachus
1993-02-12 18:52 Kenneth Anderson
replies disabled

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