Ada Programming Language - PowerPoint PPT Presentation

1 / 74
About This Presentation
Title:

Ada Programming Language

Description:

Modules are independent of each other in debug, understand & maintain. ... 1978 Ironman. 1979 Steelman. 8. Information Hiding, Verification, concurrency ... – PowerPoint PPT presentation

Number of Views:1320
Avg rating:5.0/5.0
Slides: 75
Provided by: sabouribeh
Category:

less

Transcript and Presenter's Notes

Title: Ada Programming Language


1
Ada Programming Language
In the Name of God
Modularity And Data Abstraction
  • Chapter 7

2
The software crisis reliable programming
Software Crisis
  • 1970
  • software cost increase without bound
  • Dijkstra
  • difficulty of producing a program program
    length2

But this proportion must become linear
3
Parnass Principle
  • Control of complexity of a large program
  • Modularization
  • Modules are independent of each other in debug,
    understand maintain.
  • Parnas principle There should be one module
    for each difficult design decision in the
    program.
  • If the decision changed, the corresponding module
    will be changed.
  • Information Hiding

4
Abstract Data Types
  • Data structure representation A common
    design decision
  • stack implementation array or linked list
  • set implementation array of values or bit
    string
  • Any manipulation must be through procedures.
  • Users must do abstract operations on the DS.
  • Modules provides this abstract operations.
  • Abstract Data Types
  • Abstract operation push pop on the stack DS
  • Concrete operation pointer operations

5
Experimental Abstract Type Languages
  • 1973 Languages that support data types and
    modules.
  • Alphard, CLU, Mesa, Euclid, Modula
  • Many of them has the construct of a class, first
    included in Simula67.
  • These experiences were important for the
    development of Ada.

6
DoD Saw the Need for a New Language
  • 1970 the need for a new PL for military
    services in embedded (or mission critical)
    computer applications.
  • Embedded the computer is integrated with some
    larger system.
  • Nonembedded i.e. scientific data processing
    applications.
  • DoD spent a lot of money for embedded
    applications, but because of multiple PLs much of
    them has the portability reuse problem
  • HOLWG group was created.
  • Higher Order Language Working Group

7
A Series of specifications
  • HOWLG published a series of specifications, each
    more detailed specific than the previous
  • 1975 Strawman
  • 1975 Woodenman
  • 1976 Tinman
  • 1978 Ironman
  • 1979 Steelman

8
Information Hiding, Verification, concurrency
  • General requirements on the language design
  • Readability
  • Simplicity
  • More specific requirements
  • Module facility to support information hiding
  • Concurrent programming
  • Verification of program correctness
  • Concrete requirements
  • Character set
  • Commenting conventions

9
Several competing designs winner Ada
  • 26 existing languages was studied, none is usable
  • At first,16 proposals
  • The winner named Ada
  • Augusta Ada, A mathematician and Charles
    Babbages first programmer
  • A tradition of naming PLs after mathematicians
  • Become an ISO standard in 1987

10
No Subset or Superset
  • Portability purpose No subset or superset
  • Register the Ada name as trademark.
  • No subset superset can legally named Ada
  • How to understand which compiler implements the
    spec?
  • Validation Procedure, comprising over 2500 tests,
    attempts to ensure no more no less than the
    standard language

11
Ada Has Been Revised
  • 1983 1st version, Ada83
  • 1988 new version, Ada95
  • 1990 report 41 requirement 22 new topics.
  • 1995 resulting revision includes ideas from
    5th generation OO PLs.

12
Ada95
  • A language that specify all the specification was
    very detailed.
  • 1. core languages
  • 2. six special need annexes
  • must be implemented
  • Optional extensions for particular application
    areas (system programming, information systems,
    real-time systems, numerical programming,
    distributed systems, safety security)

Ada95
13
Design Structural Organization
  • Syntax quite similar to Pascals
  • Keywords in lowercase
  • Others mixed (lower upper)

Declarations
Expressions
Adas constructs
Statements
Types
14
Adas constructs
  • Expressions statements similar to Pascal
  • Types also similar but more flexible less
    problem
  • Declarations are very different

object
type
subprogram
package
task
15
  • package Tables is
  • type Table is array (Integer range lt gt ) of
    float
  • procedure BinSearch (T Table Sought Float
  • Location out Integer Found out Boolean) is
  • subtype Index is Integer range TFirst ..
    TLast
  • Lower Index TFirst
  • Upper Index TLast
  • Middle Index (TFirst TLast)/2
  • begin
  • loop
  • if T (Middle)Sought then
  • locationmiddle
  • Foundtrue
  • return
  • elsif UpperltLower then
  • Foundfalse
  • return
  • elsif T (Middle)gtSought then Upper
    Middle-1
  • else LowerMiddle1

16
Declarations
  • Object same as Pascals constant variable
    declarations.
  • Subprogram same as Pascals function procedure
    declarations also operator overloading.
  • Package Tasks most important facilities
  • declare modules. Tasks can execute concurrently.
    Basic blocks of Ada programs.

17
modules
  • Communication through interfaces.

specification
body (definition)
Implements information hiding principle
18
Ada Compiler
  • Syntactic analyzer (parser)
  • More complicated than Pascal
  • Some have syntax-directed editor
  • Generates parse tree
  • Semantic analyzer
  • Type checking
  • Process generic declarations overloaded
    operators
  • More complex than Pascals
  • Generates program tree
  • Optimizer
  • Code generator

19
Design Data structure typing
  • The numeric types are generalized.
  • Integer type like Pascal, plus range
    constraint.
  • type coordinate is range -100 .. 100
  • Real

Floating point
Type coefficient is digits 10 range -1.0e10 ..
1.0e10
Fixed point
  • If the computer has single or double
  • precision the compiler selects between them.

20
Numeric Types
Short_Float
  • Float
  • Programmers are encouraged to use digit
    constraint rather than the above types to be more
    machine independent.
  • Preservation of information principle
  • Floating-point arithmetic
  • Maximum precision then rounded.

Long_Float
21
Floating point
Fixed point
Approximate Arith. with a relative error bound
Absolute error bound It fell into disuse after
introducing Floating points. The rule of early
computers. Still in use for commercial
programming More complicated arithmetic Ada
must support it, because in some embedded
systems peripheral devices (i.e. ADC) use this
method.
22
Fixed point Numbers
  • type Dollars is delta 0.01 range 0.00 ..
    1_000_000.00
  • Values of Dollars are multiples of 0.01
  • 16.7516750.01
  • 22000.01
  • Min Number of bits
  • If delta a power of 2 left or right shift
  • Compiler sometimes do this itself

Absolute error bound
23
Data Structure Typing
  • Constructors Are Based on Pascals
  • Similar to Pascals
  • Name equivalence is used.
  • 2 reasons
  • Repeating a type definition means logical
    difference.
  • Structural equivalence isnt well defined.
  • 2 new concepts subtype derived type

24
Subtypes
  • Constraints defines the subtype of the base type.
  • Arithmetic operations are allowed.
  • Compatible with its base type other subtypes of
    its base type (with runtime constraint check)
  • Subtype Index is Integer range 1 .. 100
  • keyword

25
Derived Types
  • Type percent is new Integer range 0 .. 100
  • It inherits all of the functions (user defined or
    built-in) from base type.
  • We can define abstractly-different derived type.
  • Conversion can be done explicitly between
    base/derived.

26
Constraints replace subranges
  • Replacement of Pascal subrange constructor
    constraint
  • Range constraint
  • Accuracy constraint
  • Discriminant constraint
  • Index constraint

27
1.Range constraint
  • Integer range 1..100
  • The same implementation as Pascals
  • Runtime expressions are allowed

28
Accuracy constraint
  • Float digits 10 range -1e6 .. 1e6

29
Discriminant constraint
  • Person (Male) is a type
  • person is a variant record of Male , Female
  • A runtime check is necessary in the assignment of
    a Person to a
  • Person (Male)

30
Index constraint
  • 2 problems of Pascals arrays
  • Static indexes
  • Passing arrays with different sizes to a function
    (i.e. sum)
  • These problems are solved
  • Type Vector is array (integer range lt gt) of float
  • Data Vector (1..100)
  • Days Vector (1 .. 365)
  • Function sum (V vector) return Float is

31
Index constraint
  • The compiler must pass actual bounds of the array
    as Hidden parameter
  • VFirst , VLast , VRange

Name of the array
For I in VRange loop Total Total V(I) End
loop
32
Enumerations can be overloaded
  • Pascal doesnt allow overlap of the elements of
    the enumeration types.
  • Type primary is ( Red, Blue, Green )
  • Type StopLight ( Red, Yellow, Green )
  • the Red identifier overloaded to 2meaning
  • Ada uses context to determine which Red is meant
  • In many situations programmers are required to
    specify.
  • Primary (Red), StopLight (Red)

33
Why we need overloaded enumerations?
  • Convenience
  • In natural languages, one word has several
    meanings.
  • Ada character set enumeration type
  • Characters may be repeated in different
    enumerations

34
Ada character set enumeration type
  • Type Discode is (A,B,C,D,E,F,G,
    H,I,J,K,L,M,N,O,P,Q,R,S,
    T,U,V,W,X,Y,Z,0,1,2,3,4,
    5,6,7,8,9,,-,.)

35
74 Design Name Structures
  • The primitives are those of Pascal
  • Constant
  • Variable
  • Type
  • Procedure
  • Function
  • Task
  • Package

36
Variable declaration
  • One of the simplest declaration is the variable
    declaration
  • It allows initialization
  • Eliminates a common error using an uninitialized
    variable
  • It causes a program to be more readable
  • The initial value is not restricted to be a
    constant. It can be an expression

37
Constant declaration
  • It is more general than a Pascal constant
  • Its value can be computed during the execution of
    the program
  • This facility aids program maintenance
  • Example
  • Feet_Per_Mile constant Integer 5280
  • PI constant 3.14159_26535_89793

38
  • Ada 83 allows the type to be omitted if it is a
    numeric type, and if the expression on the right
    involve only
  • Literals
  • Names of numeric literals
  • Calls of the predefined function ABS
  • Parenthesized literal expression
  • Predefined arithmetic operation

39
  • This feature is included to allow constants of
    type universal integer and universal real to be
    named
  • These types
  • Have the maximum possible precision
  • Are not normally accessible to programmers
  • This kind of declaration permits the programmer
    to name a type- and precision independent
    numerical constant

40
Specifications and definitions
  • Information hiding was supported by the ability
    to divide declarations into two parts
  • Interface
  • Implementation
  • Since subprograms form most of the interface to a
    package subprogram specification is very
    important

41
Global Variables Considered Harmful
  • Problems with block structure
  • Side effects
  • Result from hidden access to a variable
  • Indiscriminate access
  • The problem of indiscriminate access is the
    inability to prevent access to a variable

42
  • Vulnerability
  • Means a program segment can not preserve access
    to a variable
  • No overlapping definitions
  • The need for this arises from attempts to
    modularize large systems
  • We can not control share access to variables

43
Side Effects
  • Example
  • Integer procedure Max (x,y) integer x, y
  • begin
  • count count 1
  • Max if xgty then x else y
  • end
  • It makes it very difficult to determine the
    effects of a procedure from the form of a call of
    the procedure

44
Indiscriminate Access
  • Example
  • begin
  • integer array s1100
  • integer top
  • procedure Push(x) integer x
  • begin top top 1 stop x end
  • top 0
  • uses of Push
  • end

45
Vulnerability
  • Under certain circumstances it is impossible to
    preserve access to a variable
  • The basic problem is that new declarations can be
    interposed between the definition and use of a
    variable

46
  • Example
  • Begin
  • integer x
  • many lines of code
  • begin real x
  • many lines of code
  • x x 1
  • end
  • end

47
No Overlapping Definitions
  • Example
  • begin
  • array DA
  • array DB
  • procedure p1 .
  • procedure p2 .
  • procedure p3 .
  • procedure p4 .
  • end

48
Attributes of an Alternative
  • The default should not be to extend the scope of
    a variable to inner blocks
  • The right to access a name should be by the
    mutual consent of the creator and accessor of the
    name
  • Access right to a structure and its substructures
    should be decoupled.

49
  • It should be possible to distinguish different
    types of access
  • Declaration of definition ,name access,and
    allocation should be decoupled

50
Two Important Principles of Information Hiding
  • One must provide the intended user with all the
    information needed to use the module correctly
    and nothing more
  • One must provide the implementer with all the
    information needed to complete the module and
    nothing more

51
Packages and Info hiding
  • Package is primary Ada construct for implementing
    information hiding.
  • Can conceive of an Ada package as the
    implementation of an ADT.
  • Two parts
  • Interface specification
  • Body

52
Package Interface Spec
  • Contract with user
  • Addresses Rule 1 of Parnass Principles
  • One must provide the intended user with all the
    information needed to use the module correctly
    and nothing more.
  • package Complex_Type is ...specification of
    public names ...end Complex_Type

53
Package Interface Spec
  • package Complex_Type is type Complex is
    private I constant Complex function (X,Y
    Complex) return Complex function - (X,Y
    Complex) return Complex function (X,Y
    Complex) return Complex function / (X,Y
    Complex) return Complex function Re (X
    Complex) return Float function Im (X Complex)
    return Floatprivate type Complex is
    record Re, Im Float 0.0 end record I
    constant Complex (0.0, 1.0)end Complex_Type

54
Package Body
  • Known only to implementor
  • Contains the information for Rule 2 of Parnass
    Principles
  • One must provide the implementor with all the
    information needed to complete the module and
    nothing more.

55
Package Body
  • package body Complex_Type is function (X,Y
    Complex) return Complex is begin return
    (X.Re Y.Re, X.Im Y.Im) end function
    (X,Y Complex) return Complex is RP
    constant Float X.ReY.Re X.ImY.Im
    IP constant Float X.ReY.Im X.ImY.Re
    begin return (RP, IP) end function Re (X
    Complex) return Float is begin return X.Re
    end function Im (X Complex) return Float is
    begin return X.Im end function (X
    Float Y Complex) return Complex is begin
    return (X Y.Re, Y.Im) end ----- other
    definitions to complete the package ----
    end Complex_Type

56
The Mutual Consent Problem
  • Implementor can control access to names by their
    placement.
  • User needs to be able to control access to names
    visible to him
  • Packages solve this problems, too
  • use declaration

57
The Mutual Consent Problem
  • declare use Complex_type X,Y Complex Z
    Complex 1.5 2.5Ibegin X 2.5
    3.5I Y X Z Z Re(Z) Im(X)I if X
    Y the X Y X else X YZ end ifend

58
Packages for Shared Data
  • package Communication is In_Ptr, Out_Ptr
    Integer range 0..99 0 Buffer array (0..99)
    of Character (0..99 gt )end communication

59
Packages for Shared Data
  • with Communication use communicationprocedure
    P isbegin ... Buffer(In_Ptr) Next In_Ptr
    (In_Ptr 1) mod 100 ...end P
  • with Communication use Communicationprocedure
    Q isbegin ... C Buffer(Out_Ptr) ...end Q

60
Data Structure Management
  • package Stack1 is procedure Push (X in
    Integer) procedure Pop (X out
    Integer) function Empty return
    Boolean function Full return Boolean Stack_Err
    or exceptionend Stack1

61
Package Body
  • package body stack1 is
  • ST array(1..100) of Integer
  • Top Integer range 0..100 0
  • procedure Push (X in Integer) is
  • begin if Full then raise Stack_Error
  • else Top Top 1 ST (Top) X
  • end if end Push
  • procedure Push (X out Integer) is
  • begin end Pop
  • function Empty return Boolean is
  • begin return Top 0 end
  • function Full return Boolean is
  • begin return Top 100 end
  • end stack1

62
Data Structure Management
  • declare use Stack1 I, N Integerbegin ...
    Push(I) Pop(N) ... if Empty then Push(N)
    end if ...end

63
Data Structure Management
  • Dot notation
  • Stack1.Push(I)
  • Stack1.Pop(N)
  • if Stack1.Empty then Stack1.Push(N) end if
  • But theres a problem
  • What if you need more than one stack?

64
Generic Packages
  • Permit the definition of multiple data structures
    of the same type without copying all the code

65
Generic Packages
  • genericpackage Stack is procedure Push (X in
    Integer) procedure Pop (X out
    Integer) function Empty return
    Boolean function Full return Boolean Stack_Err
    or exceptionend Stack

66
Using Generic Packages
  • Works as if its a new type, which it really is
    if you look at the declaration
  • package Stack1 is new Stack
  • package Stack2 is new Stack
  • Stack1.push(x)
  • Stack2.pop(y)
  • Static instantiation

67
Parameterized Packages
  • What if you wanted different size stacks?
  • Out definition is fixed at 100 entries

68
Parameterized Packages
  • generic Length Natural 100package Stack
    is procedure Push (X in Integer) procedure
    Pop (X out Integer) function Empty return
    Boolean function Full return Boolean Stack_Err
    or exceptionend Stack

69
Parameterized Packages
  • package Stack1 is new Stack(100)
  • package Stack2 is new Stack(64)
  • Now we can have as many stacks as we like, in
    whatever size we like.
  • But what if you need a stack of characters
    instead of a stack of integers?

70
Type Parameters
  • generic Length Natural 100 type Element
    is privatepackage Stack is procedure Push (X
    in Element) procedure Pop (X out
    Element) function Empty return
    Boolean function Full return Boolean Stack_Err
    or exceptionend Stack

71
Type Parameters
  • package Stack1 is new Stack(100, Integer)
  • package Stack2 is new Stack(64, character)
  • Now we can have as many stacks as we like, in
    whatever size and with whatever type we like.
  • What else could you ever ask for?

72
Compiling Generic Packages
  • Relative easy if there are no parameters
  • Still relatively simple for simple
    parameterization
  • Type parameters more complicated
  • Calculating space for the array
  • Need different code for different types
  • Must generate code for each parameterized set of
    types!
  • But still want to eliminate duplicate code

73
Internal vs. External Representation
  • Internal Representation
  • Stack1.Pop(n)
  • Number of instances limited by the number of
    generic instantiations
  • Precursor of classes as seen in Simula and
    Smalltalk
  • External Representation
  • Complex type
  • Can treat type as a bona fide data value
  • Number of instances can be determined dynamically

74
Overloaded Procedures
  • Identification of what the operator really does
    requires understanding the context of the
    operator in question
  • Worse in nested function calls
  • Z F (G (X, Y))
  • Solve by propagating type information up down
    an expression tree in several passes.
Write a Comment
User Comments (0)
About PowerShow.com