© ACT Europe under the GNU Free Documentation License 1 Franco Gasperoni
© ACT Europe under the GNU Free Documentation License 2 Copyright Notice © ACT Europe under the GNU Free Documentation License Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; provided its original author is mentioned and the link to is kept at the bottom of every non-title slide. A copy of the license is available at:
© ACT Europe under the GNU Free Documentation License 3
© ACT Europe under the GNU Free Documentation License 4 Programming in Ada 95 by John Barnes (Addison Wesley) Lovelace On line Ada 95 tutorial
© ACT Europe under the GNU Free Documentation License 5 Course Objectives Building reliable software systems Comparing structured & OO software design Programming distributed systems Practice, practice, practice
© ACT Europe under the GNU Free Documentation License 6 Lectures Introduction to Ada 95 Building reliable SW systems Structured design TP 1 Introduction to Ada 95 Building reliable SW systems Structured design TP 1 OO design Java TP 2 OO design Java TP 2 Distributed programming TP 3 Distributed programming TP h h 3+3 h
© ACT Europe under the GNU Free Documentation License 7 Course Evaluation TP OO: 6 points TP distributed programming: 4 points
© ACT Europe under the GNU Free Documentation License 8 Background on Ada 95 Programming with Ada 95
© ACT Europe under the GNU Free Documentation License Fortran(54) PL/I(66) Ada(83) Ada(95) Java(96) Basic(66) C(72) Pascal(70) Cobol(58) Algol(60) Simula(67) Smalltalk(80) C++(89) imperative A S S E M B L Y
© ACT Europe under the GNU Free Documentation License 10 Software Crisis: 1976 Many languages (>450) –Many dialects Low interoperability High maintenance costs Low reliability
© ACT Europe under the GNU Free Documentation License 11 The 70s - Mid 80s Biggest SW contractor SW with long life cycles (10-30 years) Safety critical apps
© ACT Europe under the GNU Free Documentation License 12 Safety Critical Drinking water contaminated for 1 h/month 2 accidents/month at O’Hare International Airport 22,000 checks drawn from the wrong account / hour Is 99.9% acceptable for defects?
© ACT Europe under the GNU Free Documentation License 13 Ada 1975 study: No language was adequate for safety critical applications : International competition -> Ada 1983: Ada made an ISO standard –No dialects allowed –Formal compiler validation procedure (> 4,000 tests) 1995: Ada ISO standard brought up to date
© ACT Europe under the GNU Free Documentation License 14 Ada goals: –Reliability & maintainability –Large, long-lasting, complex projects –Standardization & validation In 1998, still nothing else exists!
© ACT Europe under the GNU Free Documentation License 15 Source: MITRE (Avionics domain) 270,000 LOC 225,000 LOC 150,000 LOC 135,000 LOC 112,500 LOC 75,000 LOC Average Annual Costs for Software Maintenance
© ACT Europe under the GNU Free Documentation License 16 Industrial Applications in Ada Weirton Steel - process controller Volvo manufacturing plant Orson & Prompt - videotape applications Astree -French developed, Europe-wide railroad operation Helsinki Radiotelescope CANAL+ - decoder boxes BNP - trading systems Flight Warning System - Airbus A340 Boeing 777
© ACT Europe under the GNU Free Documentation License 17 Background on Ada 95 Programming with Ada 95
© ACT Europe under the GNU Free Documentation License 18 The C programming style: Conventions & NO Verification void main () { printf (“Hello world.\n”); } void main () { printf (“Hello world.\n”); } void main () { printf (12345); } void main () { printf (12345); }
© ACT Europe under the GNU Free Documentation License 19 #include void main () { printf (“Hello world in C\n”); } #include void main () { printf (“Hello world in C\n”); } NO guarantee you will link with –the correct library –the good version of the correct library Even with...
© ACT Europe under the GNU Free Documentation License 20 with Text_IO; procedure Hello is begin Text_IO.Put_Line (“Hello world in Ada”); end Hello; with Text_IO; procedure Hello is begin Text_IO.Put_Line (“Hello world in Ada”); end Hello; hello.adb #include void main () { printf (“Hello world in C\n”); } #include void main () { printf (“Hello world in C\n”); } hello.c
© ACT Europe under the GNU Free Documentation License 21 with Text_IO; procedure Hello is begin Text_IO.Put_Line (“Hello world in Ada”); end Hello; with Text_IO; procedure Hello is begin Text_IO.Put_Line (“Hello world in Ada”); end Hello; hello.adb % gnatmake -q hello % hello Hello world in Ada % % gnatmake -q hello % hello Hello world in Ada % Predefined Ada library subprogram in library Checks you are using the correct version of every module & library
© ACT Europe under the GNU Free Documentation License 22 with Text_IO; use Text_IO; procedure Hello is begin Text_IO.Put_Line (“Hello world in Ada”); end Hello; with Text_IO; use Text_IO; procedure Hello is begin Text_IO.Put_Line (“Hello world in Ada”); end Hello; hello.adb
© ACT Europe under the GNU Free Documentation License 23 with Hello; procedure Two_Hello is begin Hello; end Two_Hello; with Hello; procedure Two_Hello is begin Hello; end Two_Hello; two_hello.adb with Text_IO; procedure Hello is begin Text_IO.Put_Line (“Hello world in Ada”); end Hello; with Text_IO; procedure Hello is begin Text_IO.Put_Line (“Hello world in Ada”); end Hello; hello.adb %gnatmake -q two_hello % two_hello Hello world in Ada % %gnatmake -q two_hello % two_hello Hello world in Ada %
© ACT Europe under the GNU Free Documentation License 24 with Hello; with Fact; procedure Main is begin for I in 1.. Fact (4) loop Hello; end loop; end Main; with Hello; with Fact; procedure Main is begin for I in 1.. Fact (4) loop Hello; end loop; end Main; main.adb with Text_IO; use Text_IO; procedure Hello is begin Put_Line (“Hello”); end Hello; with Text_IO; use Text_IO; procedure Hello is begin Put_Line (“Hello”); end Hello; hello.adb function Fact (N : Integer) return Integer is begin if N <= 1 then return 1; else return N * Fact (N-1); end if; end Fact; function Fact (N : Integer) return Integer is begin if N <= 1 then return 1; else return N * Fact (N-1); end if; end Fact; fact.adb
© ACT Europe under the GNU Free Documentation License 25 General Structure with... procedure Some_Main is begin …. end Some_Main; with... procedure Some_Main is begin …. end Some_Main; with... Ada Library with... Ada Library
© ACT Europe under the GNU Free Documentation License 26 Programming with Ada 95 –Scalar data types –Checks & Exceptions –Access types (pointers) –Arrays –Records –Parameter passing
© ACT Europe under the GNU Free Documentation License 27 Elementary Ada types Scalar –Discrete integer (Integer) enumeration (Boolean, Character) –Real floating point (Float) Access (pointers)
© ACT Europe under the GNU Free Documentation License 28 Integer types function Compute (P, Q : Integer) return Integer is R : Integer; M : Integer := 2 * P; begin R := Q / M; return R; end Compute;
© ACT Europe under the GNU Free Documentation License 29 function Compute (P, Q : Integer) return Integer is type My_Int is range _000_000; T : My_Int; begin T := P + 1; T := My_Int (P) + 1; return Integer (T) + Q; end Compute; Compilation ERROR Ada is strongly typed Introduces a new type Explicit conversion needed
© ACT Europe under the GNU Free Documentation License 30 Enumeration types procedure Compute (A : Character; B : Boolean) is type Day is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); D : Day := Wed; C : Character := ‘W’; Week_Day : Boolean := D in Mon.. Fri; Lower_Case : Boolean := A in ‘a’.. ‘z’; begin Week_Day := Week_Day or B; end Compute;
© ACT Europe under the GNU Free Documentation License 31 Real types procedure Compute (M : Integer) is Pi : constant := 3.141; F : constant Float := Float (M); R : Float := F * Pi; A : Integer := Integer (R); begin null; end Compute; Explicit conversions needed
© ACT Europe under the GNU Free Documentation License 32 Type Attributes TYPE ’ First : smallest value in TYPE TYPE ’ Last : biggest value in TYPE TYPE ’ Image (X) : String representation of X (X in TYPE)
© ACT Europe under the GNU Free Documentation License 33 with Text_IO; procedure Print (A : Integer; P : Float) is type My_Int is range _000_000; T : My_Int := My_Int ’ Last; type Day is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); D : Day := Day ’ First; B : Integer := Integer ’ First; begin Text_IO.Put (Integer ’ Image (A)); Text_IO.Put (Float ’ Image (P)); end Compute;
© ACT Europe under the GNU Free Documentation License 34 Programming with Ada 95 –Scalar data types –Checks & Exceptions –Access types (pointers) –Arrays –Records –Parameter passing
© ACT Europe under the GNU Free Documentation License 35 Overflow in C #include void main { int k = INT_MAX; k = k + 1; } Semantics undefined
© ACT Europe under the GNU Free Documentation License 36 void do_something (int m, int n) { int k; for (k = m; k <= n; k++) { /* Do something */... } Reliability: may loop forever Portability: may only arise on certain platforms (16 / 32 / 64 bit architecture) Potential problems...
© ACT Europe under the GNU Free Documentation License 37 Overflow & Constraint Checks procedure Checks is K : Integer := Integer ’ Last; begin K := K + 1; end Checks; exception Constraint_Error raised during execution % gnatmake -q checks % checks raised CONSTRAINT_ERROR %
© ACT Europe under the GNU Free Documentation License 38 procedure Checks (X : Integer) is type My_Int is range _000_000; T : My_Int; begin T := My_Int (X); end Checks; Constraint_Error raised if X not in _000_000
© ACT Europe under the GNU Free Documentation License 39 Subtypes type Day_Of_A_Month is range ; type Day_Of_February is range ; D1 : Day_Of_February := 25; D2 : Day_Of_A_Month := D1; D2 : Day_Of_A_Month := Day_Of_A_Month (D1); Compilation ERROR Ada is strongly typed OK, but tedious
© ACT Europe under the GNU Free Documentation License 40 type Day_Of_A_Month is range ; subtype Day_Of_February is Day_Of_A_Month range ; D1 : Day_Of_February := 25; D2 : Day_Of_A_Month := D1; OK Constraint_Error raised if D3 not in D3 : Day_Of_A_Month; D4 : Day_Of_February := D3;
© ACT Europe under the GNU Free Documentation License 41 Predefined Subtypes subtype Natural is Integer range 0.. Integer ’ Last; subtype Positive is Natural range 1.. Natural ’ Last;
© ACT Europe under the GNU Free Documentation License 42 Exceptions procedure Checks is A : Integer := Integer ’ First; begin A := A - 1; end Checks; exception Constraint_Error raised during execution % gnatmake -q checks % checks raised CONSTRAINT_ERROR %
© ACT Europe under the GNU Free Documentation License 43 Predefined Exceptions Constraint_Error: overflow, computation error (divide by zero), array index out of range, … Storage_Error: no more memory available Program_Error: fundamental program error (e.g. end of function with no return statement)
© ACT Europe under the GNU Free Documentation License 44 Catching an Exception with Text_IO; use Text_IO; procedure Checks is A : Integer := Integer ’ First; begin A := A - 1; exception when Constraint_Error => Put_Line (“Overflow occurred”); end Checks; % gnatmake -q checks % checks Overflow occurred %
© ACT Europe under the GNU Free Documentation License 45 procedure Checks is Internal_Error : exception; Creating your own exceptions procedure Foo is begin raise Internal_Error; end Foo; procedure Bar is begin Foo; end Bar; begin Bar; exception... end Checks; Exception Handler
© ACT Europe under the GNU Free Documentation License 46 procedure Foo is begin raise Internal_Error; end Foo; procedure Bar is begin Foo; end Bar; begin Bar; exception when Internal_Error => Put_Line (“problem occurred”); when others => Put_Line (“some other exception”); end Checks; Exception Handler
© ACT Europe under the GNU Free Documentation License 47 procedure Foo is begin raise Internal_Error; end Foo; procedure Bar is begin Foo; end Bar; begin Bar; exception when Internal_Error => Put_Line (“problem occurred”); when others => Put_Line (“some other exception”); end Checks; Exception Handler
© ACT Europe under the GNU Free Documentation License 48 procedure Foo is begin raise Internal_Error; end Foo; procedure Bar is begin Foo; end Bar; begin Bar; exception when Internal_Error => Put_Line (“problem occurred”); when others => Put_Line (“some other exception”); end Checks;
© ACT Europe under the GNU Free Documentation License 49 Catching an exception where YOU want Want to catch some exception in a region of code without exiting from the subprogram } procedure Checks is... begin end Checks;
© ACT Europe under the GNU Free Documentation License 50 Use declare Blocks procedure Checks is... begin end Checks; Some_Label : declare begin exception end Some_Label; Statements Declarations Handler
© ACT Europe under the GNU Free Documentation License 51 Example procedure Calc (A, B : Float) is C, D : Float; begin declare Old_C : Float := C; begin C := A * B; D := C ** 2; exception when Constraint_Error => C := Old_C; D := 0.0; end; end Calc;
© ACT Europe under the GNU Free Documentation License 52 Programming with Ada 95 –Scalar data types –Checks & Exceptions –Access types (pointers) –Arrays –Records –Parameter passing
© ACT Europe under the GNU Free Documentation License 53 Access Types type Int_Ptr is access Integer; P : Int_Ptr; pointers are initialized to null by default pointers are initialized to null by default Memory
© ACT Europe under the GNU Free Documentation License 54 type Int_Ptr is access Integer; P : Int_Ptr; P := new Integer; Memory P ???
© ACT Europe under the GNU Free Documentation License 55 type Int_Ptr is access Integer; P : Int_Ptr; P := new Integer; P.all := 3344; P Memory 3334
© ACT Europe under the GNU Free Documentation License 56 type Int_Ptr is access Integer; P : Int_Ptr; P := new Integer ’ (1234); P Memory 1234
© ACT Europe under the GNU Free Documentation License 57 type Int_Ptr is access Integer; P : Int_Ptr; X : Integer := 567; P := new Integer ’ (X); P Memory 567
© ACT Europe under the GNU Free Documentation License 58 type Int_Ptr is access Integer; type Another_Int_Ptr is access Integer; P : Int_Ptr := new Integer; Q : Another_Int_Ptr; Q := Another_Int_Ptr (P); 567 COMPILATION ERROR you must use general access types to perform pointer conversions
© ACT Europe under the GNU Free Documentation License 59 type Ptr_Int is access all Integer; P : Ptr_Int; Y : aliased Integer := 9999; Memory 9999 Y General Access Types
© ACT Europe under the GNU Free Documentation License 60 type Ptr_Int is access all Integer; P : Ptr_Int; Y : aliased Integer := 9999; P := Y ’ access; Memory P 9999 Y
© ACT Europe under the GNU Free Documentation License 61 type Ptr_Int is access all Integer; P : Ptr_Int; Y : aliased Integer := 9999; P := Y ’ access; P.all := 1234; P Memory 1234 Y
© ACT Europe under the GNU Free Documentation License 62 type Ptr_Int is access all Integer; P : Ptr_Int; X : Integer; P := X ’ access; compilation error X is not aliased
© ACT Europe under the GNU Free Documentation License 63 type Int_Ptr is access all Integer; type Another_Int_Ptr is access all Integer; P : Int_Ptr := new Integer; Q : Another_Int_Ptr; Q := Another_Int_Ptr (P); OK
© ACT Europe under the GNU Free Documentation License 64 Programming with Ada 95 –Scalar data types –Checks & Exceptions –Access types (pointers) –Arrays –Records –Parameter passing
© ACT Europe under the GNU Free Documentation License 65 Composite Ada types array (String) record tagged record protected types tasks
© ACT Europe under the GNU Free Documentation License 66 One of a Kind Arrays procedure Compute (N : Integer) is A : array (1.. N) of Float; begin for I in 1.. N loop A (I) := 3.141; end loop; end Compute; Arrays can have - dynamic bounds - dynamic size
© ACT Europe under the GNU Free Documentation License 67 Typed Arrays procedure Compute (N : Integer) is type Arr is array (Integer range <>) of Float; A : Arr (1.. N); B : Arr := A; B takes its bounds from A Constraint_Error if C’Length /= A’Length Constraint_Error if A’Last < 8 C : Arr ( ); begin C := A C ( ) := A (5.. 8);
© ACT Europe under the GNU Free Documentation License 68 A : array ( ) of Float; B : array ( ) of Float; A := B; Compilation error A and B are one of a kind
© ACT Europe under the GNU Free Documentation License 69 1-Dim Array Attributes ARRAY ’ First : smallest index value in ARRAY ARRAY ’ Last : biggest index value in ARRAY ARRAY ’ Length : # of elements in ARRAY ARRAY ’ range : ARRAY ’ First.. ARRAY ’ Last
© ACT Europe under the GNU Free Documentation License 70 type Vector is array (Natural range <>) of Float; function Max (V : Vector) return Float is M : Float := Float ’ First; begin for I in V ’ range loop if V (I) > M then M := V (I); end if; end loop; return M; end Max;
© ACT Europe under the GNU Free Documentation License 71 type Vector is array (Natural range <>) of Float; function Max (V : Vector) return Float; V1 : Vector := (0.0, 1.0, 2.0, 3.0, 4.0, 5.0); V2 : Vector ( ) := (1.0, 2.0, others => 99.0); X : Float := Max (V1); Y : Float := Max (V2); V1’First = 0 V1’Last = 6 V1’Length = 7 V2’First = 1 V2’Last = 100 V2’Length = 100
© ACT Europe under the GNU Free Documentation License 72 Predefined Array Type type String is array (Positive range <>) of Character; R : String (1.. 10); S : String := (‘H’, ‘e’, ‘l’, ‘l’, ‘o’); T : String := “Hello”; Q : String := S & “ “ &T “ you”; Q = “Hello Hello You”
© ACT Europe under the GNU Free Documentation License 73 Programming with Ada 95 –Scalar data types –Checks & Exceptions –Access types (pointers) –Arrays –Records –Parameter passing
© ACT Europe under the GNU Free Documentation License 74 Record Types type Date is record Day : Positive range ; Month : Positive range ; Year : Integer; end record; D : Date := (3, 9, 1975); A : Date := (Day => 31, Month => 12, Year => 1999); B : Date := A; Y : Integer := B. Year;
© ACT Europe under the GNU Free Documentation License 75 type Node; type Node_Ptr is access Node; type Node is record D : Date := (1, 1, 1900); Next : Node_Ptr; end record; P1 : Node_Ptr := new Node; Memory P null P2 P2 : Node_Ptr := new Node ’ ((3, 9, 1975), P1);
© ACT Europe under the GNU Free Documentation License 76 N : Node := ((31, 12, 1999), null); P3 : Node_Ptr := new Node ’ (N); Memory P null
© ACT Europe under the GNU Free Documentation License 77 Record fields: simple rule P pointer to a record P.all points to the WHOLE record P.all.Field points to Field in the record P.Field same as P.all.Field
© ACT Europe under the GNU Free Documentation License 78 type Node is record D : Date := (1, 1, 1900); Next : Node_Ptr; end record; P : Node_Ptr := new Node; DD : Date := P.D; NN : Node_Ptr := P.Next;
© ACT Europe under the GNU Free Documentation License 79 Parametrizing Records: Discriminants type Q_array (Natural range <>) of Integer; type Queue (Max_Size : Natural) is record First : Natural; Last : Natural; Size : Natural; Q : Q_Array (0.. Max_Size); end record;
© ACT Europe under the GNU Free Documentation License 80 type Q_Array (Positive range <>) of Integer; type Queue (Max_Size : Positive) is record First : Positive := 1; Last : Positive := 1; Size : Natural := 0; Q : Q_Array (1.. Max_Size); end record; X : Queue (4); X.Max_Size = 4
© ACT Europe under the GNU Free Documentation License 81 X : Queue :=(Max_Size => 4, First => 2, Last => 3, Size => 2, Q => (0, 11, 22, 0)); X : Queue; Compilation error Queue is an unconstrained type must specify discriminant value X.Max_Size = ???
© ACT Europe under the GNU Free Documentation License 82 Programming with Ada 95 –Scalar data types –Checks & Exceptions –Access types (pointers) –Arrays –Records –Parameter passing
© ACT Europe under the GNU Free Documentation License 83 Parameter Passing in (functions & procedures) in out (procedures only) out (procedures only)
© ACT Europe under the GNU Free Documentation License 84 function Inc (X : Integer) return Integer is begin X := X + 1; return X; end Inc; Compilation error X is like a constant inside Inc in parameters are copied IN during a subprogram call
© ACT Europe under the GNU Free Documentation License 85 procedure Inc (X : in out Integer) is begin X := X + 1; end Inc; Val : Integer := 3; Inc (Val); -- here Val = 4 X is a regular variable inside Inc in out parameters are copied IN during a subprogram call and copied OUT upon return
© ACT Europe under the GNU Free Documentation License 86 procedure Random (X : out Integer) is begin -- compute random number X := …; end Random; Val : Integer; Random (Val); X is a regular variable inside Inc without initial value out parameters are copied OUT upon return
© ACT Europe under the GNU Free Documentation License 87 Control Structures if-then-else case statements loops –for –while –generic