From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 16 Feb 93 16:17:13 GMT 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... Message-ID: <1993Feb16.171713@lglsun.epfl.ch> List-Id: 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 |