Title: Design Specification Principles
1Design Specification Principles
- Abstractions
- Parameter and Parameter Transmission
- Exception and Exception Handling
- Expressions
- Static and Dynamic Environments
2 Abstraction
- A representation of an object that ignores what
could be considered as irrelevant details of that
object, thus making the use of that object
easier. - User/Logical/Conceptual/High-level view
- Programming language abstraction falls into two
general categories data abstraction and control
abstraction
3 Abstraction
- With data abstraction, you can work more
effectively. With procedural abstraction, you can
concentrate on good design practices and
modularity. - Basic Abstraction
- Structured Abstraction
- Unit Abstraction
4Data Abstraction
- Deal with program components that are subject to
computation. - Based on the properties of the data objects and
operations on those objects. - Leads consideration of abstract data types, which
allow development of programs that are
independent of how data are represented in them.
5Abstract Data Type
- ADT chosen data representation and set of
procedures. - Only this procedures can direct access to the
data. - Representation independence client code is
independent of data representation if
representation is changed, only the ADT
procedure is affected.
6Basic Data Abstraction
- Refers to the internal representation of common
data values in a computer system. - Declaration statement specify the variables
name and data type. - int x
- float y
- char z
7Structured Data Abstraction
- The principal method for abstraction collections
of data values that are related. - Example personal record/struct, poker-card
record/struct - Useful when you want to assign one entire record
to another or pass an entire record as a
parameter. - Permits the user to manipulate a record by name,
ignoring such details as component names and
types.
8Structured Data Abstraction
- typedef struct
- char grade
- int course
- Card
- Card deck52
- typedef does not create new type, it simply
creates a new type name, which may be used as an
alias for an existing type name.
9Unit Data Abstraction
- The principal method for collecting all the
information needed to create and use a particular
data type in one unit location. ??????????
???????????????????. - The typical scope of unit data abstraction is a
module. - Unit data abstraction ensures that changes in the
structure of the data type do not affect other
areas of the program. - Modula-2 supports module
- Ada supports package
10Advantages of using Unit Data Abstraction
- Page 50Page 51
- Program units are easier to read, write, and
modify. - This property is crucial to the top-down design
approach to programming languages. - Program units are reusable.
- Eliminates redundant programming effort and
reduces the number of errors. - A program based on the unit data abstraction is
not affected by the details of its implementation.
11Unit Abstraction in Turbo Pascal
- We can combine all of the const, type, and
variable definitions and the stack-processing
procedures and functions into a single unit. - Unit heading unit unitname
- File nameconsist of the first 8 characters of
the unit name and ended with .PAS - Compiled object file name with extension .TPU
12Unit Abstraction in Turbo Pascal
- Interface part begin with reserved word
interface may contain a use clause to import
items from other units and a declaration
part(const, type, variable, procedure heading,
function heading) - Implementation part begin with reserved word
implementation followed by an optional use
clause, a declaration part, and an initialization
part. - Initializationmay consist only the reserved word
end or some some statements.
13Unit Abstraction in Turbo Pascal
- After a unit has been compiled, the constants,
types, functions, and procedures defined in its
interface part can then be imported into and used
in any program by simply inserting a use clause
after the program heading. - The statements in the initialization part are
executed before those in any program or other
unit in which this unit is used.
14Turbo Pascal Unit Abstraction xample
- unit StackADT
- Interface
- Const StackMAX
- type StackItemType
- StackArrayType
- StackTyperecord
- top 0..StackMAX
- Items StackArrayType
- end
- Var StackError boolean
- Procedure CreateStack(var StackStackType)
-
15Turbo Pascal Unit Abstraction Example
- Function EmptyStack(stackStackType)boolean
- Procedure Pop(var StackStackType var
ItemStackItemType) - Procedure Push(var StackStackType
ItemStackItemType) - Implementation ( ???????????? )
- ( ???StackError?? )
- Function EmptyStack(StackStackType)boolean
- begin EmptyStack (Stack.Top 0)
- StackError false end
- end.(????????)
16Unit Abstraction in Modula-2
- Modules
- A MODULA-2 program consists of a program module
and other modules from which the program module
imports. - The other modules are split into two parts
definition module and implementation module. - DEFINITION MODULE StackModule
- EXPORT QUALIFIED push,pop,empty
- PROCEDURE push(chCHAR)
- PROCEDURE pop(VAR chCHAR)
- PROCEDURE empty()BOOLEAN
- END StackModule.
17Unit Abstraction in Modula-2
- MODULE Reverse
- FROM InOut IMPORT Read,Write,WriteLn, EOL,
Writestring - FROM StackModule IMPORT push,pop,Empty
- VAR chCHAR
- BEGIN
- Writestring(Enter string) Read(ch)
- WHILE chltgtEOL Do push(ch) read(ch) END
- Writestring(The reversal is)
- WHILE NOT empty() Do pop(ch) Write(ch) END
- WriteLn
- END Reverse.
-
18Unit Abstraction in Modula-2
- IMPLIMENTATION MODULE StackModule
- CONST Max100
- VAR StackArrayARRAY1..MAX OF CHAR
- Top0..MAX
- PROCEDURE push(chCHAR)
- BEGIN INC(Top) StackArrayTopch END push
- PROCEDURE pop(VAR chCHAR)
- BEGIN chStackArrayTop DEC(top) END pop
- PROCEDURE empty()BOOLEAN
- BEGIN RETURN Top0 END empty
- BEGIN Top0 END StackModule. (initialization
code)
19Separate Compilation in Modula-2
- In Module-2, before a module can be compiled, all
definition modules that it imports from must have
been compiled previously this allows the
Modula-2 compiler to check that each imported
object is used in the manner that its definition
module specifies. Similarly, each definition
module is compiled before its corresponding
implementation module, since the definition
module forms a preface to the implementation
module. - Version checkingensures consistent interface,
but does not ensure an up-to-date interface.
20Separate Compilation in Modula-2
.def file
.sym file ?version
Compiling
.sym file
( for modules imported from )
Containing information about the objects exported
by the module.
.mod file
Compiling
.lnk file ?object ?version
.sym file
object code
( for modules imported from )
.lnk file
Linking
.lod file
21Separate Compilation in Modula-2
- A module(program or implementation) or procedure
can declare five kinds of objectsconstants,
types, variables, procedures,and modules. - A module that is declared inside another module
or procedure is called a local module. - Although local modules do not have the advantage
of separate compilation, they retain the other
advantage of modulesobjects within a local
module that are not exported are hidden from the
rest.
22Control Abstraction
- Describes the order in which statements or groups
of statements (program blocks) are to be
executed. - Deals with the components of the program that
transfer control (e.g. loops, conditional
statements, procedure calls) - Can be thought of as modification of a programs
execution path.
23Basic Control Abstraction
- Assignment is a basic control abstraction
- Assignment statement abstracts the computation
and storage of a value to the location given by a
variable. - Two mechanisms can be used to accomplish the flow
of control over individual instructions - sequencing and branching
- Sequencing is accomplished by automatically
increasing the program counter. Branching can be
represented by a jump or goto instruction.
24Structured Control Abstraction
- Selection
- While-loop, for-loop, do-while loop, repeat loop,
loop-exit . - Subprogram definition and invocation allows one
to consider a sequence of actives as a single
action and to control the instruction of these
actions with other parts of the program. - Subprogram calls are more complex than selection
or looping. - Structured control abstractions can be nested to
any desired depth.
25Unit Control Abstraction
- Represents a collection of procedures that
provide logically related services to other parts
of a program. - Statistical terms computation mean, median,
standard deviation - Ada provides this feature.
26Parameter and Parameter Transmission
- Refers to the mechanisms that send data to and
return data from subprograms. - Parameter transmission is the major alternative
method for sharing data objects among
subprograms. - Subprogram heading list the name and declare the
type of the formal parameter. - A formal parameter is the specification of the
parameter within the called subprogram.
27Parameter and Parameter Transmission
- Actual parameter is the specification of the
parameter in the caller subprogram. It is a data
object that is shared with the called
subprogram.(?) - Actual parameter can be classified as
- local data object of the caller, formal
parameter of the caller, nonloacl data object
visible to the caller, a result returned by a
subprogram invoked by the caller. - Example page 55
28Semantics Models of Parameter Passing
- Relation between formal parameter and actual
parameters can be characterized by one of the
distinct semantic models - In mode,
- Out mode,
- InOut mode.
- Language designers have developed a variety of
models to guide the implementation of the
parameter passing.
29Parameter Passing by Constant-Value
- The argument expressions are evaluated by the
time of the call, and their values become the
values of the parameters while the subprogram
runs. - Is an implementation of the In Mode semantics
model. - The formal parameter acts as a local constant
during execution of the subprogram (no
modification in the value of the actual parameter
is possible ).
30Parameter Passing by Constant-Value
- Example Max (10,23)
- Need additional memory space and physical data
transfer, but the access is more efficient. - Default parameter passing mechanism of Ada,
Pascal, Modula-2 and C
31Parameter Passing by Reference
- The most common parameter passing mechanism.
- Evaluate the address of the actual argument at
calling point Pass the address and the local
storage location for the formal parameter holds
the address Formal parameter becomes an alias
for the actual parameter. - An Implementation of the InOut semantics model.
32Parameter Passing by Reference
- procedure change (VAR X integer )
- Begin XX1 End
- change(Y)
- void change (int X) X
- change(Y)
- void change (int X) (X)
- change(Y)
33Parameter Passing by Reference
- Advantages storage and time savings for
aggregate types of actual arguments. - Disadvantages slower access, error-prone in
one-way communication, aliasing.
34Parameter Passing by Name
- The most difficult parameter-passing mechanism.
- Views a subprogram call as a substitution for the
entire body of the subprogram. - An implementation for the InOut Mode semantics
model. - The actual parameter is textually substituted for
the corresponding formal parameter in all its
occurrences in the called subprogram.
35Parameter Passing by Name
- void change (int A, int B)
- int temp
- tempA AB Btemp
-
- caller change (i, Xi)
- effect tempi
- iXi
- Xitemp
- let before call i3, X34
- after call i4, x43,
x34unaffected!!
3627
- The formal parameter is bound to an access method
(a parameter-less subprogram, called thunk, to
evaluate the value or value of the actual
argument) at the time of the subprogram call, but
the actual binding to a value or an address is
delayed until the formal parameter is assigned or
referenced. - Disadvantage Can easily lead to programs that
are hard to read and understand. Run time calls
to thunks are costly. - Advantage Flexibility (Can be a powerful
mechanism in certain circumstances. F-g. apply an
operation to an entire array).
3728
- include ltstdio.hgt
- main()
-
- int Sum(int, int, int, int)
- int I0, Total0
- int X101,2,3,4,5,6,7,8,9,10
- Total Sum(X,I,0,10)
- printf(the sum of all emements is d,
Total) -
- int Sum(int A, int Index, int Lower, int
Upper) -
- int Temp0
- for(IndexLower IndexltUpper Index)
-
- TempTempAIndex
-
- return Temp
-
38Parameter Passing by Result
- An Implementation for the Out Mode semantics
model. - Formal parameter acts as a local variable and
does not transfer back its value to the
corresponding actual argument upon exit the
subprogram. - Need local storage and data transfer.
- Transfer back order is usually implementation
dependent. this causes portability problem.
39Parameter Passing by Result
- include ltstdio.hgt
- main()
-
- void Init(int, int, int)
- int Index, A5
- Init(A,0,5)
- for(Index0 Indexlt5 Index)
- printf(d, AIndex)
-
- void Init(int A, int Lower, int Upper)
-
- int I
- for(ILower IltUpper I)
- xII2
-
40Parameter Passing by Value-Result
- A combination of pass by value and pass by
result. - An implementation for the InOut Mode semantics
mode. - Also known as call by copy.
- Share the same advantages/disadvantage of pass by
value and pass by result.
41Parameter Passing by Value-Result
- (calling subprogram)
-
- void Change(int)
- int A10
- Change(A)
- printf(d,A)
-
- (called subprogram) Change(int X)
-
- X5
- A2
-
42Expressions
- Formed from operators and operands.
- Operators are known as functions, operands are
known as arguments or parameters. - Predefined operators or user-defined operators
- Monadic or unary operators, dyadic or binary
operators, ternary operators - Function can have different number of operands
43Infix Notation
- Operand1, Operator Operand2
- Tree RepresentationPage83 Figure3-3
- Suitable for binary operations.
- With implicit precedence and associativity rules
and explicit use of parentheses provides a
natural representation for most arithmetic,
relational,and logical expressions.
44Prefix Notation
- Also know as Polish Prefix
- Operator Operand1 operand2
- Parenthesis-free
- Cambridge Polish A variant of prefix notation
used in LISP. All operators and operands must be
enclosed in parentheses . - ( (- 5 3) ( 2 4) )
- The decoding of prefix notation extends to
operators with kgt1 operands
45Prefix Notation
- Also known as Suffix or Reverse Polish notation.
- Operand1 operand2 operator
- Parentheses free
- Can be evaluated easily by use of a stack
structure. - The decoding of postfix notation extends to
operators with kgt1 operands.
46Mixfix Notation
- The operations are defined as a combination of
prefix, postfix,and infix notations. - If agtb then a 25 else b 35
- While (agtb) a b5
- For(a25 alt10 aa1) b b5
- refer to page 85
47Applicative Order Evaluation
48Normal Order Evaluation
49Short Circuit Evaluation
50Lazy Evaluation
51Block Order Evaluation