Upload
gneuromante-canaladaorg
View
2.985
Download
4
Embed Size (px)
DESCRIPTION
Presentation under the GPL license by Ludovic Brenta: "I originally wrote these slides for a 40-minute "introduction to Ada" that I gave at the Libre Software Meeting in Bordeaux in 2004. I made sure these slides were suitable for reuse. Their purpose is not to teach Ada; this is impossible to do in just 40 minutes. Instead, they try to give an overview of the main features of Ada and provide pointers to further information."
Citation preview
Introduction to Ada slides suitable for a 40minute presentationCopyright (C) 2004 Ludovic Brenta <[email protected]>
This presentation is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public License aspublished by the Free Software Foundation; either version 2 of theLicense, or (at your option) any later version.
This presentation is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.
You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place, Suite 330, Boston, MA 021111307USA
I originally wrote these slides for a 40minute "introduction to Ada"that I gave at the Libre Software Meeting in Bordeaux in 2004. I madesure these slides were suitable for reuse.
Their purpose is not to teach Ada; this is impossible to do in just 40minutes. Instead, they try to give an overview of the main featuresof Ada and provide pointers to further information.
Prologue: Lady Ada Lovelace
Ada Augusta Byron, countess of Lovelace (18151852)
Poet Lord Byron's daughter
A Mathematician
Worked with Charles Babbage on the Difference Engine
First “computer scientist” in history
Invented the first program, and programming language, for Babbage's Difference Engine
Historique
Lisp (1958) was created by and for MIT professors
C (1973) was created by and for kernel hackers
Ada was created by and for industrial engineers1980 : first military standard, MILSTD1815
● All compilers must be validated against the standard● No dialects allowed
1983 : first civil US standard (ANSI)
1987 : first international standard, ISO 8652
1995 : Ada 95 is the first standard objectoriented language
2005 : Ada 2005 is in the works
Increased traffic on comp.lang.ada et al since 1995
Who uses Ada in 2004?
Space and aeronauticsEurofighter: 2 million lines of Ada code
Boeing (in progress: 7E7)
Airbus (in progress: A380, A400M)
Ariane
Satellites
Eurocontrol (air traffic control)
Their subcontractors: Barco Avionics, Thales, BAe Systems, Smiths Aerospace, Raytheon, etc.
Qui utilise Ada en 2004?
Rail transport industryFrench highspeed train (TGV)
Metro lines in New York, Paris (line 14), Delhi, Calcutta, etc.
Nuclear industryElectricité de France (EDF): emergency reactor shutdown
OtherFinancial: BNP (France), Paranor (Switzerland), PostFinance
Healthcare and medical : JEOL (USA), ReadySoft (France)
Automotive : BMW (Allemagne)
Communications : Canal+ (France)
And lots of free software!
In industryWhen human life is at stake
When software just has to work (no excuses accepted)
When you want to know why your software works (certification)
Free softwareTo develop twice as fast as in C
To produce 90% fewer bugs than in C
To be close to the application domain, and protected against machine details (highlevel programming)
For software to be portable
For software to be maintainable
Why Ada?
Philosophy (1)Cost of software development
Design
Implementation
Debugging
Hence the bright idea: bugs are the problem
Philosophy (2)
Implementation
Compilation Testing After delivery0
100
200
300
400
500
600
700
800
900
1000
Effort required to correct a bug
Hence the bright idea: find bugs as early as possible!
Obvious? Not at all!
Goals of the language
Reduce development and maintenance costsPrevent writing bugs if possible
Detect bugs as early as possible, preferably during compilation
Encourage code reuse and team work● Packages with separate interface and implementation● Generics (a.k.a. templates)
Make maintenance easier● Legibility is paramount; language is “autodocumented”
Work well in all situationsIn the small: embedded, limited resources, real time, no operating system
In the large: millions of lines of code, networking, GUIs, etc.
The type system
Ada is powerfully and statically typedDetects most bugs at compile time, before ever testing the program!
The main strength of the language
Programmers can define new typesTo reflect the application domain
● Number_Of_Apples, Number_Of_Oranges; not just Integer
To document the constraints of the problem● To not compare apples to oranges● The compiler enforces all constraints
Scalar typespackage Apples_And_Oranges is type Number_Of_Apples is range 1 .. 20; integer type Number_Of_Oranges is range 1 .. 40; integer type Mass is digits 4 range 0.0 .. 4000.0; real type Colour is (Red, Green, Blue); enumerationend Apples_And_Oranges;
Scalar types have attributes:T'First, T'Last : constants
T'Range = T'First .. T'Last (range of permitted values)
T'Pred(X), T'Succ (X) : functions that return the previous or next value
T'Image (X) : a string representation of X
etc.
Arrayspackage Arrays is type Colour is (Red, Green, Blue); type Value is range 0 .. 255; type Point is array (Colour) of Value; The index is an enumerated type
type Width is range 0 .. 640; type Height is range 0 .. 480; type Screen is array (Width, Height) of Point; The indexes are rangesend Arrays;
with Arrays;procedure Arrays_Trial is My_Screen : Arrays.Screen;begin My_Screen (4, 5) := (Arrays.Red => 42, Arrays.Green => 0, Arrays.Blue => 35);end Arrays_Trial;
Arrays have attributes
For any array A:A'Range is the range of the index
● A1 : array (4 .. 8) of Integer;● A1'Range = 4 .. 8;● A2 : array (Colour) of Integer;● A2'Range = Red .. Blue;
A'Range (N) is the range of the Nth dimension
A'Length and A'Length (N) yield the number of elements
etc.
Recordswith Ada.Calendar;package Records is type Gender is (Male, Female);
type Person (Sex : Gender) is record First_Name, Last_Name : String (1 .. 100); case Sex is when Male => null; when Frmale => Maiden_Name : String (1 .. 100); end case; end record;end Records;
The record above has an optional discriminant
Pointers (access types)
with Records; use Records;procedure Pointers is type Pointer_To_Element; type List_Element is record P : Person; Next : Pointer_To_Element; end record; type Pointer_To_Element is access List_Element;
List_Head : Pointer_To_Element := new List_Element;begin List_Head.Next := new List_Element;end Pointers;
Access types are incompatible with one anotherImpossible to mix up different pointers (safety)
The compiler guarantees absence of dangling pointers
Pointers are used only when necessaryDynamic data structures
OO programming and dynamic binding
Objectoriented programmingwith Ada.Calendar;package Objects is type Gender is (Male, Female);
type Person (Sex : Gender) is tagged record First_Name, Last_Name : String (1 .. 100); case Sex is when Male => null; when Female => Maiden_Name : String (1 .. 100); end case; end record;
type Diploma is record Received : Ada.Calendar.Time; Institution : String (1 .. 100); Title : String (1 .. 200); end record;
type Set_Of_Diplomas is array (Positive range <>) of Diplome;
type Educated_Person (Sex : Gender; Number_Of_Diplomas : Positive) is new Person (Sex) with record Diplomas : Set_Of_Diplomes (1 .. Nombre_Of_Diplomas); end record;end Objects;
Conclusion about typesAda allows construction of very complex types
Types describe the “universe”: this is called modellingHigh level of abstraction
Explicit constraints
Autodocumentation
Types and objects have attributesArray dimensions are always known
Useful for loops and branches (if, case)
Pointers are much safer than in C or C++
The compiler enforces and checks all constraintsStatically whenever possible
At run time whenever necessary (using exceptions)
SubprogramsProcedures and functions as in Pascal, except:
Parameters are “in”, “out” et “in out”The programmer says what he wants to do, not how to do it
The compiler chooses between byreference and byvalue
Possible to pass large objects (arrays, records) without explit pointers
Overloaded operators
Default parameter values like in C++
FunctionsAlso overloaded by their return value
Return value cannot be ignored (no ambiguity = safety)
All parameters are “in”
Subprograms : example
package Subprograms is function A return Boolean; function A return Integer; procedure A;
type Private_Type is private; an abstract data type
Null_Object : constant Private_Type;
function "+" (Left, Right : in Private_Type) return Private_Type; operator
procedure A (T1 : in Private_Type := Null_Object; default parameter value T2 : in out Private_Type);
procedure A (T : out Private_Type); constructorprivate type Private_Type is record Value : Integer; end record;
Null_Object : constant Private_Type := (Value => 0);end Subprograms;
Subprograms in OOPA method is a subprogram that:
accepts one or more parameters of a tagged type T, or access to T
is declared in the same package as T
procedure Foo (Object : in T); method of type T
No particular syntax; “this” parameter is explicit
Selection of methods (binding) may be:static: decided at compile time
dynamic: decided at run time (as with C++ virtual methods)
For each tagged type T, there is a type T'Class that encompasses T and its descendants
procedure Foo (Object : in T'Class); not a method
allows forcing static binding
Like “static methods” in C++
Calling subprograms
with Subprograms;procedure Use_Subprograms is Object1, Object2 : Subprograms.Private_Type; B : Boolean := Subprograms.A; I : Integer := Subprograms.A; use Subprograms;begin A (Object1); constructor Object2 := Object1 + Null_Object; operator "+" A (T1 => Object1, T2 => Object2);end Use_Subprograms;
Control structures Loopsprocedure Control_Structures is type Colour is (Red, Green, Blue); I : Natural := 0;
function Foo (I : in Natural) return Colour is separate;
Col : Colour := Foo (I);begin for C in Colour loop I := I + 1; end loop;
while I > 1 loop I := I 1; end loop;
Named_Loop : loop I := I + 1;
exit Named_Loop when I = 1000; I := I + 2; end loop Named_Loop;end Control_Structures;
Branchesseparate (Control_Structures)function Foo (I : in Natural) return Colour is Result : Colour;begin if I in 1 .. 10 then Result := Red; elsif I in 11 .. 20 then Result := Green; elsif I in 21 .. 30 then Result := Blue; end if;
case I is when 1 .. 10 => Result := Red; when 11 .. 20 => Result := Green; when 21 .. 30 => Result := Blue; when others => Result := Red; all cases must be processed end case;
return Result;end Foo;
Exceptions
with Ada.Text_IO; use Ada.Text_IO;procedure Exceptions_Example (I : in Natural) is Error : exception; type Colour is (Red, Green, Blue); Result : Colour;begin case I is when 1 .. 10 => Result := Red; when 11 .. 20 => Result := Green; when 21 .. 30 => Result := Blue; when others => raise Error; end case;exception when Error => Put_Line (“Error : I does not represent a colour”);end Exceptions_Example;
Générics (1)
More powerful than C++'s templates
A generic can be a procedure, a function or a package
Generic parameters can betypes
variables
procedures
functions
packages
Generics can impose constraints on accepted parameters
Générics (2)
generic type Item_Type is private; Item_Type may be any nonlimited typepackage JE.Stacks is type Stack_Type is limited private;
procedure Push (Stack : in out Stack_Type; Item : in Item_Type); procedure Pop (Stack : in out Stack_Type; Item : out Item_Type); function Top (Stack : Stack_Type) return Item_Type; function Size (Stack : Stack_Type) return Natural; function Empty (Stack : Stack_Type) return Boolean;
Stack_Overflow, Stack_Underflow : exception;
private to be dealt with laterend JE.Stacks;
Tasking (1)
with Ada.Text_IO; use Ada.Text_IO;procedure Tasking_Example (Nombre_De_Taches : in Natural) is task type Background_Task;
task body Background_Task is begin delay 1.0; 1 second Put_Line (“I am in a background task”); end Background_Task;
type Task_Index is range 1 .. 10;
The_Tasks : array (Task_Index) of Background_Task;begin Put_Line (“The main task is starting”); Put_Line (“The main task is waiting for all background tasks to complete”);end Tasking_Example;
Part of the language; not in a library
Keywords task, protected, accept, entry, abort, etc.
Tasking (2) : protected objects
functions are reentrant: concurrent calls allowed
procedures and entries are not reentrant: a single call at a time
Entries have guards allowing to synchronise calls
protected type Shared_Stack_Type is procedure Push (Item : in Integer); entry Pop (Item : out Integer); function Top return Integer; function Size return Natural; function Empty return Boolean;private package Int_Stacks is new JE.Stacks (Integer); Stack : Int_Stacks.Stack_Type;end Shared_Stack_Type;
Tasking (3): guardsprotected body Shared_Stack_Type is procedure Push (Item : in Integer) is begin Int_Stacks.Push (Stack,Item); end Push;
entry Pop (Item : out Integer) when not Int_Stacks.Empty (Stack) is begin Int_Stacks.Pop (Stack,Item); end Pop;
function Top return Integer is begin return Int_Stacks.Top (Stack); end Top;
function Size return Natural is begin return Int_Stacks.Size (Stack); end Size;
function Empty return Boolean is begin return Int_Stacks.Empty (Stack); end Empty;end Shared_Stack_Type;
Other featuresRepresentation clauses
Allow safe, lowlevel programming
Separate compilation is part of the languageThe compiler guarantees consistency of the final executable
No need for a Makefile
Tasks can rendezvous with each other
Standard interfaces with C, COBOL, FORTRANAllows using any C library out there (e.g. GTK+)
Controlled objects (Initialize, Adjust, Finalize)
ASIS (Ada Semantic Interface Specification)high level API to the compiler
a program can inspect an Ada program parsed by the compiler
an ISO standard
Even more functionality
Specialised needs annexes in Ada 95All of them are implemented by GNAT
Annex C : Systems programming (interrupts, machine code)
Annex D : Realtime systems
Annex E : Distributed systems (remote subprogram call)
Annex F : Information system (decimal number representations)
Annex G : Numerics (complex numbers, bounded performance)
Annex H : Safety and Security (aids to certification)
More information?
Web sites :Ada Information Clearinghouse : http://www.adaic.com
ACT Europe Libre Software : http://libre.acteurope.fr
Ada France : http://www.adafrance.org
News groupscomp.lang.ada, fr.comp.lang.ada
Many free books and tutorials are on lineIn English and other languages, including French
Thanks to John English for the tasking examplesAda 95, The Craft of ObjectOriented Programming
http://www.it.bton.ac.uk/staff/je/adacraft