SlideShare a Scribd company logo
1 of 87
Download to read offline
Franco Gasperoni
                             gasperon@act-europe.fr
                             http://libre.act-europe.fr

                                                                                           1
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
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 http://libre.act-europe.fr/ is kept at the bottom of
             every non-title slide. A copy of the license is available at:
           •                          http://www.fsf.org/licenses/fdl.html




                                                                                            2
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
3
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
http://www-inf.enst.fr/~domdl

                  • Programming in Ada 95
                       by John Barnes (Addison Wesley)


                  • Lovelace
                             On line Ada 95 tutorial



                                                                                             4
http://libre.act-europe.fr           © ACT Europe under the GNU Free Documentation License
Course Objectives

           • Building reliable software systems

           • Comparing structured & OO software design

           • Programming distributed systems

           • Practice, practice, practice


                                                                                         5
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
Lectures
      ••    Introduction to Ada 95
           Introduction to Ada 95
      ••    Building reliable SW systems
           Building reliable SW systems                                                   3+3 h

      ••    Structured design
           Structured design
      ••    TP 1
           TP 1
                      •• OO design
                         OO design
                      •• Java
                         Java
              4.5+3 h •• TP 2
                         TP 2

                             ••   Distributed programming
                                  Distributed programming
                             ••   TP 3
                                  TP 3                                                        3+3 h
                                                                                                      6
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
Course Evaluation

           • TP OO: 6 points

           • TP distributed programming: 4 points




                                                                                         7
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
• Background on Ada 95


                         • Programming with Ada 95




                                                                                         8
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
1950                          1960               1970                             1980                       1990                2000

                                        PL/I(66)

                                                                                                                      Ada(95)

                                                                                               Ada(83)
                        Cobol(58)
A S S E M B L Y




                                              Pascal(70)
                                                                                                                        Java(96)
                            Algol(60)

                                                               C(72)                                       C++(89)

                  Fortran(54)
                                        Simula(67)                             Smalltalk(80)



                                                                                                                            imperative
                                        Basic(66)
                                                                                                                                   9
http://libre.act-europe.fr                         © ACT Europe under the GNU Free Documentation License
Software Crisis: 1976

        • Many languages (>450)
               – Many dialects
        • Low interoperability
        • High maintenance costs
        • Low reliability




                                                                                         10
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
The 70s - Mid 80s

                                                 • Biggest SW contractor

                                                 • SW with long life
                                                        cycles (10-30 years)

                                                 • Safety critical apps



                                                                                        11
http://libre.act-europe.fr      © ACT Europe under the GNU Free Documentation License
Safety Critical

                        Is 99.9% acceptable for defects?


       • Drinking water contaminated for 1 h/month

       • 2 accidents/month at O’Hare International Airport

       • 22,000 checks drawn from the wrong account / hour



                                                                                         12
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
Ada

      • 1975 study: No language was adequate for
                             safety critical applications
      • 1976-80: 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
                                                                                        13
http://libre.act-europe.fr      © ACT Europe under the GNU Free Documentation License
Ada goals:

              – Reliability & maintainability

              – Large, long-lasting, complex projects

              – Standardization & validation



       In 1998, still nothing else exists!



                                                                                     14
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
Average Annual Costs for
                                      Software Maintenance
                              1800
                              1600
      1000s of 1994 Dollars




                                        Ada                                                                      270,000 LOC
                              1400
                                        Other HOLs
                              1200      C                                                                        225,000 LOC

                              1000                                      135,000 LOC

                              800                                                                                150,000 LOC

                              600                                           112,500 LOC

                              400
                                                                            75,000 LOC
                              200
                                 0
                                         350         700                1,050                 1,400      1,750      2,100
                                                              Function Points

                                          Source: MITRE (Avionics domain)                                                      15
http://libre.act-europe.fr                       © ACT Europe under the GNU Free Documentation License
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
                                                                                     16
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
• Background on Ada 95


                         • Programming with Ada 95




                                                                                         17
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
The C programming style:
           Conventions & NO Verification

         void main ()                                                        void main ()
                                                                            void main ()
        void main ()
        {{                                                                  {{
            printf (“Hello world.n”);                                          printf (12345);
                                                                               printf (12345);
           printf (“Hello world.n”);
        }}                                                                  }}




                                                                                                  18
http://libre.act-europe.fr     © ACT Europe under the GNU Free Documentation License
Even with ...
                             #include <stdio.h>
                             #include <stdio.h>

                              void main ()
                             void main ()
                             {{
                                 printf (“Hello world in Cn”);
                                printf (“Hello world in Cn”);
                             }}


       • NO guarantee you will link with
              – the correct library
              – the good version of the correct library

                                                                                              19
http://libre.act-europe.fr            © ACT Europe under the GNU Free Documentation License
hello.adb
           hello.adb
                     with Text_IO;
                     with Text_IO;

                     procedure Hello is
                     procedure Hello is
                     begin
                     begin
                        Text_IO.Put_Line (“Hello world in Ada”);
                        Text_IO.Put_Line (“Hello world in Ada”);
                     end Hello;
                     end Hello;
            hello.c
            hello.c
                 #include <stdio.h>
                 #include <stdio.h>

                      void main ()
                     void main ()
                     {{
                         printf (“Hello world in Cn”);
                        printf (“Hello world in Cn”);
                     }}
                                                                                             20
http://libre.act-europe.fr           © ACT Europe under the GNU Free Documentation License
Predefined Ada library
          hello.adb
          hello.adb                                                               subprogram in library
                    with Text_IO;
                    with Text_IO;

                    procedure Hello is
                    procedure Hello is
                    begin
                    begin
                       Text_IO.Put_Line (“Hello world in Ada”);
                       Text_IO.Put_Line (“Hello world in Ada”);
                    end Hello;
                    end Hello;

                   % gnatmake -q hello
                   % gnatmake -q hello
                   % hello
                   % hello
                   Hello world in Ada
                   Hello world in Ada                                   Checks you are using
                   %
                   %                                                    the correct version
                                                                        of every module & library

                                                                                                    21
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
hello.adb
          hello.adb
                    with Text_IO; use Text_IO;
                    with Text_IO; use Text_IO;

                    procedure Hello is
                    procedure Hello is
                    begin
                    begin
                       Text_IO.Put_Line (“Hello world in Ada”);
                       Text_IO.Put_Line (“Hello world in Ada”);
                    end Hello;
                    end Hello;




                                                                                          22
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
hello.adb
         hello.adb
                  with Text_IO;
                  with Text_IO;
                  procedure Hello is
                  procedure Hello is
                  begin
                  begin
                     Text_IO.Put_Line (“Hello world in Ada”);
                     Text_IO.Put_Line (“Hello world in Ada”);
                  end Hello;
                  end Hello;
                                                                     %gnatmake -q two_hello
                                                                     %gnatmake -q two_hello
                                                                     % two_hello
                                                                     % two_hello
          two_hello.adb
         two_hello.adb                                               Hello world in Ada
                                                                     Hello world in Ada
                                                                     Hello world in Ada
                                                                     Hello world in Ada
                      with Hello;
                     with Hello;                                     %
                                                                     %
                      procedure Two_Hello is
                     procedure Two_Hello is
                      begin
                     begin
                           Hello;
                           Hello;
                           Hello;
                           Hello;
                      end Two_Hello;
                     end Two_Hello;ACT Europe under the GNU Free Documentation License   23
http://libre.act-europe.fr        ©
with Text_IO; use Text_IO;
   with Text_IO; use Text_IO;                      function Fact (N ::Integer) return Integer is
                                                    function Fact (N Integer) return Integer is
    procedure Hello is
   procedure Hello is                               begin
                                                   begin
    begin
   begin                                               if N <= 1 then
                                                      if N <= 1 then
       Put_Line (“Hello”);
      Put_Line (“Hello”);                                   return 1;
                                                           return 1;
    end Hello;
   end Hello;                                          else
                               hello.adb              else
                                                           return N **Fact (N-1);
                                                            return N Fact (N-1);
                                                       end if;
                                                      end if;
                                                    end Fact;
                                                   end Fact;
                                                                                                  fact.adb


                              with Hello;
                             with Hello;
                              with Fact;
                             with Fact;
                              procedure Main is
                             procedure Main is
                              begin
                             begin
                                for IIin 1 .. Fact (4) loop
                                 for in 1 .. Fact (4) loop
                                     Hello;
                                    Hello;
                                 end loop;
                                end loop;
                              end Main;
                             end Main;
                                                             main.adb                                 24
http://libre.act-europe.fr                © ACT Europe under the GNU Free Documentation License
General Structure
   Ada Library                                                                                              Ada Library

                                                                                with ...

                                   with ...




                                                                                                 with ...

                       with ...




                                                            with ...
                                                           with ...
                                                            procedure Some_Main is
                                                           procedure Some_Main is
                                                            begin
                                                           begin
                                                               ….
                                                              ….
                                                            end Some_Main;
                                                           end Some_Main;                                                 25
http://libre.act-europe.fr                    © ACT Europe under the GNU Free Documentation License
• Programming with Ada 95
                             – Scalar data types
                             – Checks & Exceptions
                             – Access types (pointers)
                             – Arrays
                             – Records
                             – Parameter passing

                                                                                            26
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Elementary Ada types

           • Scalar
                  – Discrete
                         • integer (Integer)
                         • enumeration (Boolean, Character)
                  – Real
                         • floating point (Float)
           • Access (pointers)
                                                                                             27
http://libre.act-europe.fr           © ACT Europe under the GNU Free Documentation License
Integer types
             function Compute (P, Q : Integer) return Integer is
               R : Integer;
               M : Integer := 2 * P;

             begin
               R := Q / M;
               return R;
             end Compute;




                                                                                       28
http://libre.act-europe.fr     © ACT Europe under the GNU Free Documentation License
Introduces a new type

             function Compute (P, Q : Integer) return Integer is

                  type My_Int is range -100 .. 1_000_000;

                  T : My_Int;                                                      Compilation ERROR
                                                                                   Ada is strongly typed
             begin
               T := P + 1;

                  T := My_Int (P) + 1;                                             Explicit conversion
                                                                                   needed
               return Integer (T) + Q;
             end Compute;


                                                                                                     29
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
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;                                                                   30
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Real types
             procedure Compute (M : Integer) is
               Pi : constant := 3.141;

                  F : constant Float := Float (M);

                  R : Float := F * Pi;                                               Explicit conversions
                                                                                     needed
                  A : Integer := Integer (R);

             begin
               null;
             end Compute;

                                                                                                      31
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
Type Attributes


        • TYPE ’ First : smallest value in TYPE

        • TYPE ’ Last : biggest value in TYPE


        • TYPE ’ Image (X) : String representation of X
                             (X in TYPE)


                                                                                       32
http://libre.act-europe.fr     © ACT Europe under the GNU Free Documentation License
with Text_IO;
                  procedure Print (A : Integer; P : Float) is

                       type My_Int is range -100 .. 1_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;
                                                                                              33
http://libre.act-europe.fr            © ACT Europe under the GNU Free Documentation License
• Programming with Ada 95
                             – Scalar data types
                             – Checks & Exceptions
                             – Access types (pointers)
                             – Arrays
                             – Records
                             – Parameter passing

                                                                                            34
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Overflow in C

                             #include <limits>

                             void main
                             {
                               int k = INT_MAX;

                                 k = k + 1;
                             }                                                           Semantics
                                                                                         undefined


                                                                                                     35
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
Potential problems ...
                             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) 36
http://libre.act-europe.fr              © ACT Europe under the GNU Free Documentation License
Overflow & Constraint Checks
                 procedure Checks is

                      K : Integer := Integer ’ Last;

                 begin                                                                   exception
                   K := K + 1;                                                        Constraint_Error
                 end Checks;                                                               raised
                                                                                      during execution
                 % gnatmake -q checks
                 % checks

                 raised CONSTRAINT_ERROR
                 %
                                                                                                         37
http://libre.act-europe.fr           © ACT Europe under the GNU Free Documentation License
procedure Checks (X : Integer) is

                      type My_Int is range -100 .. 1_000_000;

                      T : My_Int;
                                                                             Constraint_Error
                 begin                                                             raised
                   T := My_Int (X);                                             if X not in
                 end Checks;                                                -100 .. -1_000_000




                                                                                                 38
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Subtypes


      type Day_Of_A_Month is range 1 .. 31;
      type Day_Of_February is range 1 .. 28;
                                                                              Compilation ERROR
      D1 : Day_Of_February := 25;                                             Ada is strongly typed

      D2 : Day_Of_A_Month := D1;

      D2 : Day_Of_A_Month := Day_Of_A_Month (D1);

                                                                            OK, but tedious
                                                                                               39
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
type Day_Of_A_Month is range 1 .. 31;

  subtype Day_Of_February is Day_Of_A_Month range 1 .. 28;


  D1 : Day_Of_February := 25;

  D2 : Day_Of_A_Month := D1;                             OK




                                                                          Constraint_Error
  D3 : Day_Of_A_Month;
                                                                              raised if
                                                                           D3 not in 1 .. 28
  D4 : Day_Of_February := D3;

                                                                                               40
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
Predefined Subtypes

                  subtype Natural is Integer range 0 .. Integer ’ Last;

                  subtype Positive is Natural range 1 .. Natural ’ Last;




                                                                                          41
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
Exceptions
                 procedure Checks is
                   A : Integer := Integer ’ First;
                                                                                   exception
                                                                                Constraint_Error
                 begin
                                                                                     raised
                   A := A - 1;
                                                                                during execution
                 end Checks;


                 % gnatmake -q checks
                 % checks

                 raised CONSTRAINT_ERROR
                 %
                                                                                                   42
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
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)


                                                                                           43
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
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
                         %
                                                                                             44
http://libre.act-europe.fr           © ACT Europe under the GNU Free Documentation License
procedure Checks is

                                           Internal_Error : exception;

  Creating                                  procedure Foo is
                                            begin
   your own                                    raise Internal_Error;
     exceptions                             end Foo;
                                            procedure Bar is
                                            begin
                                               Foo;
                                            end Bar;

                                             begin
                                                    Bar;
                                             exception
                                                     Exception Handler
                                                    ...
                                                                                     45
http://libre.act-europe.fr                   end Checks;
                             © ACT Europe under the GNU Free Documentation License
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”);
                                 Exception Handler
                           when others =>
                              Put_Line (“some other exception”);
                         end Checks;                                                           46
http://libre.act-europe.fr             © ACT Europe under the GNU Free Documentation License
procedure Foo is
                             begin
                                raise Internal_Error;
              1
                             end Foo;
                             procedure Bar is                                     2
                             begin
                                Foo;
                3
                             end Bar;

                         begin                                                       4
                           Bar;
        5
                         exception
                           when Internal_Error =>
                              Put_Line (“problem occurred”);
                                 Exception Handler
                           when others =>
                              Put_Line (“some other exception”);
                         end Checks;                                                           47
http://libre.act-europe.fr             © ACT Europe under the GNU Free Documentation License
procedure Foo is
                             begin
                                raise Internal_Error;
              1
                             end Foo;
                             procedure Bar is                                     2
                             begin
                                Foo;
                3            end Bar;

                         begin                                                       4
                           Bar;
        5
                         exception
                           when Internal_Error =>
                              Put_Line (“problem occurred”);
                           when others =>
                              Put_Line (“some other exception”);
                         end Checks;                                                           48
http://libre.act-europe.fr             © ACT Europe under the GNU Free Documentation License
Catching an exception
                     where YOU want
              procedure Checks is
                ...
              begin




                                     }                      Want to catch some
                                                            exception in a region
                                                            of code without exiting
                                                            from the subprogram

                                                                                             49
http://libre.act-europe.fr Checks;
                    end              © ACT Europe under the GNU Free Documentation License
procedure Checks is
                                     ...
   Use                             begin

    declare
      Blocks                             Some_Label : declare
                                            Declarations
                                         begin
                                                    Statements
                                         exception
                                                        Handler
                                         end Some_Label;



                                   end Checks;                                       50
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
procedure Calc (A, B : Float) is
                                  C, D : Float;
       Example                  begin



                                      declare
                                        Old_C : Float := C;
                                      begin
                                        C := A * B;
                                        D := C ** 2;
                                      exception
                                        when Constraint_Error =>
                                           C := Old_C;
                                           D := 0.0;
                                      end;

                                                                                     51
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
• Programming with Ada 95
                             – Scalar data types
                             – Checks & Exceptions
                             – Access types (pointers)
                             – Arrays
                             – Records
                             – Parameter passing

                                                                                            52
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Access Types

            type Int_Ptr is access Integer;

            P : Int_Ptr;
                                                                                     pointers
                                                                                     pointers
                                                                                  are initialized
                                                                                  are initialized
                                                                                       to null
                                                                                      to null
                                                                                    by default
                                                                                    by default




                                                                                           Memory
                                                                                                53
http://libre.act-europe.fr    © ACT Europe under the GNU Free Documentation License
type Int_Ptr is access Integer;

        P : Int_Ptr;
                                                                P

        P := new Integer;                                                              ???




                                                                                     Memory
                                                                                              54
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
type Int_Ptr is access Integer;

        P : Int_Ptr;
                                                                P

        P := new Integer;                                                              3334


        P.all := 3344;




                                                                                     Memory
                                                                                              55
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
type Int_Ptr is access Integer;

        P : Int_Ptr;
                                                                P

        P := new Integer ’ (1234);                                                     1234




                                                                                     Memory
                                                                                              56
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
type Int_Ptr is access Integer;

        P : Int_Ptr;
                                                                 P
        X : Integer := 567;
                                                                                        567
        P := new Integer ’ (X);




                                                                                      Memory
                                                                                               57
http://libre.act-europe.fr    © ACT Europe under the GNU Free Documentation License
type Int_Ptr is access Integer;

        type Another_Int_Ptr is access Integer;

        P : Int_Ptr := new Integer;
                                                                                       567
        Q : Another_Int_Ptr;
                                                          COMPILATION ERROR
        Q := Another_Int_Ptr (P);
                                                               you must use
                                                           general access types
                                                            to perform pointer
                                                                conversions



                                                                                             58
http://libre.act-europe.fr     © ACT Europe under the GNU Free Documentation License
General Access Types


        type Ptr_Int is access all Integer;

        P : Ptr_Int;

        Y : aliased Integer := 9999;
                                                                                                9999
                                                                                          Y



                                                                                              Memory
                                                                                                       59
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
P
        type Ptr_Int is access all Integer;

        P : Ptr_Int;

        Y : aliased Integer := 9999;
                                                                                           9999
        P := Y ’ access;
                                                                                     Y



                                                                                         Memory
                                                                                                  60
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
P
        type Ptr_Int is access all Integer;

        P : Ptr_Int;

        Y : aliased Integer := 9999;
                                                                                           1234
        P := Y ’ access;
                                                                                     Y
        P.all := 1234;

                                                                                         Memory
                                                                                                  61
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
type Ptr_Int is access all Integer;

        P : Ptr_Int;

        X : Integer;                                             compilation
                                                                    error
        P := X ’ access;                                          X is not
                                                                   aliased



                                                                                     62
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
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




                                                                                       63
http://libre.act-europe.fr     © ACT Europe under the GNU Free Documentation License
• Programming with Ada 95
                             – Scalar data types
                             – Checks & Exceptions
                             – Access types (pointers)
                             – Arrays
                             – Records
                             – Parameter passing

                                                                                            64
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Composite Ada types


           • array (String)
           • record
           • tagged record
           • protected types
           • tasks
                                                                                          65
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
One of a Kind Arrays

                             procedure Compute (N : Integer) is

                               A : array (1 .. N) of Float;                                    Arrays can have
                                                                                               - dynamic bounds
                             begin                                                             - dynamic size
                               for I in 1 .. N loop
                                   A (I) := 3.141;
                               end loop;
                             end Compute;



                                                                                                             66
http://libre.act-europe.fr             © ACT Europe under the GNU Free Documentation License
Typed Arrays
                       procedure Compute (N : Integer) is
                         type Arr is array (Integer range <>) of Float;

                             A : Arr (1 .. N);
                                                                             B takes its bounds from A
                             B : Arr := A;

                             C : Arr (11 .. 20);

                       begin
                                                                       Constraint_Error
                         C := A
                                                                       if C’Length /= A’Length

                             C (15 .. 18) := A (5 .. 8);
                                                                                         Constraint_Error
                                                                                         if A’Last < 8    67
http://libre.act-europe.fr                © ACT Europe under the GNU Free Documentation License
A : array (10 .. 20) of Float;

                             B : array (10 .. 20) of Float;

                             A := B;

                                                                          Compilation error
                                                                            A and B are
                                                                            one of a kind



                                                                                               68
http://libre.act-europe.fr             © ACT Europe under the GNU Free Documentation License
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

                                                                                           69
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
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;
                                                                                           70
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
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);
                                                                                        V1’First = 0
                                                                                        V1’Last = 6
                                                                                        V1’Length = 7


        V2 : Vector (1 .. 100) := (1.0, 2.0, others => 99.0);
                                                                                     V2’First = 1
        X : Float := Max (V1);                                                       V2’Last = 100
        Y : Float := Max (V2);                                                       V2’Length = 100

                                                                                                   71
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
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”




                                                                                                      72
http://libre.act-europe.fr             © ACT Europe under the GNU Free Documentation License
• Programming with Ada 95
                             – Scalar data types
                             – Checks & Exceptions
                             – Access types (pointers)
                             – Arrays
                             – Records
                             – Parameter passing

                                                                                            73
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Record Types
                 type Date is record
                    Day : Positive range 1 .. 31;
                    Month : Positive range 1 .. 12;
                    Year : Integer;
                 end record;

                 D : Date := (3, 9, 1975);

                 A : Date := (Day => 31, Month => 12, Year => 1999);

                 B : Date := A;

                 Y : Integer := B . Year;
                                                                                           74
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
P2



   type Node;                                                                           3
   type Node_Ptr is access Node;                                                        9
                                                                                      1975
   type Node is record
      D    : Date := (1, 1, 1900);
      Next : Node_Ptr;
   end record;                                                                          1
                                                                          P1            1
   P1 : Node_Ptr := new Node;                                                         1900
                                                                                      null



  P2 : Node_Ptr := new Node ’ ((3, 9, 1975), P1);                                    Memory
                                                                                              75
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
N : Node := ((31, 12, 1999), null);

      P3 : Node_Ptr := new Node ’ (N);
                                                                                       31
                                                                          P3           12
                                                                                      1999
                                                                                      null



                                                                                     Memory
                                                                                              76
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
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


                                                                                                               77
http://libre.act-europe.fr             © ACT Europe under the GNU Free Documentation License
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;




                                                                                           78
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
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;

                                                                                           79
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
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




                                                                                           80
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
X : Queue :=(Max_Size => 4,
                        First => 2,
                        Last => 3,
                        Size => 2,
                        Q => (0, 11, 22, 0));


           X : Queue;        X.Max_Size = ???


                                                             Compilation error
                                                                Queue is an
                                                            unconstrained type
                                                                must specify
                                                             discriminant value
                                                                                         81
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
• Programming with Ada 95
                             – Scalar data types
                             – Checks & Exceptions
                             – Access types (pointers)
                             – Arrays
                             – Records
                             – Parameter passing

                                                                                            82
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Parameter Passing


           • in               (functions & procedures)

           • in out                                   (procedures only)

           • out                                      (procedures only)




                                                                                           83
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
in parameters are
                     copied IN during a subprogram call

      function Inc (X : Integer) return Integer is
      begin
         X := X + 1;
         return X;
                                            Compilation error
      end Inc;
                                               X is like a
                                                constant
                                                inside Inc



                                                                                       84
http://libre.act-europe.fr     © ACT Europe under the GNU Free Documentation License
in out parameters are
                     copied IN during a subprogram call
                        and copied OUT upon return


             procedure Inc (X : in out Integer) is
             begin
                X := X + 1;                        X is a regular
             end Inc;                                 variable
                                                     inside Inc
             Val : Integer := 3;

             Inc (Val);
             -- here Val = 4
                                                                                           85
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
out parameters are
                             copied OUT upon return



             procedure Random (X : out Integer) is
             begin
                -- compute random number          X is a regular
                X := …;                        variable inside Inc
             end Random;                      without initial value

             Val : Integer;
             Random (Val);
                                                                                          86
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
Control Structures

           • if-then-else
           • case statements
           • loops
                  – for
                  – while
                  – generic
                                                                                         87
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License

More Related Content

Viewers also liked

Työelämän tarpeet ja kompetenssit kahdesta näkövinkkelistä - Satu Ågren & Ink...
Työelämän tarpeet ja kompetenssit kahdesta näkövinkkelistä - Satu Ågren & Ink...Työelämän tarpeet ja kompetenssit kahdesta näkövinkkelistä - Satu Ågren & Ink...
Työelämän tarpeet ja kompetenssit kahdesta näkövinkkelistä - Satu Ågren & Ink...Otavan Opisto
 
Tulevaisuuden peruskoulu
Tulevaisuuden peruskouluTulevaisuuden peruskoulu
Tulevaisuuden peruskouluSDP
 
Hyvä ja turvallinen koulu kaikille
Hyvä ja turvallinen koulu kaikilleHyvä ja turvallinen koulu kaikille
Hyvä ja turvallinen koulu kaikilleSDP
 
Vincit V-kayrä
Vincit V-kayrä Vincit V-kayrä
Vincit V-kayrä Celkee Oy
 
Opettajan ja opiskelijan rooli ja tehtävät verkko-opinnoissa
Opettajan ja opiskelijan rooli ja tehtävät verkko-opinnoissaOpettajan ja opiskelijan rooli ja tehtävät verkko-opinnoissa
Opettajan ja opiskelijan rooli ja tehtävät verkko-opinnoissaKirsiViitanen
 
Tutkimus opettajien odotuksista ja asenteista: Sähköiset oppimateriaalit osan...
Tutkimus opettajien odotuksista ja asenteista: Sähköiset oppimateriaalit osan...Tutkimus opettajien odotuksista ja asenteista: Sähköiset oppimateriaalit osan...
Tutkimus opettajien odotuksista ja asenteista: Sähköiset oppimateriaalit osan...Sanoma Pro
 
Työhyvinvointi ja sen kehittäminen julkisella alalla ja erityisesti opetusala...
Työhyvinvointi ja sen kehittäminen julkisella alalla ja erityisesti opetusala...Työhyvinvointi ja sen kehittäminen julkisella alalla ja erityisesti opetusala...
Työhyvinvointi ja sen kehittäminen julkisella alalla ja erityisesti opetusala...Pauli Forma
 
Pauli Forman esitys julkisen alan työhyvinvoinnista JUKO/OAJ:n tilaisuudessa ...
Pauli Forman esitys julkisen alan työhyvinvoinnista JUKO/OAJ:n tilaisuudessa ...Pauli Forman esitys julkisen alan työhyvinvoinnista JUKO/OAJ:n tilaisuudessa ...
Pauli Forman esitys julkisen alan työhyvinvoinnista JUKO/OAJ:n tilaisuudessa ...Pauli Forma
 
Emootiot ja vuorovaikutus
Emootiot ja vuorovaikutusEmootiot ja vuorovaikutus
Emootiot ja vuorovaikutusAnne Rongas
 
Opettajan muuttuva identiteetti
Opettajan muuttuva identiteettiOpettajan muuttuva identiteetti
Opettajan muuttuva identiteettiMiikka Salavuo
 
Hietanen-Peltola: Kouluympäristön terveellisyys ja turvallisuus sekä kouluyht...
Hietanen-Peltola: Kouluympäristön terveellisyys ja turvallisuus sekä kouluyht...Hietanen-Peltola: Kouluympäristön terveellisyys ja turvallisuus sekä kouluyht...
Hietanen-Peltola: Kouluympäristön terveellisyys ja turvallisuus sekä kouluyht...Kouluterveyskysely
 
Suuntana tulevaisuus- Yhteisöllinen koulu ja sosiaalinen media elinikäisten o...
Suuntana tulevaisuus- Yhteisöllinen koulu ja sosiaalinen media elinikäisten o...Suuntana tulevaisuus- Yhteisöllinen koulu ja sosiaalinen media elinikäisten o...
Suuntana tulevaisuus- Yhteisöllinen koulu ja sosiaalinen media elinikäisten o...Miikka Salavuo
 
ope valmentajana opas_2016
ope valmentajana opas_2016ope valmentajana opas_2016
ope valmentajana opas_2016Kristina Ström
 

Viewers also liked (17)

Työelämän tarpeet ja kompetenssit kahdesta näkövinkkelistä - Satu Ågren & Ink...
Työelämän tarpeet ja kompetenssit kahdesta näkövinkkelistä - Satu Ågren & Ink...Työelämän tarpeet ja kompetenssit kahdesta näkövinkkelistä - Satu Ågren & Ink...
Työelämän tarpeet ja kompetenssit kahdesta näkövinkkelistä - Satu Ågren & Ink...
 
Tulevaisuuden peruskoulu
Tulevaisuuden peruskouluTulevaisuuden peruskoulu
Tulevaisuuden peruskoulu
 
Hyvä ja turvallinen koulu kaikille
Hyvä ja turvallinen koulu kaikilleHyvä ja turvallinen koulu kaikille
Hyvä ja turvallinen koulu kaikille
 
Vincit V-kayrä
Vincit V-kayrä Vincit V-kayrä
Vincit V-kayrä
 
STTK 30.9.2016
STTK 30.9.2016STTK 30.9.2016
STTK 30.9.2016
 
Opettajan ja opiskelijan rooli ja tehtävät verkko-opinnoissa
Opettajan ja opiskelijan rooli ja tehtävät verkko-opinnoissaOpettajan ja opiskelijan rooli ja tehtävät verkko-opinnoissa
Opettajan ja opiskelijan rooli ja tehtävät verkko-opinnoissa
 
Tutkimus opettajien odotuksista ja asenteista: Sähköiset oppimateriaalit osan...
Tutkimus opettajien odotuksista ja asenteista: Sähköiset oppimateriaalit osan...Tutkimus opettajien odotuksista ja asenteista: Sähköiset oppimateriaalit osan...
Tutkimus opettajien odotuksista ja asenteista: Sähköiset oppimateriaalit osan...
 
Työhyvinvointi ja sen kehittäminen julkisella alalla ja erityisesti opetusala...
Työhyvinvointi ja sen kehittäminen julkisella alalla ja erityisesti opetusala...Työhyvinvointi ja sen kehittäminen julkisella alalla ja erityisesti opetusala...
Työhyvinvointi ja sen kehittäminen julkisella alalla ja erityisesti opetusala...
 
Pauli Forman esitys julkisen alan työhyvinvoinnista JUKO/OAJ:n tilaisuudessa ...
Pauli Forman esitys julkisen alan työhyvinvoinnista JUKO/OAJ:n tilaisuudessa ...Pauli Forman esitys julkisen alan työhyvinvoinnista JUKO/OAJ:n tilaisuudessa ...
Pauli Forman esitys julkisen alan työhyvinvoinnista JUKO/OAJ:n tilaisuudessa ...
 
Emootiot ja vuorovaikutus
Emootiot ja vuorovaikutusEmootiot ja vuorovaikutus
Emootiot ja vuorovaikutus
 
Opettajan tehtävät
Opettajan tehtävätOpettajan tehtävät
Opettajan tehtävät
 
Opettajien yhteisollinen asiantuntijuus_niina_impio
Opettajien yhteisollinen asiantuntijuus_niina_impioOpettajien yhteisollinen asiantuntijuus_niina_impio
Opettajien yhteisollinen asiantuntijuus_niina_impio
 
Opettajan muuttuva identiteetti
Opettajan muuttuva identiteettiOpettajan muuttuva identiteetti
Opettajan muuttuva identiteetti
 
Hietanen-Peltola: Kouluympäristön terveellisyys ja turvallisuus sekä kouluyht...
Hietanen-Peltola: Kouluympäristön terveellisyys ja turvallisuus sekä kouluyht...Hietanen-Peltola: Kouluympäristön terveellisyys ja turvallisuus sekä kouluyht...
Hietanen-Peltola: Kouluympäristön terveellisyys ja turvallisuus sekä kouluyht...
 
Suuntana tulevaisuus- Yhteisöllinen koulu ja sosiaalinen media elinikäisten o...
Suuntana tulevaisuus- Yhteisöllinen koulu ja sosiaalinen media elinikäisten o...Suuntana tulevaisuus- Yhteisöllinen koulu ja sosiaalinen media elinikäisten o...
Suuntana tulevaisuus- Yhteisöllinen koulu ja sosiaalinen media elinikäisten o...
 
Kognitiivinen kuormittuminen ja aivojen hyvinvointi työssä
Kognitiivinen kuormittuminen ja aivojen hyvinvointi työssäKognitiivinen kuormittuminen ja aivojen hyvinvointi työssä
Kognitiivinen kuormittuminen ja aivojen hyvinvointi työssä
 
ope valmentajana opas_2016
ope valmentajana opas_2016ope valmentajana opas_2016
ope valmentajana opas_2016
 

Similar to Ada 95 - Introduction

Olpc France Presentation Sl2008
Olpc France Presentation Sl2008Olpc France Presentation Sl2008
Olpc France Presentation Sl2008olberger
 
Jay Phillips's Presentation at eComm 2009
Jay Phillips's Presentation at eComm 2009Jay Phillips's Presentation at eComm 2009
Jay Phillips's Presentation at eComm 2009eCommConf
 
"The OpenCV Open Source Computer Vision Library: What’s New and What’s Coming...
"The OpenCV Open Source Computer Vision Library: What’s New and What’s Coming..."The OpenCV Open Source Computer Vision Library: What’s New and What’s Coming...
"The OpenCV Open Source Computer Vision Library: What’s New and What’s Coming...Edge AI and Vision Alliance
 
3 Vampir Trace In Detail
3 Vampir Trace In Detail3 Vampir Trace In Detail
3 Vampir Trace In DetailPTIHPA
 
Web Application Frewall
Web Application FrewallWeb Application Frewall
Web Application FrewallAbhishek Singh
 
Symbian Based Mobile Software Development
Symbian Based Mobile Software DevelopmentSymbian Based Mobile Software Development
Symbian Based Mobile Software Developmenttoteb5
 
"Building Complete Embedded Vision Systems on Linux—From Camera to Display," ...
"Building Complete Embedded Vision Systems on Linux—From Camera to Display," ..."Building Complete Embedded Vision Systems on Linux—From Camera to Display," ...
"Building Complete Embedded Vision Systems on Linux—From Camera to Display," ...Edge AI and Vision Alliance
 
Hack.lu 09 ip-morph
Hack.lu 09 ip-morphHack.lu 09 ip-morph
Hack.lu 09 ip-morphSteph Cliche
 

Similar to Ada 95 - Introduction (20)

Developing Software that Matters II
Developing Software that Matters IIDeveloping Software that Matters II
Developing Software that Matters II
 
Developing Software That Matters I
Developing Software That Matters IDeveloping Software That Matters I
Developing Software That Matters I
 
Olpc France Presentation Sl2008
Olpc France Presentation Sl2008Olpc France Presentation Sl2008
Olpc France Presentation Sl2008
 
Ada 95 - Programming in the large
Ada 95 - Programming in the largeAda 95 - Programming in the large
Ada 95 - Programming in the large
 
Ada 95 - Structured programming
Ada 95 - Structured programmingAda 95 - Structured programming
Ada 95 - Structured programming
 
Jay Phillips's Presentation at eComm 2009
Jay Phillips's Presentation at eComm 2009Jay Phillips's Presentation at eComm 2009
Jay Phillips's Presentation at eComm 2009
 
Amoocon May 2009 Germany
Amoocon May 2009   GermanyAmoocon May 2009   Germany
Amoocon May 2009 Germany
 
OSS Legal issues method
OSS Legal issues methodOSS Legal issues method
OSS Legal issues method
 
Airbus and open source for fossa 2010
Airbus and open source for fossa 2010Airbus and open source for fossa 2010
Airbus and open source for fossa 2010
 
8051 zigbee interface
8051 zigbee interface8051 zigbee interface
8051 zigbee interface
 
"The OpenCV Open Source Computer Vision Library: What’s New and What’s Coming...
"The OpenCV Open Source Computer Vision Library: What’s New and What’s Coming..."The OpenCV Open Source Computer Vision Library: What’s New and What’s Coming...
"The OpenCV Open Source Computer Vision Library: What’s New and What’s Coming...
 
3 Vampir Trace In Detail
3 Vampir Trace In Detail3 Vampir Trace In Detail
3 Vampir Trace In Detail
 
Web Application Frewall
Web Application FrewallWeb Application Frewall
Web Application Frewall
 
Locust Fear
Locust FearLocust Fear
Locust Fear
 
CRC MMB Projects and Tools
CRC MMB Projects and ToolsCRC MMB Projects and Tools
CRC MMB Projects and Tools
 
Symbian Based Mobile Software Development
Symbian Based Mobile Software DevelopmentSymbian Based Mobile Software Development
Symbian Based Mobile Software Development
 
"Building Complete Embedded Vision Systems on Linux—From Camera to Display," ...
"Building Complete Embedded Vision Systems on Linux—From Camera to Display," ..."Building Complete Embedded Vision Systems on Linux—From Camera to Display," ...
"Building Complete Embedded Vision Systems on Linux—From Camera to Display," ...
 
Hack.lu 09 ip-morph
Hack.lu 09 ip-morphHack.lu 09 ip-morph
Hack.lu 09 ip-morph
 
Open Source In Education
Open Source In EducationOpen Source In Education
Open Source In Education
 
Large organisation airbus and open source - fossa2010
Large organisation   airbus and open source - fossa2010Large organisation   airbus and open source - fossa2010
Large organisation airbus and open source - fossa2010
 

More from Gneuromante canalada.org

More from Gneuromante canalada.org (8)

Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs
Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada ProgramsAst2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs
Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs
 
SIGAda Hibachi Workshop Presentation
SIGAda Hibachi Workshop PresentationSIGAda Hibachi Workshop Presentation
SIGAda Hibachi Workshop Presentation
 
Developing Software that Matters (condensed)
Developing Software that Matters (condensed)Developing Software that Matters (condensed)
Developing Software that Matters (condensed)
 
Ada at Barco avionics
Ada at Barco avionicsAda at Barco avionics
Ada at Barco avionics
 
Programming Languages and Software Construction
Programming Languages and Software ConstructionProgramming Languages and Software Construction
Programming Languages and Software Construction
 
Ada 95 - Distributed systems
Ada 95 - Distributed systemsAda 95 - Distributed systems
Ada 95 - Distributed systems
 
Ada 95 - Object orientation
Ada 95 - Object orientationAda 95 - Object orientation
Ada 95 - Object orientation
 
Ada in Debian GNU/Linux
Ada in Debian GNU/LinuxAda in Debian GNU/Linux
Ada in Debian GNU/Linux
 

Recently uploaded

Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 

Recently uploaded (20)

Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 

Ada 95 - Introduction

  • 1. Franco Gasperoni gasperon@act-europe.fr http://libre.act-europe.fr 1 http://libre.act-europe.fr © 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 http://libre.act-europe.fr/ is kept at the bottom of every non-title slide. A copy of the license is available at: • http://www.fsf.org/licenses/fdl.html 2 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 3. 3 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 4. http://www-inf.enst.fr/~domdl • Programming in Ada 95 by John Barnes (Addison Wesley) • Lovelace On line Ada 95 tutorial 4 http://libre.act-europe.fr © 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 5 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 6. Lectures •• Introduction to Ada 95 Introduction to Ada 95 •• Building reliable SW systems Building reliable SW systems 3+3 h •• Structured design Structured design •• TP 1 TP 1 •• OO design OO design •• Java Java 4.5+3 h •• TP 2 TP 2 •• Distributed programming Distributed programming •• TP 3 TP 3 3+3 h 6 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 7. Course Evaluation • TP OO: 6 points • TP distributed programming: 4 points 7 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 8. • Background on Ada 95 • Programming with Ada 95 8 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 9. 1950 1960 1970 1980 1990 2000 PL/I(66) Ada(95) Ada(83) Cobol(58) A S S E M B L Y Pascal(70) Java(96) Algol(60) C(72) C++(89) Fortran(54) Simula(67) Smalltalk(80) imperative Basic(66) 9 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 10. Software Crisis: 1976 • Many languages (>450) – Many dialects • Low interoperability • High maintenance costs • Low reliability 10 http://libre.act-europe.fr © 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 11 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 12. Safety Critical Is 99.9% acceptable for defects? • Drinking water contaminated for 1 h/month • 2 accidents/month at O’Hare International Airport • 22,000 checks drawn from the wrong account / hour 12 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 13. Ada • 1975 study: No language was adequate for safety critical applications • 1976-80: 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 13 http://libre.act-europe.fr © 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! 14 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 15. Average Annual Costs for Software Maintenance 1800 1600 1000s of 1994 Dollars Ada 270,000 LOC 1400 Other HOLs 1200 C 225,000 LOC 1000 135,000 LOC 800 150,000 LOC 600 112,500 LOC 400 75,000 LOC 200 0 350 700 1,050 1,400 1,750 2,100 Function Points Source: MITRE (Avionics domain) 15 http://libre.act-europe.fr © 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 16 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 17. • Background on Ada 95 • Programming with Ada 95 17 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 18. The C programming style: Conventions & NO Verification void main () void main () void main () void main () {{ {{ printf (“Hello world.n”); printf (12345); printf (12345); printf (“Hello world.n”); }} }} 18 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 19. Even with ... #include <stdio.h> #include <stdio.h> void main () void main () {{ printf (“Hello world in Cn”); printf (“Hello world in Cn”); }} • NO guarantee you will link with – the correct library – the good version of the correct library 19 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 20. hello.adb hello.adb with Text_IO; with Text_IO; procedure Hello is procedure Hello is begin begin Text_IO.Put_Line (“Hello world in Ada”); Text_IO.Put_Line (“Hello world in Ada”); end Hello; end Hello; hello.c hello.c #include <stdio.h> #include <stdio.h> void main () void main () {{ printf (“Hello world in Cn”); printf (“Hello world in Cn”); }} 20 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 21. Predefined Ada library hello.adb hello.adb subprogram in library with Text_IO; with Text_IO; procedure Hello is procedure Hello is begin begin Text_IO.Put_Line (“Hello world in Ada”); Text_IO.Put_Line (“Hello world in Ada”); end Hello; end Hello; % gnatmake -q hello % gnatmake -q hello % hello % hello Hello world in Ada Hello world in Ada Checks you are using % % the correct version of every module & library 21 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 22. hello.adb hello.adb with Text_IO; use Text_IO; with Text_IO; use Text_IO; procedure Hello is procedure Hello is begin begin Text_IO.Put_Line (“Hello world in Ada”); Text_IO.Put_Line (“Hello world in Ada”); end Hello; end Hello; 22 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 23. hello.adb hello.adb with Text_IO; with Text_IO; procedure Hello is procedure Hello is begin begin Text_IO.Put_Line (“Hello world in Ada”); Text_IO.Put_Line (“Hello world in Ada”); end Hello; end Hello; %gnatmake -q two_hello %gnatmake -q two_hello % two_hello % two_hello two_hello.adb two_hello.adb Hello world in Ada Hello world in Ada Hello world in Ada Hello world in Ada with Hello; with Hello; % % procedure Two_Hello is procedure Two_Hello is begin begin Hello; Hello; Hello; Hello; end Two_Hello; end Two_Hello;ACT Europe under the GNU Free Documentation License 23 http://libre.act-europe.fr ©
  • 24. with Text_IO; use Text_IO; with Text_IO; use Text_IO; function Fact (N ::Integer) return Integer is function Fact (N Integer) return Integer is procedure Hello is procedure Hello is begin begin begin begin if N <= 1 then if N <= 1 then Put_Line (“Hello”); Put_Line (“Hello”); return 1; return 1; end Hello; end Hello; else hello.adb else return N **Fact (N-1); return N Fact (N-1); end if; end if; end Fact; end Fact; fact.adb with Hello; with Hello; with Fact; with Fact; procedure Main is procedure Main is begin begin for IIin 1 .. Fact (4) loop for in 1 .. Fact (4) loop Hello; Hello; end loop; end loop; end Main; end Main; main.adb 24 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 25. General Structure Ada Library Ada Library with ... with ... with ... with ... with ... with ... procedure Some_Main is procedure Some_Main is begin begin …. …. end Some_Main; end Some_Main; 25 http://libre.act-europe.fr © 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 26 http://libre.act-europe.fr © 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) 27 http://libre.act-europe.fr © 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; 28 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 29. Introduces a new type function Compute (P, Q : Integer) return Integer is type My_Int is range -100 .. 1_000_000; T : My_Int; Compilation ERROR Ada is strongly typed begin T := P + 1; T := My_Int (P) + 1; Explicit conversion needed return Integer (T) + Q; end Compute; 29 http://libre.act-europe.fr © 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; 30 http://libre.act-europe.fr © 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; Explicit conversions needed A : Integer := Integer (R); begin null; end Compute; 31 http://libre.act-europe.fr © 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) 32 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 33. with Text_IO; procedure Print (A : Integer; P : Float) is type My_Int is range -100 .. 1_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; 33 http://libre.act-europe.fr © 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 34 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 35. Overflow in C #include <limits> void main { int k = INT_MAX; k = k + 1; } Semantics undefined 35 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 36. Potential problems ... 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) 36 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 37. Overflow & Constraint Checks procedure Checks is K : Integer := Integer ’ Last; begin exception K := K + 1; Constraint_Error end Checks; raised during execution % gnatmake -q checks % checks raised CONSTRAINT_ERROR % 37 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 38. procedure Checks (X : Integer) is type My_Int is range -100 .. 1_000_000; T : My_Int; Constraint_Error begin raised T := My_Int (X); if X not in end Checks; -100 .. -1_000_000 38 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 39. Subtypes type Day_Of_A_Month is range 1 .. 31; type Day_Of_February is range 1 .. 28; Compilation ERROR D1 : Day_Of_February := 25; Ada is strongly typed D2 : Day_Of_A_Month := D1; D2 : Day_Of_A_Month := Day_Of_A_Month (D1); OK, but tedious 39 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 40. type Day_Of_A_Month is range 1 .. 31; subtype Day_Of_February is Day_Of_A_Month range 1 .. 28; D1 : Day_Of_February := 25; D2 : Day_Of_A_Month := D1; OK Constraint_Error D3 : Day_Of_A_Month; raised if D3 not in 1 .. 28 D4 : Day_Of_February := D3; 40 http://libre.act-europe.fr © 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; 41 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 42. Exceptions procedure Checks is A : Integer := Integer ’ First; exception Constraint_Error begin raised A := A - 1; during execution end Checks; % gnatmake -q checks % checks raised CONSTRAINT_ERROR % 42 http://libre.act-europe.fr © 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) 43 http://libre.act-europe.fr © 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 % 44 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 45. procedure Checks is Internal_Error : exception; Creating procedure Foo is begin your own raise Internal_Error; exceptions end Foo; procedure Bar is begin Foo; end Bar; begin Bar; exception Exception Handler ... 45 http://libre.act-europe.fr end Checks; © 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”); Exception Handler when others => Put_Line (“some other exception”); end Checks; 46 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 47. procedure Foo is begin raise Internal_Error; 1 end Foo; procedure Bar is 2 begin Foo; 3 end Bar; begin 4 Bar; 5 exception when Internal_Error => Put_Line (“problem occurred”); Exception Handler when others => Put_Line (“some other exception”); end Checks; 47 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 48. procedure Foo is begin raise Internal_Error; 1 end Foo; procedure Bar is 2 begin Foo; 3 end Bar; begin 4 Bar; 5 exception when Internal_Error => Put_Line (“problem occurred”); when others => Put_Line (“some other exception”); end Checks; 48 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 49. Catching an exception where YOU want procedure Checks is ... begin } Want to catch some exception in a region of code without exiting from the subprogram 49 http://libre.act-europe.fr Checks; end © ACT Europe under the GNU Free Documentation License
  • 50. procedure Checks is ... Use begin declare Blocks Some_Label : declare Declarations begin Statements exception Handler end Some_Label; end Checks; 50 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 51. procedure Calc (A, B : Float) is C, D : Float; Example begin declare Old_C : Float := C; begin C := A * B; D := C ** 2; exception when Constraint_Error => C := Old_C; D := 0.0; end; 51 http://libre.act-europe.fr © 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 52 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 53. Access Types type Int_Ptr is access Integer; P : Int_Ptr; pointers pointers are initialized are initialized to null to null by default by default Memory 53 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 54. type Int_Ptr is access Integer; P : Int_Ptr; P P := new Integer; ??? Memory 54 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 55. type Int_Ptr is access Integer; P : Int_Ptr; P P := new Integer; 3334 P.all := 3344; Memory 55 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 56. type Int_Ptr is access Integer; P : Int_Ptr; P P := new Integer ’ (1234); 1234 Memory 56 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 57. type Int_Ptr is access Integer; P : Int_Ptr; P X : Integer := 567; 567 P := new Integer ’ (X); Memory 57 http://libre.act-europe.fr © 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; 567 Q : Another_Int_Ptr; COMPILATION ERROR Q := Another_Int_Ptr (P); you must use general access types to perform pointer conversions 58 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 59. General Access Types type Ptr_Int is access all Integer; P : Ptr_Int; Y : aliased Integer := 9999; 9999 Y Memory 59 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 60. P type Ptr_Int is access all Integer; P : Ptr_Int; Y : aliased Integer := 9999; 9999 P := Y ’ access; Y Memory 60 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 61. P type Ptr_Int is access all Integer; P : Ptr_Int; Y : aliased Integer := 9999; 1234 P := Y ’ access; Y P.all := 1234; Memory 61 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 62. type Ptr_Int is access all Integer; P : Ptr_Int; X : Integer; compilation error P := X ’ access; X is not aliased 62 http://libre.act-europe.fr © 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 63 http://libre.act-europe.fr © 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 64 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 65. Composite Ada types • array (String) • record • tagged record • protected types • tasks 65 http://libre.act-europe.fr © 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; Arrays can have - dynamic bounds begin - dynamic size for I in 1 .. N loop A (I) := 3.141; end loop; end Compute; 66 http://libre.act-europe.fr © 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 takes its bounds from A B : Arr := A; C : Arr (11 .. 20); begin Constraint_Error C := A if C’Length /= A’Length C (15 .. 18) := A (5 .. 8); Constraint_Error if A’Last < 8 67 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 68. A : array (10 .. 20) of Float; B : array (10 .. 20) of Float; A := B; Compilation error A and B are one of a kind 68 http://libre.act-europe.fr © 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 69 http://libre.act-europe.fr © 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; 70 http://libre.act-europe.fr © 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); V1’First = 0 V1’Last = 6 V1’Length = 7 V2 : Vector (1 .. 100) := (1.0, 2.0, others => 99.0); V2’First = 1 X : Float := Max (V1); V2’Last = 100 Y : Float := Max (V2); V2’Length = 100 71 http://libre.act-europe.fr © 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” 72 http://libre.act-europe.fr © 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 73 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 74. Record Types type Date is record Day : Positive range 1 .. 31; Month : Positive range 1 .. 12; Year : Integer; end record; D : Date := (3, 9, 1975); A : Date := (Day => 31, Month => 12, Year => 1999); B : Date := A; Y : Integer := B . Year; 74 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 75. P2 type Node; 3 type Node_Ptr is access Node; 9 1975 type Node is record D : Date := (1, 1, 1900); Next : Node_Ptr; end record; 1 P1 1 P1 : Node_Ptr := new Node; 1900 null P2 : Node_Ptr := new Node ’ ((3, 9, 1975), P1); Memory 75 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 76. N : Node := ((31, 12, 1999), null); P3 : Node_Ptr := new Node ’ (N); 31 P3 12 1999 null Memory 76 http://libre.act-europe.fr © 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 77 http://libre.act-europe.fr © 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; 78 http://libre.act-europe.fr © 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; 79 http://libre.act-europe.fr © 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 80 http://libre.act-europe.fr © 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; X.Max_Size = ??? Compilation error Queue is an unconstrained type must specify discriminant value 81 http://libre.act-europe.fr © 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 82 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 83. Parameter Passing • in (functions & procedures) • in out (procedures only) • out (procedures only) 83 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 84. in parameters are copied IN during a subprogram call function Inc (X : Integer) return Integer is begin X := X + 1; return X; Compilation error end Inc; X is like a constant inside Inc 84 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 85. in out parameters are copied IN during a subprogram call and copied OUT upon return procedure Inc (X : in out Integer) is begin X := X + 1; X is a regular end Inc; variable inside Inc Val : Integer := 3; Inc (Val); -- here Val = 4 85 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 86. out parameters are copied OUT upon return procedure Random (X : out Integer) is begin -- compute random number X is a regular X := …; variable inside Inc end Random; without initial value Val : Integer; Random (Val); 86 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 87. Control Structures • if-then-else • case statements • loops – for – while – generic 87 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License