Ada95- A brief History.ppt

上传人:Iclinic170 文档编号:378026 上传时间:2018-10-09 格式:PPT 页数:63 大小:133.50KB
下载 相关 举报
Ada95- A brief History.ppt_第1页
第1页 / 共63页
Ada95- A brief History.ppt_第2页
第2页 / 共63页
Ada95- A brief History.ppt_第3页
第3页 / 共63页
Ada95- A brief History.ppt_第4页
第4页 / 共63页
Ada95- A brief History.ppt_第5页
第5页 / 共63页
亲,该文档总共63页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、ada-typesI- 1,Ada95: A brief History,1975-1979: strawman-steelman requirements, Pascal is base 1981 : Green wins, becomes preliminary Ada 1983 : ANSI Standard, first validated compiler 1987 : ISO Standard 1990-1994 : Revision Process 1995 : ANSI - ISO Ada 95 Standard 1995 : First validated GNAT comp

2、iler,ada-typesI- 2,Design principles,Software Engineering: programming in the large Readability over writability Rigorous standard for portability Precise semantics Built-in concurrency Strong-Typing: all unsafe code is explicit,ada-typesI- 3,Ada, C+ and Java,Big languages winstrong typing : Ada, C+

3、, JavaGenerics : Ada C+Concurrency : Ada JavaPackages: Ada C+ and JavaAhead of its time in designOut of step in implementationBetween language generations,ada-typesI- 4,Key Goals,Readability : between COBOL and APLstrong typing : compile, dont debugProgramming in the large: package itException Handl

4、ing: Murphy is always rightData abstraction : apples are not orangesObject orientation : Dispatch and inheritTasking : walk, chew gum, etc.Generic units : the easiest reuse is to copyInterfacing : play well with others,ada-typesI- 5,The canonical example,with Text_Io; use Text_Io; procedure example

5、is beginPut_Line (“easier than we thought!”); end;,ada-typesI- 6,A small package,Package Math_Functions isfunction Sqrt (X : Float) return Float;function Exp (Base : Float; Exponent : Float) return Float; end Math_Functions;,ada-typesI- 7,Using the package,with Math_Functions; with Gnat_IO; use Gnat

6、_IO; procedure Example2 isVal : Float; beginget (Val);put (“Sqrt (“); put (Val); put (“) = “);put (Math_Functions.Sqrt (Val);new_line; end;,ada-typesI- 8,Implementing the package,Package body Math_Functions isEpsilon : constant := 1.0e-7;function Sqrt (X : Float) return Float isResult : Float := X /

7、 2.0;beginwhile abs (Result * Result - X ) Epsilon loopResult := 0.5 * (X / Result + Result);end loop;return Result;end Sqrt;. end Math_Functions;,ada-typesI- 9,Abstraction at its best: enumeration types,Trivial Implementation: literals are mapped into successive integersVery common abstraction: lis

8、t of names, propertiesExpressive of real-world domain, hides machine representation type suit is (hearts, diamonds, spades, clubs) ; type direction is (east, west, north, south, lost);Order of list implies that spades hearts, etc.,ada-typesI- 10,Enumeration types and strong typing,type Fruit is (App

9、le, Orange, Grape, Apricot);type Vendor is (Apple, IBM, Compaq);My_PC : Vendor;Dessert : Fruit;My_PC := Apple;Dessert := Apple;Dessert := My_PC; - ERROR,ada-typesI- 11,Built-in enumeration types,type Boolean is (False, True);type Character is (. - full ASCII.- not expressible in Adatype Wide_Charact

10、er is ( - Unicode, or ISO646,ada-typesI- 12,Array Types,Index is typed:type weekday is (Mon, Tue, Wed, Thu, Fri);type workhours is array (Weekday) of integer;Predefined array:type String is array (Positive range ) of Character;,ada-typesI- 13,Record Types,conventional named fields:type Buffer is rec

11、ordsize : Positive;contents : String (1 100);end record;B1 : Buffer; - can use B1, B1.size, B1.contents (10).,ada-typesI- 14,Access Types,Typed pointers, for type safety and to minimize aliasing:type Handle is access Buffer;Ptr : Handle := new Buffer;Ptr.all is a Buffer.Can write Ptr.size, Ptr.conte

12、nts, etc.,ada-typesI- 15,Abstraction mechanisms,Packages Private types Objects and Inheritance Classes, polymorphism, dynamic dispatching Generic units Concurrency : tasks and protected types,ada-typesI- 16,Packages,A related set of types, constants, and subprogramsSeparate declaration (interface) a

13、nd implementationSupports privacy and data hidingThe single most important idea in software engineering,ada-typesI- 17,A package for stacks,package Stacks istype Stack is private;procedure Push (It : Character; On : in out Stack);procedure Pop (It : Character; From : in out Stack);function Empty (S

14、: Stack) return Boolean; privatetype Stack is recordtop : Integer := 0;contents : String (1 80) := (others = *);end record;end Stacks;,ada-typesI- 18,Object-oriented Programming,Type extension Inheritance and overriding Run-time dispatching Polymorphism Class-wide programming Abstract types and subp

15、rograms,ada-typesI- 19,Type Extension,Mechanism to define new types by enrichment:type Point is tagged recordX_Coord, Y_Coord : Integer;end record;type Pixel is new Point with recordR, G, B : Integer;end record;,ada-typesI- 20,Inheritance,A type has primitive operations: operations that involve the

16、type as a parameter or a returned value. A type extension inherits the primitive operations of its parent. A primitive operation can be redefined and overridden for a descendant type.,ada-typesI- 21,Polymorphism,A class is a family of types with the same ancestor. An object of the class is identifie

17、d at run-time by its tag. Dynamic dispatching uses the tag of the object to determine the operation that applies. A classwide operation applies uniformly to all member of the class:procedure Examine (Thing : in out TClass);,ada-typesI- 22,Generic Units,The basic tool for software reuse. Parameters c

18、an be types, objects, subprograms, packages. Akin to C+ templates. Absence from Java is incomprehensible,ada-typesI- 23,A Generic Package,Generictype T is private; package Stacks istype Stack is private;procedure Push (Thing : T ; On : in out Stack); privatetype Arr is array (1 100) of T;type stack

19、is recordTop : Integer := 100;contents : Arr;end record; end Stacks;,ada-typesI- 24,A Generic Subprogram,Generictype T is private;type Arr is array (Integer range ) of T;with function “ (X, Y : T) return Boolean; procedure Sort (Table : in out Arr);,ada-typesI- 25,The Type Model,Types and Subtypes D

20、eclarations and their scope Objects, constants and variables Scalar types Array types Record types Access types,ada-typesI- 26,Types and Subtypes,A type is a set of values and a group of operations A type declaration specifies static properties of objects A subtype declaration specifies dynamic prop

21、erties of values. Types with different names are distinct and incompatible: name equivalence everywhere, instead of structural equivalence. Subtypes of the same base type are compatible.,ada-typesI- 27,Compile-time vs. run-time,Type properties are enforced by the compiler:x : integer := false; - pro

22、gram rejectedx : positive := f (z); - if f returns an integer, need to check value at run-time,ada-typesI- 28,Built-in subtypes,type Integer is - implementation defined subtype Positive is integer range 1 IntegerLast; - useful attribute subtype Natural is integer range 0 IntegerLast; X : integer :=

23、500; Y : Positive := 2 * X; Z : Natural := - Y; - legal, raises constraint error,ada-typesI- 29,Declarations and Scope,Declarations are elaborated in order Entities become visible at the end of their declaration (usually) Block structure allows arbitrary nesting of declarative regions. Declarations

24、can appear in subprograms packages blocks .,ada-typesI- 30,Blocks,DeclareX : Integer := F (5);Y : Integer := 2 * X;Z : Integer := Y * Z; - Error: prematureX : Float ; - Error: duplicate begindeclareX : Float := Float (Y); - hides outer X beginPut_Line (FloatImage (X);end;end;,ada-typesI- 31,Variable

25、s and Constants,Variable declaration:Limit : Integer := 25;Offset : Integer range 1 20;Constant declaration:Sqrt2 : constant float := Sqrt (2.0); - not static Always : constant Boolean := True; - static valueNever : constant Boolean := not Always; - static expression,ada-typesI- 32,Variables must be

26、 constrained,Subtype is constrained:First_Name : String (15) := “Ralph”;but not necessarily static:Last_Name : String (1 X * 2);else subtype is indefinite but initial value provides bounds:Comment : String := “this is obvious”; - bounds are 1 15,ada-typesI- 33,Multiple Declarations,This, That : T :=

27、 F (1, 2, 3); Is equivalent toThis : T := F (1, 2, 3);That : T := F (1, 2, 3);F is called twice. Important if expression has side-effect:type Ptr is access R;P1, P2 : Ptr := new R;two Rs are allocated.,ada-typesI- 34,Number Declarations,Pi : constant := 3.14159265; - type deduced from valueHalf_Pi :

28、 constant := Pi / 2; - mixed arithmetic OKBig : constant := 2 * 200; - legalOne : constant := 2 * Big / (Big + Big); - must be exact,ada-typesI- 35,Scalar Types,Discrete types Integer types Enumeration Types Real types Floating-point types Fixed_point types,ada-typesI- 36,Integer Types,Several prede

29、fined types: Integer, Long_Integer, Short_IntegerSpecific bounds of type must be static:type My_Int is range -2 * 16 2 * 16 - 1;type Tiny is range 0 10; By giving explicit bounds, program is more portable: each compiler will figure out what hardware type correspondsModular types:type Byte is mod 2 *

30、 8;,ada-typesI- 37,Integer Operations,Comparison Operators: = /= = Addition Operators: + - Unary operators + - Multiplying operators * / mod rem Highest precedence Operators: * abs,ada-typesI- 38,Boolean Operations,All attributes of discrete typesBoolean binary operators : and or xorBoolean unary op

31、erators : notShort-circuit operations : and then or else Membership operations : in not inWhen in doubt, parenthesize!,ada-typesI- 39,Attributes,Attributes denote properties of a type, or type-specific properties of a value BooleanSize - 1, because single bit charactersize - 8 bits monthpos (Jul) -

32、involves type and literal tablelength (1) - specify array and dimension Could be written as a function, but functions dont take types as arguments = need new syntax.,ada-typesI- 40,Attributes on Discrete Types,ByteFirst, Long_IntegerLast - applies to type or subtype WeekdaySucc (Today) - like functi

33、on call IntegerSucc (X*Y) - like adding one BooleanPred (True) - Yields False BooleanSucc (True) - Exception WeekdayPos (Mon) - Yields 0 WeekdayVal (3) - Yields Thu PositiveMax (X, Y) - function with two args,ada-typesI- 41,Real Types,All computations are approximate: Fixed point type: absolute boun

34、d on error: type temp is delta 2 * (-16) range -100.0 100.0; Floating point type: relative bound on error:type Angle is digits 7 range -2.0 2.0; Predefined floating point types: Float, Long_Float, etc.,ada-typesI- 42,Derived Types,A general mechanism for creating new types with the properties of exi

35、sting ones: type Like_T is new T; - same set of values, same operations.type Small_Int is range 1 10; equivalent totype Anon is new Integer;subtype Small_Int is Anon range 1 10; and all arithmetic operations are inherited,ada-typesI- 43,Array Types,Index types can be of any discrete type Component t

36、ype must be definite:type class_list is array ( 1 100) of String (110); - OKtype class_list is array ( 1 100) of String; - Error Subtype constrains all indices or none:type Matrix is array(positive range , positive range ) of Long_Float;subtype Table is Matrix;subtype Rotation is Matrix (1 3, 1 3);,

37、ada-typesI- 44,Anonymous Array Types,Grades : array (1 Num_Students) of Natural;type of Grades has no name: distinct from any other array types.Ar1, Ar2 : array (1 10) of Boolean;Ar1 := Ar2; - Error: different (anonymous) types. If type is useful, it deserves to have a name.,ada-typesI- 45,Array Att

38、ributes,type Matrix is array (Positive range , Positive range )of Float;subtype Rect is Matrix (1 3, 1 5);M3 : Rect;M3First (1) - Yields 1M3First - same.Rectlength (2) - Yields 5 (applies to type)M3range (2) - equivalent to 15StringLength - ERROR: unconstrained,ada-typesI- 46,Array Aggregates,Expres

39、sion that yields an array value:A := (1, 2, 3, 10); - positionalA := (1, others = 0); - notation for default.A := (13 = 1, 4 = -999); - component associationsDefault can only be used if bounds are known:A : String (1 10) := (others = ?); - OKA : String := (others = ?); - Error : unknown bounds.,ada-

40、typesI- 47,Aggregates and Qualification,Aggregate may be ambiguous:type Vector is array (1 3) of Float;procedure Display (V : vector);type Assay is array (1 3) of Float;procedure Display (A : assay);Display (1.0, 1.2, 1.5); - ambiguousDisplay (Vector (1.0, 1.2, 1.5); - OK.,ada-typesI- 48,Multidimens

41、ional Arrays,Aggregates given in row-major order with subaggregates:type Square is array (1 3, 1 3) of Integer;Unit : constant Square := ( (1, 0 ,0), (0, 1, 0), (0, 0, 1);Two-dimensional array is NOT array of arrays:type vector is array (1 3) of Integer;type V3 is array (1 3) of vector; - not conver

42、tible to Square,ada-typesI- 49,Operations on One_Dimensional Arrays,Boolean operations extend pointwise:type Set is array (1 Card) of Boolean;S1, S2,S3 : Set;S3 := S1 and S2; - Intersection lexicographic comparisons on arrays of discrete types:S1 := (T, T, T);S2 := (T, T, F);S2 S1 - yields True,ada-

43、typesI- 50,Concatenation and Slicing,Both operations yield the base type:type Table is array (110) of Integer;T1, T2 : Table;T1 T1 & T2 , T1 (X Y) are of type Anon,ada-typesI- 51,Specifying a range,subtype Sub is Positive range 2 4; Label : String (110) := “transcends” ; Label (2 4) - Yields “ran”La

44、bel (Integer range 2 4) - SameLabel (Sub) - DittoAlso used in loops and case statements.,ada-typesI- 52,Control Structures,Conventional sequential constructs: If-Statement Loop Case statement Goto and labelsMore novel forms for task communication.,ada-typesI- 53,If-Statement,If Done (X, Y) thenSucce

45、ss;Close_Up; elsif Almost_Done (X) then - the only keyword that isnt EnglishHurry (Y); elseif X = 0 then Call_For_Help (X) else Panic; end if; end if;,ada-typesI- 54,Loops,Infinite loop:loopPut_Line (“Forever”);end loop;In general, better to stop:loopPut_Line (“Still_Going”);exit when Time_Is_Up; -

46、must be boolean valueend loop;,ada-typesI- 55,Loops over discrete ranges,for J in 1 1000 loop - declares Jfor K in 1 0 loop - empty rangefor Month in Feb Nov loopfor Month in Months range Feb Nov loopfor K in Positive loop - might take a long timefor Num in reverse 1 1000 loop - descending order,ada

47、-typesI- 56,While-Loops,while abs (Result * Result - X ) Epsilon loopResult := 0.5 * (X / Result + Result);end loop;Effect of Until can be obtained with while and exit statements.,ada-typesI- 57,Named Loops,search : while X 0 loopX := F(X, Y);refine: for J in 1 N loopY := G (Z);exit search when X =

48、0.0;end loop refine;if T 20 then exit; end if; - alternate formend loop search;.,ada-typesI- 58,Case Statements,Most programs are interpreters for some abstract machine = case statement is most useful control structure!Works naturally with discrete types, in particular enumerations.Case alternatives must cover all values of the range,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 教学课件 > 大学教育

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1