Download Chapter 4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter Four
Data Types
Pratt
Data Objects
• A run-time grouping of one or more pieces
of data in a virtual machine
• a container for data
• it can be
– system defined
– programmer defined
2
Attributes and Bindings
•
•
•
•
Type
Location
Value
Name
3
Data Types
• A data type is a class of data objects
together with a set of operations for creating
and manipulating them.
• Specification of a data type:
– attributes
– valid values
– valid operations
• example: specification of an array
4
Data Types
• Implementation of a data type
– storage representation of data object
– algorithms of valid operations
• Syntactic representation
5
Elementary Data Types
• Elementary data object contains a single
data value.
• A class of such data objects and the valid
operations: elementary data type.
6
Operations
• Signature of an operation:
op name: arg type * arg type * … *arg type --> result type
7
Operations as Mathematical
Functions
• Undefined for certain inputs.
– Underflow , overflow
• Implicit arguments.
• Side effects (implicit results).
• Self-modification (history sensitive)
8
Implementation
• Storage representation.
– Attributes:
• not stored in the runtime storage representation
• run time descriptor
• implementation of operations
9
Declarations
•
•
•
•
Choice of storage representation
Storage management
Polymorphic operations
Type checking
10
Type Checking
• Checking that each operation executed by a
program receives the proper number of
arguments of the proper data type.
• Dynamic type checking: run-time (type tags
for data objects)
• Static type checking: compile-time
11
Dynamic Type Checking
• Advantage: Flexibility
• Disadvantages:
– difficult debugging, some paths never checked.
– Extra storage for type information during
program execution.
– Software simulated type checking, reducing
speed.
12
Static Type Checking
• Information required:
– For each operation, the number, order, and data
types of its arguments and results.
– For each variable, the type of data object
named.
• Always A has the same type (a formal parameter).
– The type of each constant data object.
13
• Strong Typing.
– Detect all type errors statically.
– A function f , with signature f : S --> R , is type
safe if execution of f cannot generate a value
outside of R .
– Type inference.
• ML (p.124)
14
Type Conversion and Coercion
• A type mismatch can cause :
– error
– coercion (implicit type conversion)
• type conversion:
– conversion-op : type1 --> type2
• coercions if no loss of information.
– Widening or promotion
– Narrowing
15
• What about Coercion
– for dynamic type checking?
– for static type checking?
( Code inserted during compilation)
(p. 126)
16
Two Opposed Philosophies
• No coercions (Pascal, Ada)
• Coercion as a rule (C)
17
Assignment
• Assignment is the basic operation for
changing the binding of a value to a data
object.
• In Pascal:
– assignment: integer * integer --> void
• In C:
– assignment:integer * integer-->integer
(p 127)
18
Initialization
• An uninitialized variable: an l-value with no
corresponding r-value.
• A serious source of programming errors.
• Explicit , implicit.
19
Elementary Data Types
• Numeric Data Types
–
–
–
–
Integers
Subranges
Floating-point Real Numbers
Fixed-point Real Numbers
• Enumerations (one of a small number of symbolic values)
• Booleans
• Characters
20
Internationalization
•
•
•
•
•
Sorting
Case
Scanning direction
Country-specific data format
Country-specific time format
21
Structured Data Objects and
Data Types
• Structured data object or data structure: a
data object that is constructed as an
aggregate of other data objects, called
components.
• Particular aspects of structured data types:
– how to indicate the component data objects of
a data structure and their relationships.
– storage management.
22
Specification of data structure
types
•
•
•
•
•
Number of components.
Type of each component.
Names to be used for selecting components.
Maximum number of components.
Organization of the components.
23
Number of Components
• Fixed size.
– Arrays, records , character strings.
• Variable size.
– Stacks, lists, sets, tables, files, character strings.
– Use a pointer data type.
– Insert and delete operations.
24
Type of Each Component
• Homogeneous.
– Arrays, character strings, sets, files.
• Heterogeneous.
– Records, lists.
25
Names to be used for selecting
components
• Array: an integer subscript or a sequence of
subscripts.
• Record: a programmer defined identifier.
• Stacks and files: ?
26
Maximum number of components
• For a variable size data structure.
27
Organization of the components
• Simple linear sequence .
– Vectors, records, strings, stacks, lists, files.
• Multidimensional.
– Arrays, record, lists.
28
Operations on Data Structures
• Component selection operations.
– Random selection
– Sequential selection.
How you select a component?
• Whole-data-structure operations.
– Addition(arrays), assignment(records),
union(sets).
• Insertion/deletion of components.
• Creation/deletion of data structures.
29
Implementation of Data Structure
Types
Storage Representation :
• affected by
– efficient selection of components.
– efficient overall storage management.
• Includes
– storage for the components,
– an optional descriptor (for the attributes).
30
Storage Representation
• Sequential representation.
– Descriptor and components.
– Fixed size.
• Linked representation.
– By pointers.
– Variable size.
31
Implementation of Operations
• Sequential representation
– base-address-plus-offset using an accessing
formula. (p. 146)
• Linked representation
– following a chain of pointers
32
Storage Management
• Access path : its name, a pointer.
• Life time of a data object: binding to a
storage location.
Two problems:
• garbage
• dangling references
33
• garbage: all access paths to a data object are
destroyed but the data object continues to
exist (the binding of data object to storage
location has not been broken),
• dangling references: an access path that
continues to exist after the lifetime of the
associated data object.
(p. 149)
34
Type Checking
• Existence of a selected component.
• Type of a selected component.
35
Data Structures
•
•
•
•
•
•
•
•
Vectors and Arrays
Records
Variant Records
Lists
Character Strings
Pointers
Sets
Files
36