Anda di halaman 1dari 17

Values and Types

Types of values. Primitive, composite types.

Functions as mappings.
Java Objects , Strings.

2-1

Types (1)
Values are grouped into types according to the operations that may be performed on them. Different PLs support different types of values (according to their intended application areas):
Ada: booleans, characters, enumerands, integers, real numbers, records, arrays, discriminated records, objects (tagged records), strings, pointers to data, pointers to procedures.
C: enumerands, integers, real numbers, structures, arrays, unions, pointers to variables, pointers to functions. Java: booleans, integers, real numbers, arrays, objects. Haskell: booleans, characters, integers, real numbers, tuples, disjoint unions, lists, recursive types.
2-2

Types (2)
Roughly speaking, a type is a set of values:
v is a value of type T if v T.

E is an expression of type T if E is guaranteed to yield a value of type T.

But only certain sets of values are types:


{false, true} is a type, since the operations not, and, and or operate uniformly over the values false and true.

{, 2, 1, 0, +1, +2, } is a type, since operations such as addition and multiplication operate uniformly over all these values.
{13, true, Monday} is not considered to be a type, since there are no useful operations over this set of values.
2-3

Types (3)
More precisely, a type is a set of values, equipped with one or more operations that can be applied uniformly to all these values. The cardinality of a type T, written #T, is the number of values of type T.

2-4

Primitive types
A primitive value is one that cannot be decomposed into simpler values. A primitive type is one whose values are primitive. Every PL provides built-in primitive types. Some PLs also allow programs to define new primitive types.

2-5

Built-in primitive types (1)


Typical built-in primitive types:
Boolean = {false, true}
Character = {, A, , Z, , 0, , 9, } Integer Float = {, 2, 1, 0, +1, +2, } = {, 1.0, , 0.0, +1.0, }

PL- or implementation-defined set of characters


PL- or implementation-defined set of whole numbers PL- or implementation-defined set of real numbers

Names of types vary from one PL to another: not significant.


2-6

Built-in primitive types (2)


Cardinalities:
#Boolean
#Integer

= 2
= max integer min integer + 1

Note: In some PLs (such as C), booleans and characters are just small integers.

2-7

Example: Ada numerics


Type declaration:
type Population is range 0 .. 1e10;

Set of values:
Population = {0, 1, , 1010}

Cardinality:
#Population = 1010+1

2-8

Composite types
A composite value is one that is composed from simpler values. A composite type is a type whose values are composite. PLs support a huge variety of composite types. All these can be understood in terms of a few concepts:
Cartesian products (tuples, structures, records)

mappings (arrays)
disjoint unions (algebraic data types, discriminated records, objects) recursive types (lists, trees, etc.)
2-9

Cartesian products
In a Cartesian product, values of several types are grouped into tuples. Let (x, y) stand for the pair whose first component is x and whose second component is y.

Let S T stand for the set of all pairs (x, y) such that x is chosen from set S and y is chosen from set T:
S T = { (x, y) | x S; y T }

Cardinality:
#(S T) = #S #T

2-10

Mappings
We write m : S T to state that m is a mapping from set S to set T. In other words, m maps every value in S to some value in T. If m maps value x to value y, we write y = m(x). The value y is called the image of x under m.

2-11

Functions as mappings
Functions (found in all PLs) can also be understood as mappings. A function maps its argument(s) to its result. If a function has a single argument of type S and its result is of type T, the functions type is S T.

2-12

Example: Ada functions


Definition:
function is_even (n: Integer) return Boolean is begin return (n mod 2 = 0); end;

or any other code that achieves the same effect

Type:
Integer Boolean

Value:
{, 0 true, 1 false}

Other functions of same type: is_odd, is_prime, etc.


2-13

Example: Java objects (1)


Type declarations:
class Point { private float x, y; // methods } class Circle extends Point { private float r; // methods } class Rectangle extends Point { private float w, h; // methods }
inherits x and y from Point

inherits x and y from Point

2-14

Example: Java objects (2)


Set of objects in this program:
Point(Float Float) + Circle(Float Float Float) + Rectangle(Float Float Float Float) +

The set of objects is open-ended. It is augmented by any further class declarations.

2-15

Example: Java objects (3)


Methods:
class Point { public float area() { return 0.0; } }
class Circle extends Point { public float area() { return 3.1416 * r * r; } }

overrides Points area() method

class Rectangle extends Point { overrides Points area() method public float area() { return w * h; } 2-16 }

Strings
A string is a sequence of 0 or more characters.

Some PLs (ML, Python) treat strings as primitive.


Haskell treats strings as lists of characters. Strings are thus equipped with general list operations (length, head selection, tail selection, concatenation, ). Ada treats strings as arrays of characters. Strings are thus equipped with general array operations (length, indexing, slicing, concatenation, ). Java treats strings as objects, of class String.

2-17

Anda mungkin juga menyukai