Download ch05a

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
Programming Languages
2nd edition
Tucker and Noonan
Chapter 5
Types
Types are the leaven of computer programming;
they make it digestible.
Robin Milner
Copyright © 2006 The McGraw-Hill Companies, Inc.
5.1 Type Errors
5.2 Static and Dynamic Typing
5.3 Basic Types
5.4 Non-Basic Types-through 5.4.2
5.5 Recursive Data Types
5.6 Functions as Types
5.7 Type Equivalence
5.8 Subtypes
5.9 Polymorphism and Generics
5.10 Programmer-Defined Types
Copyright © 2006 The McGraw-Hill Companies, Inc.
What is a Type?
A type is a collection of values and operations on those values.
Example: Integer type has values ..., -2, -1, 0, 1, 2, ... and
operations +, -, *, /, <, ...
The Boolean type has values true and false and operations 
(&&),  (||),  (!).
Most types have a finite number of values due to fixed size
allocation (e.g., can’t express all integers)
Exception: some languages (Java, Haskell) support unlimited
integer sizes
Copyright © 2006 The McGraw-Hill Companies, Inc.
How Many Types Are Needed?
Early languages, (Fortran, Algol, Cobol), built in all
types – no way to define new ones.
If you needed a type color, you could use integers;
But – there’s no way to prevent illegal operations.
In programming languages, types are used to provide
ways of effectively modeling a problem solution,
so the user should be able to define new types.
Copyright © 2006 The McGraw-Hill Companies, Inc.
5.1 Type Errors
Machine data carries no type information.
Example: 0100 0000 0101 1000 0000 0000 0000 0000
could represent
– 3.375 (floating point)
– 1,079,508,992 (a 32-bit integer)
– 16472 & 0 (two 16-bit integers)
– The ASCII characters @ X NUL NUL
depending on the interpretation
Copyright © 2006 The McGraw-Hill Companies, Inc.
• A type error is any error that arises because an
operation is attempted on a data type for which it is
undefined.
– Type errors are common in assembly language
programming.
– High level languages reduce the number of type errors.
• A type system defines the bindings between a
variable’s type, values, and the operations that can
be performed on the values.
– provides a basis for detecting type errors.
Copyright © 2006 The McGraw-Hill Companies, Inc.
5.2 Static and Dynamic Typing
A type system imposes constraints that cannot be
expressed syntactically in BNF, EBNF, or any other
notation system for context-free grammars.
Type checking is part of semantic processing
– Some languages perform type checking at compile time
(e.g, C/C++). (compile-time semantics)
– Others (e.g., Perl, Python) perform type checking at run
time.
– Still others (e.g., Java) do both.
Copyright © 2006 The McGraw-Hill Companies, Inc.
Definitions
A language is statically typed if the types of all
variables are fixed when they are declared at
compile time.
A language is dynamically typed if the type of a
variable can vary at run time depending on the
value assigned.
Copyright © 2006 The McGraw-Hill Companies, Inc.
Definitions (continued)
• A language is strongly typed if its type system
allows all type errors in a program to be detected
either at compile time or at run time.
– If a language detects all type errors we sometimes say it
exhibits type safety
– Alternate definitions of strong typing exist, however
• A strongly typed language should know (either at
compile time or run time) the type of every
variable, constant, or function return value.
– Based on this knowledge, type errors can be detected –
for example, trying to assign a float value to a bool
variable would be detected.
Copyright © 2006 The McGraw-Hill Companies, Inc.
Definitions (continued)
• A strongly typed language can be either statically
(Ada, Java) or dynamically (Scheme, Perl) typed
– (except some definitions of strong typing require types to
be statically declared.)
• C/C++ are usually not considered strongly typed.
– e.g., it allows the assignment of a char value to an int
variable; it doesn’t check for out-of-bounds array
references, … .
• Strong typing promotes reliability, limits undefined
runtime behavior.
Copyright © 2006 The McGraw-Hill Companies, Inc.
5.3 Basic Types
Consist of atomic values (e.g., floats)
Sometimes called simple or primitive types.
Traditionally, correspond for the most part to data
types provided by computers and occupy fixed
amounts of memory, based on computer memory
structure.
Copyright © 2006 The McGraw-Hill Companies, Inc.
Common Memory Sizes for the Basic
Types (On 32-bit Computers)
• Nibble: 4 bits
• Byte: 8 bits
• Half-word: 16 bits
In most languages,
simple data types are
limited to one of
these sizes
• Word: 32 bits
• Double word: 64 bits
• Quad word: 128 bits
Copyright © 2006 The McGraw-Hill Companies, Inc.
Basic Types in C, Ada, Java
Type
C
Ada
Java
Byte
---
---
byte
Integer
short, int, long
integer
short, int, long
Real
float, double
float, decimal
float, double
Character
char
character
char
boolean
boolean
Boolean
---
Copyright © 2006 The McGraw-Hill Companies, Inc.
Memory Requirements
In most languages the binding of type to memory size is
implementation dependent.
For example, in C/C++ the only language restriction is
sizeOf(short)<= sizeOf(int)<= sizeOf(long)
Java defines size of each (half-word, word, double
word)
Copyright © 2006 The McGraw-Hill Companies, Inc.
Floating Point Representation
Floating point numbers (reals) are expressed as the
product of a binary number and a power of 2.
IEEE standard is used on computers designed/built
after 1980
If s = sign bit, e = exponent (in excess 127 notation)
and m is the mantissa, a single precision
representation means (-1)s x 1.m x 2e-127
Copyright © 2006 The McGraw-Hill Companies, Inc.
IEEE 754 Floating Point Number Representation
Figure 5.1
(-1)s x 1.m x 2e-127
Copyright © 2006 The McGraw-Hill Companies, Inc.
Floating Point Problems
• Integers are exact, floats aren’t necessarily so.
– e.g., since 0.2 cannot be represented exactly as a binary
fraction, the floating point representation is not exact;
result: 0.2 * 5 is not exactly 1.0
• Overflow and underflow are possible
• Inaccuracies due to floating point arithmetic may
affect results; for example, it’s possible that
a + (b + c) ≠ (a + b) + c
Copyright © 2006 The McGraw-Hill Companies, Inc.
Example: a = -1324x103, b = 1325 x 103,
c = 5424 x 100
Assume 4 significant figures for the fraction.
(a + b) = -1324 x 103 + 1325 x 103 = 1 x 103
(a + b) + c = 1000 x 100 + 5424 x 100
= 6424 x 100
Whereas
(b + c) = 1325 x 103 + 0005 x 103 = 1330 x 103
a + (b + c) = -1324 x 103 + 1330 x 103 = 6 x 103
= 6000 x 100
This is sometimes called representational error
Copyright © 2006 The McGraw-Hill Companies, Inc.
Overflow
Arithmetic operations may overflow the finite memory
allocation and give an incorrect result, regardless of
whether the values are integers or floats.
Some machines generate an exception when this
happens, some (e.g., Java Virtual Machine) don’t.
Copyright © 2006 The McGraw-Hill Companies, Inc.
Java’s BigInteger and BigDecimal type
http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigInteger.html
arbitrary-precision integers, semantics of arithmetic operations
exactly mimic those of Java's integer arithmetic operators
http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html
arbitrary-precision signed decimal numbers, consist of an arbitrary
precision integer unscaled value and a 32-bit integer scale.
Some applications; e.g., computer security, need very large
numbers.
Copyright © 2006 The McGraw-Hill Companies, Inc.
Operator Overloading
An operator or function is overloaded when its
meaning varies depending on the types of its
operands or arguments or result.
More about function overloading later
Java: a + b
– floating point add
– integer add
– string concatenation
What if a is an integer and b is a float?
Copyright © 2006 The McGraw-Hill Companies, Inc.
Mixed Mode Expressions
Mixed mode: operands have different types;
e.g., one operand an int, the other floating
point (Also sometimes called mixed type)
Why is it a problem? (Difference in hardware
operations)
Most languages have type rules that define how
mixed mode expressions will be treated.
Copyright © 2006 The McGraw-Hill Companies, Inc.
Type Conversions
A type conversion is a narrowing conversion if
the result type permits fewer bits, thus
potentially losing information.
Narrowing: float to int (2.4 to 2, for
example), or long to short
Otherwise it is termed a widening conversion.
Copyright © 2006 The McGraw-Hill Companies, Inc.
Implicit v Explicit Type Conversions
Language design decision: Should type
conversions be done automatically by the
compiler (implicit conversions) or should the
language require a cast (explicit
conversion/casts)?
Consensus: OK to have implicit widening
conversions; narrowing conversions should
be explicit.
Copyright © 2006 The McGraw-Hill Companies, Inc.
Implicit v Explicit Type Conversions
C++ example – widening
int sum;
int n;
float av;
av = sum/n;
C++ example – narrowing
float n1;
float n2;
int sum;
sum = n1 + n2;
Copyright © 2006 The McGraw-Hill Companies, Inc.
5.4 Nonbasic Types
Nonbasic types:
• Composed from other data types (e.g., arrays)
• Also called structured types
• Size is not fixed by the language but is
programmer determined.
Copyright © 2006 The McGraw-Hill Companies, Inc.
5.4.1 Enumeration Types
Provided by many languages as a way to
improve program readability
Essentially, allow the programmer to assign
names to integer values and then use the
names in programming (e.g., color, shape,
day, …)
Copyright © 2006 The McGraw-Hill Companies, Inc.
Enumeration Types – C/C++
Example:
enum Day {Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday, Sunday};
enum Day myDay = Wednesday;
Values can be compared:
if (myDay == Sunday) ...
Implicit enumeration-type-to-integer conversion
is okay; use a cast to go the other direction:
for(myDay=Monday;myDay<Friday;day(myDay+1))
Copyright © 2006 The McGraw-Hill Companies, Inc.
Enumeration Types - Java
More powerful than C/C++ version because the
Enum class inherits methods from the Java
class Object, such as toString.
for (day d : day.values())
System.out.println(d);
Copyright © 2006 The McGraw-Hill Companies, Inc.
5.4.2 Pointers
Design decision: to have pointers, or not?
• C, C++, Ada, Pascal
• Java???
History
• Early languages (Fortran, COBOL): static memory
• Later languages added pointers for dynamic memory
allocation (Pascal, C, C++, Ada)
• Modern languages like Java do not have explicit
pointers but still support dynamic memory allocation.
Copyright © 2006 The McGraw-Hill Companies, Inc.
Pointer Operations
The value of a pointer is a memory address.
C/C++ operators:
• address-of (unary &) returns the address of a
variable
• dereferencing (unary *) returns the value of a
reference (when used as an R-value)
– Corresponds to indirect addressing mode in machine
code
For an arbitrary variable x: *(&x) = x
Copyright © 2006 The McGraw-Hill Companies, Inc.
C/C++ Examples – Linked List Definitions
struct Node {
int key;
struct Node* next;
};
struct Node* head;
Node is a recursive
data structure
Node is a recursive data structure
Copyright © 2006 The McGraw-Hill Companies, Inc.
Fig 5.4: A Simple Linked List in C/C++
Copyright © 2006 The McGraw-Hill Companies, Inc.
Pointer Problems
Error-prone (mistakes in usage)
Buffer overflow, memory leaks (from non-freed
dynamic memory)
Particularly troublesome in C/C++, in part because of
the duality between pointers and array references
Copyright © 2006 The McGraw-Hill Companies, Inc.
C Array/Pointer Example with Dereferencing
float sum(float a[ ],
int n) {
int i;
float s = 0.0;
for (i = 0; i<n;
i++)
s += a[i];
return s;
float sum(float *a,
int n) {
int i;
float s = 0.0;
for (i = 0; i<n;
i++)
s += *a++;
return s;
Copyright © 2006 The McGraw-Hill Companies, Inc.
strcpy: A “bad” C example
void strcpy(char *p, char *q) {
while (*p++ = *q++) ;
}
Strings p and q are arrays of characters ending in a
NUL character, which is interpreted as false.
(Note that if p is shorter than q, the array will be
overrun.
Copyright © 2006 The McGraw-Hill Companies, Inc.
Reference Types in Java
• In Java, all nonprimitive types are reference types
• A variable from a reference type stores the address
of a memory object; i.e., it is the equivalent of a
pointer variable (may depend on JVM
implementation).
• References differ from pointers in that they don’t
require specific dereferencing operations.
• Java arrays and classes are reference types.
Copyright © 2006 The McGraw-Hill Companies, Inc.
Preview …
Next time:
arrays
structs
subtypes
parametric polymorphism and generics
Copyright © 2006 The McGraw-Hill Companies, Inc.