Upload
alhaji-idris
View
227
Download
0
Embed Size (px)
Citation preview
8/12/2019 Values and storages
1/20
8/12/2019 Values and storages
2/20
2-2
Static vsdynamic typing (1)
Before any operation is performed, its operands must betype-checkedto prevent a type error. E.g.:
modoperation: check that both operands are integers
andoperation: check that both operands are booleans
indexing operation: check that the left operand is an array, and thatthe right operand is a value of the arrays index type.
8/12/2019 Values and storages
3/20
2-3
Static vsdynamic typing (2)
In a statically typedPL:
all variables and expressions have fixed types(either stated by the programmer or inferred by the compiler)
all operands are type-checked at compile-time.
Most PLs are statically typed, including Ada, C, C++,Java, Haskell.
8/12/2019 Values and storages
4/20
2-4
Static vsdynamic typing (3)
In a dynamically typedPL:
values have fixed types, but variables and expressions do not
operands must be type-checked when they are computed at run-
time.
Some PLs and many scripting languages are dynamically
typed, including Smalltalk, Lisp, Prolog, Perl, Python.
8/12/2019 Values and storages
5/20
2-5
Example: Ada static typing
Ada function definition:
functionis_even (n: Integer)
returnBoolean is
begin
return(nmod2 = 0);end;
The compiler doesntknow the value of n.
But, knowing that ns
type is Integer, it infers
that the type of nmod2=0 will be Boolean.
The compiler doesnt know thevalue of p. But, knowing that ps
type is Integer, it infers that the
type of p+1 will be Integer.
Call:
p: Integer;
ifis_even(p+1)
Even without knowing the values of variables andparameters, the Ada compiler can guarantee that no typeerrors will happen at run-time.
8/12/2019 Values and storages
6/20
2-6
Example: Python dynamic typing (1)
Python function definition:
defeven (n):
return(n % 2 == 0)
The type of nis unknown.
So the % (mod) operation
must be protected by a run-
time type check.
The types of variables and parameters are not declared, andcannot be inferred by the Python compiler. So run-time
type checks are needed to detect type errors.
8/12/2019 Values and storages
7/20
2-7
Static vsdynamic typing (4)
Pros and cons of static and dynamic typing:
Static typing is more efficient. Dynamic typing requires run-time
type checks (which make the program run slower), and forces all
values to be tagged (to make the type checks possible). Static
typing requires only compile-time type checks, and does not forcevalues to be tagged.
Static typing is more secure: the compiler can guarantee that the
object program contains no type errors. Dynamic typing provides
no such security.
Dynamic typing is more flexible. This is needed by someapplications where the types of the data are not known in advance.
8/12/2019 Values and storages
8/20
2-8
Expressions
An expressionis a PL construct that may be evaluatedto
yield a value.
Forms of expressions:
literals (trivial) constant/variable accesses (trivial)
constructions
function calls
conditional expressions
iterative expressions.
8/12/2019 Values and storages
9/20
2-9
Function calls
A function callcomputes a result by applying a function
to some arguments.
If the function has a single argument, a function call
typically has the form F(E), or just FE, whereFdetermines the function to be applied, and the expressionE
is evaluated to determine the argument.
In most PLs,Fis just the identifier of a specific function.
However, in PLs where functions as first-class values,Fmay be any expression yielding a function. E.g., this
Haskell function call:
(ifthensin elsecos)(x)
8/12/2019 Values and storages
10/20
2-10
Conditional expressions
A conditional expressionchooses one of its
subexpressions to evaluate, depending on a condition.
An if-expressionchooses from twosubexpressions, using
a boolean condition.
A case-expressionchooses fromseveralsubexpressions.
Conditional expressions are commonplace in functional
PLs, but less common in imperative/OO PLs.
8/12/2019 Values and storages
11/20
2-11
Example: Java if-expressions
Java if-expression:
x>y ? x : y
Conditional expressions tend to be more elegant thanconditional commands. Compare:
intmax1 (intx, inty) {return(x>y ? x : y);
}
intmax2 (intx, inty) {if(x>y)
returnx;else
returny;}
8/12/2019 Values and storages
12/20
2-12
Example: Haskell if- and case-expressions
Haskell if-expression:
ifx>y thenx elsey
Haskell case-expression:
casem offeb -> ifisLeap y then29 else28
apr -> 30
jun -> 30
sep -> 30
nov -> 30_ -> 31
8/12/2019 Values and storages
13/20
2-13
Iterative expressions
An iterative expressionis one that performs a
computation over a series of values (typically the
components of an array or list), yielding some result.
Iterative expressions are uncommon, but they aresupported by Haskell in the form of list comprehensions.
8/12/2019 Values and storages
14/20
2-14
Implementation notes
Values and types are mathematical abstractions.
In a computer, each value is represented by a bit sequencestored in one or more bytes or words.
Important principle: all values of the same type must berepresented in a uniform way. (But values of differenttypes can be represented in different ways.)
Sometimes the representation of a type is PL-defined (e.g.,
Java primitive types). More commonly, the representation is implementation-
defined, i.e., chosen by the compiler.
8/12/2019 Values and storages
15/20
2-15
Representation of primitive types (1)
Each primitive type Tis typically represented by single or
multiple bytes: usually 8 bits, 16 bits, 32 bits, or 64 bits.
The choice of representation is constrained by the types
cardinality, #T: With nbits we can represent at most 2ndifferent values.
8/12/2019 Values and storages
16/20
2-16
Representation of primitive types (2)
Booleanscan in principle be represented by a single bit (0forfalseand 1 for true). In practice, the compiler is likelyto choose a whole byte.
Charactershave a representation determined by the
character set: ASCII or ISO-Latin characters have an 8-bit representation
Unicode characters have a 16-bit representation.
8/12/2019 Values and storages
17/20
2-17
Representation of primitive types (3)
Integershave a representation influenced by the desiredrange. Assuming twos complement representation, in n
bits we can represent the integers {2n1, , 2n11}:
In a PL where the compilergets to choose the number of bits n,from that we can deduce the range of integers.
In a PL where theprogrammerdefines the range of integers, thecompiler must use that range to determine the minimum n. In
practice the compiler is likely to choose 64 bits.
Real numbershave a representation influenced by the
desired range and precision. Nowadays most compilersadopt the IEEE floating-point standard (either 32 or 64
bits).
8/12/2019 Values and storages
18/20
2-18
Representation of Cartesian products
jan
1
2000
dec
25
2004
m
d
y
Tuples, records, and structures are represented by
juxtaposing the components in a fixed order.
Example (Ada):
typeDate isrecordy: Year_Number;
m: Month;
d: Day_Number;
endrecord;
Implementation of component selection:
Let rbe a record or structure.
Each component r.fhas a fixed offset (determined by thecompiler) relative to the base address of r.
8/12/2019 Values and storages
19/20
2-19
Representation of arrays
The values of an array type are represented by juxtaposing
the components in ascending order of indices.
Example (Ada):
typeVector isarray(1 .. 3) ofFloat;
3.0
4.0
0.0
1
3
2
1.0
1.0
0.5
8/12/2019 Values and storages
20/20
2-20
Representation of objects (simplified)
Example (Java):
classPoint {privatefloatx, y;
// methods}
classCircleextendsPoint {
privatefloatr; // methods
}
classRectangleextendsPoint {
privatefloatw, h; // methods
}
tagPoint
x1.0
2.0 yCirclex0.0
0.0 y
r5.0
tag
Rect.
x1.5
2.0 y
w3.0
4.0 h
tag