surfing ada for esp - unipi.itpages.di.unipi.it/romani/didattica/ep/ada2.pdf · 15/05/2013 1...
TRANSCRIPT
15/05/2013
1
Surfing Ada for ESPPart 2
C. Montangero
Dipartimento d’Informatica
Corso di ESperienze di Programmazione
a.a. 2012/13
15/05/2013
2
Table of contents
15/05/2013
3
CHAPTER 9: Packages
• Packages allow the programmer to define groups of logically related items
• wide variety of uses– collections of common declarations
– groups of subprograms
– encapsulated data types
• basis for a powerful structuring tool for complex programs
• clear separation between– information that is usable by the rest of the program, and
– information that must remain purely internal to the package• the internal information is hidden, and thereby protected from
deliberate or inadvertent use by other programmers.
• easier to replace one implementation of (the services offered by) a package by another.
15/05/2013
4
Packages
• Packages are an essential tool for program modularity– supporting program verification and information hiding
• Parnas, D.L., Information distribution aspects of design methodology, Information Processing 71, pp. 339-344
• Facilities for modularization in many languages– Simula, Clu, and Alphard provide dynamic facilities
• may entail large run-time overhead
– Ada is more static• in the spirit of previous solutions offered in Lis, Euclid, Mesa, and
Modula
• it retains the best aspects of earlier solutions in Fortran and Jovial.
As Steve McConnell has pointed out, good object oriented design requires that
objects hide something. Unfortunately, most of the O-O code that I have seen
hides nothing. The programs look like COBOL programs with a new syntax.
ACM Fellow Profile: David Lorge Parnas http://www.sigsoft.org/SEN/parnas.html
15/05/2013
5
Kinds of modularization
• Named collections of entities– Logically related constants, variables, and types
• to be used in other program units
– The package exposes all of its declarations
• Groups of related subprograms • commonly called a software package
– Logically related functions and procedures
– Sharing of internal data, types, and subprograms
– The package exposes the declarations of the externally usable subprograms
– The package hides their implementations• and the declarations of the shared internal entities
15/05/2013
6
Kinds of modularization
• Encapsulated data types - Private types
– definition of new types and associated operations
– the user does not know how they are implemented
– the user need not care, either
• The package exposes
– the type name
– the declarations of applicable operations
• The package hides
– all details of structure, representation, and implementation of the operations
– several related types may be encapsulated in the same package.
15/05/2013
11
Groups of Related Subprograms
• Actually, an object
– ITEM_TABLE, better than TABLE_MANAGER
• Package specification -> subprogram specs only
• The implementation is hidden in the package body
15/05/2013
12
Package body
• body elaboration after that of specification
• it includes execution of the final statements– cfr object constructor
complete definition
15/05/2013
13
Private Types
• the name of a type is public
• the internal properties are available only to the subprogram bodies
– in the package body
• the type name is declared within the visible part
– at the same time specifying the type to be private
– the full definition of the type is provided followingthe visible part
15/05/2013
17
Eg: simple Java objects
• Need to split the public methods getters /setters – To allow access to any private attribute/operation
• No information needed on the implementation structure– Even for compilation
– Possibly: REPRESENTATION CLAUSE for configuration
15/05/2013
19
CHAPTER 10: Separate Compilation
and Libraries• Ada program = collection of compilation units
• Compilation unit– library unit
• package declaration
• subprogram declaration
• generic declaration or instantiation
– secondary unit (related to another compilation unit)• the body of a library unit
– a package body
– a subprogram body
– the body of a generic unit
• a subunit of another compilation unit
• Each compilation unit may have a context clause– with clauses that mention the names of the needed library units
• Compilation units can be submitted individually to a compiler– But they depend on each other as indicated by with clauses (partial order)
– the compilation units that form a given program belong to a program library
15/05/2013
20
Bottom-Up Program Development
• programmers develop libraries of generally usable packages
• separately compiled and made available in the program library and used
15/05/2013
21
Hierarchical Program Development...
• ... or stepwise refinement – Wirth, N., Program development by stepwise refinement, Comm. ACM 14,4 (1971) 221-227
• Top level: formulation of the program in terms of
– operations to be supplied by the next level
• Each such operation further defined in terms of– operations of another lower level, and so on.
• In support of stepwise refinement , Ada offers – compilation units that are subunits of other compilation units.
• (in)formal specification are paramount
15/05/2013
22
An example
15/05/2013
23
Here comes the secondary unit
• Visibility:– TABLE is seen as if
• the body of the separate unit
• were textually nested at the place of the stub
• Secondary units may have their own with clauses
15/05/2013
24
CHAPTER 12: Generic Units
• A generic subprogram is a template – for ordinary subprograms
– obtained by associating • specific actual parameters
• with the generic formal parameters
• Similarly a generic package is a template – for ordinary packages
• A generic declaration vs type declaration
15/05/2013
25
An example : sort
• ITEM qualunque
– purché abbia un’operazione ITEM x ITEM -> BOOL
• ROW array di ITEM, con qualunque indice
– purché discreto, naturalmente
-- possibile default
15/05/2013
26
Uso di SORT (animaz)
-- usa le procedura generica
-- il tipo degli elementi
-- struttura da ordinare
-- tipo dell’oggetto da ordinare
-- oggetto da ordinare
-- chiamata istanza di SORT
15/05/2013
28
mapcar
• mapcar restituisce la lista dei risultati di
ACTION su ogni elemento di T
15/05/2013
29
DATA STRUCTURE
(PLUS A B)
15/05/2013
30
IMPLEMENTATION
• Poco dinamismo, più efficienza a run-time
15/05/2013
31
CHAPTER 14: Exception Handling
• Eccezione predefinita:
– Funzione capitozzata
– Attributo predefinito
15/05/2013
32
Ada vs Java
• Possible effect: exception
• Not in the function spec
– cfr Java’s throws clause
15/05/2013
33
Previous models
• PL/1: on condition
– possibility to raise the exception again
– possibility to continue with a value
• More powerful, but
– problems with reasoning on programs
– without resumption it is easier to distinguish the normal flow and the exceptional one
– two different assertions
15/05/2013
35
Ada 2005: Object Orientation
• tagged record types
– implicit access, dereferencing and dot notation
– the first argument is «this»: note the «in» mode
– Distance to be inherited, but Area as to be redefined
15/05/2013
36
Inheritance
• with extension clause
– as extends
• and redefines Area
• how to be sure to call the right Area?
15/05/2013
37
Polimorphic functions
• CLASS attribute
– to declare polimorphic variables/parameters
• il tipo preciso di OC è staticamente ignoto
– la chiamata di Area si usa il binding dinamico
• se OC fosse di tipo Object,
– il risultato sarebbe sempre zero (binding statico)
15/05/2013
38
Ada 2005: OO & maintenance
• Hiererachical library
– child packages
• Piecewise package specification
– No effect on old programs, no re-compilation
needed
• Similarly for bodies and private parts
• (private) package child
15/05/2013
41
More in general...
• piecewise private part
– private package Parent.Child
– to structure the implementation