1 ada programming language chapter 7 modularity and data abstraction in the name of god
TRANSCRIPT
1
Ada Programming Language
Chapter 7
Modularity And Data Abstraction
Modularity And Data Abstraction
In the Name of GodIn the Name of God
2
The software crisis & reliable programming
1970
software cost increase without bound Dijkstra
difficulty of producing a program program length2
Software Crisis
But this proportion must become linear
3
Parnas’s Principle Control of complexity of a large program
Modularization Modules are independent of each other
in debug, understand & maintain. Parna’s 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 Babbage’s 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
1. must be implemented2. 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 Pascal’s Keywords in lowercase Others mixed (lower & upper)
Ada’s constructs
Declarations
Expressions
Statements
Types
14
Ada’s constructs Expressions & statements similar to
Pascal Types also similar but more flexible &
less problem Declarations are very differentobject
type
subprogram
package
task
15
package Tables istype Table is array (Integer range < > ) of float;procedure BinSearch (T: Table; Sought: Float;
Location: out Integer; Found: out Boolean) is subtype Index is Integer range T’First .. T’Last;Lower: Index := T’First;Upper: Index := T’Last;Middle: Index := (T’First+ T’Last)/2;begin loop
if T (Middle)=Sought then location:=middle; Found:=true; return;elsif Upper<Lower then Found:=false; return;elsif T (Middle)>Sought then Upper :=
Middle-1;else Lower:=Middle+1;end if;Middle:=(Lower+ Upper)/2;
end loop; end BinSearch;end Tables;
16
Declarations
Object same as Pascal’s constant & variable declarations.
Subprogram same as Pascal’s function & procedure declarations & also operator overloading.
Package & Tasks most important facilitiesdeclare 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
1. Syntactic analyzer (parser)More complicated than PascalSome have syntax-directed editorGenerates parse tree
2. Semantic analyzerType checkingProcess generic declarations & overloaded operatorsMore complex than Pascal’sGenerates program tree
3. Optimizer4. 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
Fixed point
Type coefficient is digits 10 range -1.0e10 .. 1.0e10
If the computer has single or double precision the compiler selects between them.
20
Numeric Types… 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.
Short_Float
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.0116.75=1675*0.012=200*0.01
Min Number of bits:
If delta a power of 2 : left or right shift Compiler sometimes do this itself
Absolute error bound
Bits276.26log 01.0)01000000(12
23
Data Structure & Typing Constructors Are Based on Pascal’s
Similar to Pascal’s Name equivalence is used.
2 reasons Repeating a type definition means logical
difference. Structural equivalence isn’t 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 .. 100keyword
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
1. Range constraint2. Accuracy constraint3. Discriminant constraint4. Index constraint
27
1.Range constraint
Integer range 1..100 The same implementation as
Pascal’s 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 Pascal’s arrays
Static indexes Passing arrays with different sizes to a
function (i.e. sum) These problems are solved
Type Vector is array (integer range < >) 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 parameterV’First , V’Last , V’Range Name of the array
For I in V’Range loopTotal := Total + V(I)
End loop;
32
Enumerations can be overloaded Pascal doesn’t 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?
1. Convenience In natural languages, one word has
several meanings.
2. 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
7:4 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 x>y 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 s[1:100];integer top;procedure Push(x); integer x;
begin top := top + 1; s[top] := 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 Parnas’s
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 istype 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 Float;
privatetype 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 Parnas’s Principles One must provide the implementor with all
the information needed to complete the module and nothing more.
55
Package Bodypackage 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.Re*Y.Re – X.Im*Y.Im; IP: constant Float := X.Re*Y.Im + X.Im*Y.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
declareuse Complex_type;X,Y : Complex;Z : Complex := 1.5 + 2.5*I;
beginX := 2.5 + 3.5*I;Y := X + Z;Z := Re(Z) + Im(X)*I;if X = Y the X := Y + X;else X := Y*Z;end if;
end;
58
Packages for Shared Data
package Communication isIn_Ptr, Out_Ptr : Integer range 0..99 := 0;Buffer : array (0..99) of Character := (0..99
=> ‘ ‘);end communication;
59
Packages for Shared Datawith Communication; use communicationprocedure P isbegin
...Buffer(In_Ptr) := Next;In_Ptr := (In_Ptr + 1) mod 100;...
end P;
with Communication; use Communication;procedure Q isbegin
...C := Buffer(Out_Ptr);...
end Q;
60
Data Structure Management
package Stack1 isprocedure Push (X : in Integer);procedure Pop (X : out Integer);function Empty return Boolean;function Full return Boolean;Stack_Error : exception;
end Stack1;
61
Package Bodypackage body stack1 is
ST : array(1..100) of Integer;Top : Integer range 0..100 := 0;
procedure Push (X : in Integer) isbegin if Full then raise Stack_Error;
else Top := Top +1; ST (Top) := X;end if; end Push;
procedure Push (X : out Integer) isbegin … end Pop;
function Empty return Boolean isbegin return Top = 0; end;
function Full return Boolean isbegin return Top =100; end;
end stack1;
62
Data Structure Management
declareuse Stack1;I, N : Integer;
begin...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 there’s 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_Error : exception;
end Stack;
66
Using Generic Packages
Works as if it’s 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
genericLength : Natural := 100;
package Stack isprocedure Push (X : in Integer);procedure Pop (X : out Integer);function Empty return Boolean;function Full return Boolean;Stack_Error : exception;
end 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
genericLength : Natural := 100;type Element is private;
package Stack isprocedure Push (X : in Element);procedure Pop (X : out Element);function Empty return Boolean;function Full return Boolean;Stack_Error : exception;
end 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 callsZ := F (G (X, Y));
Solve by propagating type information up & down an expression tree in several passes.