A Type is a Set - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

A Type is a Set

Description:

Any type that a program can use but cannot define for itself is a ... unsigned statics : 1; int value; unsigned ascii : 7; char eascii; } flag1, flag2; 1 ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 41
Provided by: adam242
Category:
Tags: set | statics | type

less

Transcript and Presenter's Notes

Title: A Type is a Set


1
A Type is a Set
Chapter 6 Types
int n
  • A set of values
  • plus a low-level representation
  • plus a collection of operations on those values

2
  • Issues of types
  • Type annotations (how to declare)
  • Type inference (how to determine)
  • Type checking (when to check)
  • Type equivalence issues (what is it)

3
Primitive vs. Constructed Types
  • Primitive type can be used but cannot be
    defined.
  • ML int, real, char
  • Constructed type can be defined in a program
  • ML int intpait int int

4
Primitive Types
  • Every programming language defines its primitive
    types
  • Some define the primitive types precisely for
    implementation (e.g., Java)
  • Some leave it loose for implementation (e.g., C,
    ML)

5
Integral Types in different PL
Ccharunsigned charshort intunsigned short
intintunsigned intlong intunsigned long
int No standard implementation, but longer sizes
must provide at least as much range as shorter
sizes.
Javabyte (1-byte signed)char (2-byte
unsigned)short (2-byte signed)int (4-byte
signed)long (8-byte signed)
SchemeintegerIntegers of unbounded range
6
Issues of primitive types
  • What sets of values?
  • How much is part of the language specification,
    how much left up to the implementation?
  • If necessary, how can a program find out?
    (INT_MAX in C, Int.maxInt in ML, etc.)
  • What operations are supported?
  • Detailed definitions rounding, exceptions, etc.
  • The choice of representation is a critical part
    of these decisions

7
Constructed Types
  • enumerations
  • tuples
  • arrays
  • strings
  • lists
  • unions
  • subtypes
  • function types

Example
Each has a connection to a set operation in
mathematics
8
Making Sets by Enumeration
  • We can construct sets by just listing all the
    elements

C enum coin penny, nickel, dime,
quarter Ada type GENDER is (MALE,
FEMALE) Pascal type primaryColors (red,
green, blue) ML datatype day M Tu W Th
F Sa Su
  • These define a new type ( set)
  • They also define a collection of named constants
    of that type ( elements)

9
Representing Enumeration Values
  • A common representation is to treat the values of
    an enumeration as small integers

They are countable!!
enum coin penny 1, nickel 5, dime 10,
quarter 25 enum escapes BELL '\a',
BACKSPACE '\b', TAB '\t',
NEWLINE '\n', VTAB '\v', RETURN '\r'
10
Operations on Enumeration Values
  • Equality test
  • If using integer to represent elements then
    elements are legitimate for all integer
    operations

fun isWeekend x (x Sa orelse x Su)
Pascal for C red to blue do P(C) C int x
penny nickel dime
11
Making Sets by Tupling
  • The Cartesian product of two or more sets defines
    tuples
  • Some languages support pure tuples

fun get1 (x real real) 1 x
12
Making Types by Tupling
  • Many PLs support record types tuples with named
    fields

C struct complex double rp double ip
x
ML type complex rpreal, iprealfun
getip (x complex) ip x
Operations on tuples (selection)
C x.ipML ip x
13
Representing Tuple Values
  • A common representation is to just place the
    elements side-by-side in memory
  • But there are lots of details
  • in what order?
  • with holes to align elements (e.g. on word
    boundaries) in memory?
  • Are these details visible to the programmer?

14
ANSI C
The members of a structure have addresses
increasing in the order of their declarations. A
non-field member of a structure is aligned at an
addressing boundary depending on its type
therefore, there may be unnamed holes in a
structure. If a pointer to a structure is cast
to the type of a pointer to its first member, the
result refers to the first member
The C Programming Language, 2nd ed. Brian W.
Kernighan and Dennis M. Ritchie
flag1
struct unsigned keywork 1 unsigned
external 1 unsigned statics 1 int
value unsigned ascii 7 char
eascii flag1, flag2
flag2
15
Sets of Vectors
  • Fixed-size vectors
  • Arbitrary-size vectors
  • In PLs arrays, strings, lists
  • Issues
  • What are the index values?
  • Is the array size fixed at compile time?

16
Index values for array
  • Java, C, C
  • First element of an array a is a0
  • Indexes are always integers starting from 0
  • Pascal is more flexible
  • integers, characters, enumerations, subranges
  • Starting/Ending index chosen by the programmer
  • size is fixed at compile time

type LetterCount array'a'..'z' of
Integervar Counts LetterCount begin
Counts'a' 1
17
  • Array designers need to decide
  • What are the index values?
  • Is array size fixed at compile time (static
    type)?
  • What operations are supported?
  • Are multiple dimensions allowed?
  • Is re-dimensioning possible at runtime?
  • Is a higher-dimensional array the same as an
    array of arrays?
  • What is the order of elements in memory?
  • Is there a separate type for strings (not just
    array of characters)?
  • Is there a separate type for lists?

18
Making Types by Union
Making Sets by Union
ML datatype element I of int F of real
C union element int i float f
19
Representing Union Values
  • two representations overlap each other in memory
  • This representation may or may not be exposed to
    the programmer

union element int i char p u /
sizeof(u) is / /
max(sizeof(u.i),sizeof(u.p)) /
20
Loosely Typed Unions
  • Some languages expose the details of union
    implementation (i.e., you know how)

union element int i float f e
f
i
float x int n e.i100 e.f1.0x e.f n
e.i
e
No type casting is involved
21
Strictly Typed Unions
datatype element I of int F of real
  • In ML, an element type x is either I or F, thus,
    you have to say what to do with each type of
    value in the union

fun getReal (F x) x getReal (I x) real x
Type casting is involved
22
Variant Records
  • Union where specific type is determined by to the
    value of a field (discriminated union)
  • Ada and Modula-2

C I dont care what is it. ML I have to
know what is it in advance Ada Lets see what
is it.
23
Ada Variant Record Example
type DEVICE is (PRINTER, DISK)type
PERIPHERAL(Unit DEVICE) is record
HoursWorking INTEGER case Unit is
when PRINTER gt Line_count INTEGER
when DISK gt Cylinder INTEGER
Track INTEGER end case end record
24
Subsets
  • We can define the subset selected by any
    predicate P

S
X
25
Making Subtypes
  • Less general Pascal subranges
  • type digit 0..9
  • More general Ada subtypes
  • subtype DIGIT is INTEGER range 0..9 subtype
    WEEKDAY is DAY range MON..FRI
  • Most general Lisp types with predicates

26
Ada Subtypes
type DEVICE is (PRINTER, DISK)type
PERIPHERAL(Unit DEVICE) is record
HoursWorking INTEGER case Unit is
when PRINTER gt Line_count INTEGER
when DISK gt Cylinder INTEGER
Track INTEGER end case end
record subtype DISK_UNIT is PERIPHERAL(DISK)
27
Types with Predicates in Lisp
(declare (type integer x)) (declare (type (or
null cons) x)) (declare (type (and number (not
integer)) x)) (declare (type (and integer
(satisfies evenp)) x))
28
Representing Subtype Values
  • Usually, we just use the same representation for
    the subtype as for the supertype
  • Questions
  • Do we want to shorten it if we can?
  • I.e., does X 1..9 take the same space as X
    Integer?
  • Do we enforce the subtyping?
  • Is X 10 legal? What about X X 1?

29
  • Type is a key idea of OOP
  • A class is a type data and operations on that
    data, bundled together
  • A subclass is a subtype it includes a subset of
    the objects, but supports a superset of the
    operations

30
Making Sets of Functions
A set can be a collection of functions
  • define a set of functions with given domain and
    range

31
Making Types of Functions
  • Most languages have some notion of the type of a
    function

int f(char a, char b) return ab
C
fun f(achar, bchar) (a b)
ML
32
Operations on Function Values
  • call the function
  • bound to variables
  • passed as parameters
  • Treated as value

Many languages support nothing beyond function
call (What can C do?)
ML supports many operations
33
Type Annotations
  • The syntax for describing types
  • Some languages use naming conventions to declare
    the types of variables
  • BASIC S is a string
  • Fortran I is an integer
  • Like explicit annotations, these conventions
    provide static type information

34
Type Inference
Determining the types
  • Constants usually have static types
  • Java 10 has type int, 10L has type long
  • Expressions may have static types inferred from
    operators and types of operands
  • Java if a is double, a0 is double (0.0)

Simple
  • Infers a static type for every expression and for
    every function, usually requires no annotations

Complicate
35
Static Type Checking
  • Static type checking determines a type for
    everything before running the program variables,
    functions, expressions, everything
  • Compile-time error messages when static types are
    not consistent
  • Operators 1"abc"
  • Functions round("abc")
  • Statements if "abc" then
  • Most modern languages are statically typed

36
Dynamic Typing
  • In some languages, programs are not statically
    type-checked before being run. they are
    dynamically type-checked during the runtime.
  • At runtime, the language system checks that
    operands are of suitable types for operations

37
Example Lisp
(defun f (a b) ( a b))
  • It wont work if a or b is not a number
  • An improper call, like (f nil nil), is not caught
    at compile time
  • It is caught at runtime that is dynamic typing

38
Explicit Runtime Type Tests
  • Some languages allow explicit runtime type tests
  • Java test object type with instanceof operator
  • Type information is known at runtime, even when
    the language is mostly statically typed

39
Strong Typing ?? Weak Typing
  • The purpose of type-checking is to prevent the
    application of operations to incorrect types of
    operands
  • ML and Java, the type-checking is thorough enough
    to guarantee thisthats strong typing
  • C has holes in the type system that add
    flexibility but weaken the guarantee

40
Type Equivalence
type irpair1 int realtype irpair2 int
realfun f(xirpair1) 1 x
  • What happens if apply f to an irpair2?
  • Name equivalence does not permit this irpair2
    and irpair1 are different names
  • Structural equivalence does permit this, since
    the types are constructed identically
  • ML uses Structural equivalence
Write a Comment
User Comments (0)
About PowerShow.com