06/18/22 IT 327 1 A Type is a Set 1. A set of values 2. A low-level representation 3. A collection of operations on those values int n; Chapter 6: Types Bertrand Russell (1872 – 1970) (1910 ) Alfred N. Whitehead (1861 – 1947) Required for programming languages, so it is more restricted.
41
Embed
10/31/2015IT 3271 A Type is a Set 1. A set of values 2. A low-level representation 3. A collection of operations on those values int n; Chapter 6: Types.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
04/21/23 IT 327 1
A Type is a Set
1. A set of values
2. A low-level representation
3. A collection of operations on those values
int n;
Chapter 6: Types
Bertrand Russell (1872 – 1970)
(1910)
Alfred N. Whitehead (1861 – 1947)
Required for programming languages, so it is more restricted.
04/21/23 IT 327 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?)
04/21/23 IT 327 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: type intpair = int * int
04/21/23 IT 327 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)
04/21/23 IT 327 5
Integral Types in different PL’s
C:charunsigned charshort intunsigned short intintunsigned intlong intunsigned long intNo standard implementation, but longer sizes must provide at least as much range as shorter sizes.
– 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 in these decisions
04/21/23 IT 327 7
Constructed Types
1. enumerations2. tuples3. arrays4. strings5. lists6. unions7. subtypes8. function types
Example:
Each has a connection to a set operation in mathematics
04/21/23 IT 327 8
Making Sets by Enumeration
We can construct sets by just listing all the elements:
},,{ cbaS
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)
04/21/23 IT 327 9
Representing Enumeration Values
A common representation is to treat the values of an enumeration as small integers
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;
04/21/23 IT 327 11
Making Sets by Tupling
The Cartesian product of two or more sets defines tuples:
YyXxyx
YXS
|,
Some language like ML supports pure tuples:
(i.e., fields without names)
fun get1 (x : real * real) = #1 x;
04/21/23 IT 327 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 = { rp:real, ip:real};
Operations on tuples: (selection)
C: x.ipML: #ip x
fun getip (x : complex) = #ip x;
04/21/23 IT 327 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?– Are there “holes” between elements (e.g. on
word boundaries) in memory?– Are these details visible to the programmer?
04/21/23 IT 327 14
What does C say about tuples (structs)?
Preface
C is a general-purpose programming language which features economy of expression, modern control flow and data structures, and a rich set of operators. C is not a “very high level” language, nor a “big” one, and is not specialized to any particular area of application. But its absence of restrictions and its generality make it more convenient and effective for many tasks than supposedly more powerful languages.
…..
The C Programming LanguageBrian W. Kernighan and Dennis M. Ritchie1978 edition
Let’s see what does C say about itself first.
04/21/23 IT 327 15
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.
struct { unsigned keyword : 1;
unsigned external : 1;unsigned statics : 1;
int value; unsigned ascii : 7; char eascii;} flag1, flag2;
– What are the index values?– Is the array size fixed at compile time?
What is the difference between this and tuples?
04/21/23 IT 327 17
Index values for array Java, C, C++:
– First element of an array a is a[0]– 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 Integer;var Counts: LetterCount;
begin Counts['a'] = 1 ………
04/21/23 IT 327 18
Array designers need to decide:
1. What are the index values?2. Is array size fixed at compile time (static type)?3. What operations are supported?4. Are multiple dimensions allowed?5. Is a higher-dimensional array the same as an array of
arrays?6. Is re-dimensioning possible at runtime?7. What is the order of elements in memory?8. Is there a separate type for strings (not just array of
characters)?9. Is there a separate type for lists?
04/21/23 IT 327 19
Making Types by Union
C:
union element { int i; float f;};
ML:
datatype element = I of int | F of real;
Making Sets by Union: YXS
04/21/23 IT 327 20
Representing Union Values
Two (or more) 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)) */
04/21/23 IT 327 21
Loosely Typed Unions Some languages expose the details of union implementation
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:
datatype element = I of int | F of real;
Type casting is involved
fun getReal (F x) = x | getReal (I x) = real x;
04/21/23 IT 327 23
Variant Records Union where specific type is dynamically
determined by the value of a field (“discriminated union”)
Ada and Modula-2C: I don’t care what is it.
ML: I have to know what is it in advance
Ada: Let’s see what is it.
04/21/23 IT 327 24
Ada Variant Record Example
type DEVICE is (PRINTER, DISK);
type PERIPHERAL(Unit: DEVICE) is record HoursWorking: INTEGER;
case Unit is when PRINTER => Line_count: INTEGER;
when DISK => Cylinder: INTEGER; Track: INTEGER;
end case; end record;
04/21/23 IT 327 25
Subsets
We can define the subset selected by any predicate P:
)(| xPXxS
S X
04/21/23 IT 327 26
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
04/21/23 IT 327 27
Ada Subtypestype DEVICE is (PRINTER, DISK);
type PERIPHERAL(Unit: DEVICE) is record HoursWorking: INTEGER; case Unit is when PRINTER => Line_count: INTEGER; when DISK => Cylinder: INTEGER; Track: INTEGER; end case; end record;