



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Spring 09 CSCI 4430, A Milanova/BG Ryder 1
Spring 09 CSCI 4430, A Milanova/BG Ryder 2
Spring 09 CSCI 4430, A Milanova/BG Ryder 3
A set of values and the valid operations on those values E.g., Integers: +, -, *, /, <, <=, ==, >=, > Provide semantic correctness checking
A mechanism to define types and associate them with certain language constructs Type expressions Type judgments (Complex) algorithm for associating types with constructs Additional rules for type equivalence, type compatibility
Spring 09 CSCI 4430, A Milanova/BG Ryder 4
Term applied to (mostly) statically typed languages Scott: A language is said to be strongly typed if the type system prohibits application of an operation on an object that is not intended to support that operation Cardelliโs type terminology: Forbidden error: the occurrence of one of a predetermined class of execution errors (e.g., logical-not applied to 3) A language is said to be strongly typed if it prohibits the execution of one of the predetermined forbidden errors
5
void x = (void ) new A; B q = (B ) x; // a safe downcast? int case3 = q->foo(66);
C++ (weakly typed): q->foo(66) executesโฆ Java (strongly typed): q.foo(66) would never execute
A
B
foo()
foo() foo(int)
Spring 09 CSCI 4430, A Milanova/BG Ryder 6
a[i] Weak typing โ C++ 0<=i and **i Spring 09 CSCI 4430, A Milanova/BG Ryder 7
Typically require type annotations (e.g., A a, List l) Type checking is performed (mostly) statically (that is, at compile-time) Ada, Pascal, Java, C++ A form of early binding
Typically require no type annotations! All type checking is performed dynamically (at run-time) Smalltalk, Lisp and Scheme, Python A form of late binding
8
Spring 09 CSCI 4430, A Milanova/BG Ryder 9
Type equivalence Type compatibility
Structures and variants Arrays Strings
Spring 09 CSCI 4430, A Milanova/BG Ryder 10
e := expression Are e and expression of same type?
a + b Are a and b of types that support +?
foo(arg1, arg2, โฆ,argN) Do the types of the arguments match the types of the formal parameters?
Spring 09 CSCI 4430, A Milanova/BG Ryder 11
Roughly, two types are the same if they have the same components
Roughly, each type definition introduces a new type
Spring 09 CSCI 4430, A Milanova/BG Ryder 12
19
C uses structural equivalence for everything except unions and structs, for which it uses name equivalence
*struct A struct B { char x; { char x; int y; int y; } } typedef struct A C; typedef C P; typedef struct B Q; typedef struct A R; typedef int Age; int (F) (int); Age (G) (Age); Spring 09 CSCI 4430, A Milanova/BG Ryder^20
int main(){
**struct B { char x; int y;}; typedef struct B A; struct { A a; A *next;} aa; struct { struct B a; struct B next;} bb; struct { struct B a; struct B next;} cc;
A a; struct B b; a = b; aa = bb; bb = cc; }
Spring 09 CSCI 4430, A Milanova/BG Ryder 21
double d,e;โฆ e=d+2; //2 coerced to 2.
integer โ double, float โ double But real โ int may lose precision and therefore cannot be coerced!
Spring 09 CSCI 4430, A Milanova/BG Ryder 22
In Pascal, can explicitly convert types which may lose precision (narrowing) round(s) real โ int by rounding trunc(s) real โ int by truncating In C, type casting typically is explicit conversion dqstr((double) n) where n is declared to be an int **freelist s; ... (char )s; forces s to be considered as pointing to a char for the purposes of pointer arithmetic
Spring 09 CSCI 4430, A Milanova/BG Ryder 23
An operator allows operands of more than one type, in different contexts
Addition operator Integer addition: 2+3 is 5 Real addition: 2.+3. is 5. String concatenation: โabcโ+โdefโ is โabcdefโ Comparison operator Integer comparison: 2 == 3 String comparison: โabcโ == โdefโ
Spring 09 CSCI 4430, A Milanova/BG Ryder 24
Length fixed by standards or implementation (portability issues) Multiple lengths (C: short, int, long) Signs
Should value comparison be allowed? Representation: sign(1 bit)/mantissa(23 bits)/exponent(8 bits)
Spring 09 CSCI 4430, A Milanova/BG Ryder 25
Selection through field names (s.num, p->next) Assignment Example: structures in C *typedef struct cell listcell; struct cell { int num; listcell next; } s,t; s.num = 0; s.next = 0; t = s; Spring 09 CSCI 4430, A Milanova/BG Ryder 26
order/type of fields name/type of fields order/name/type of fields
Successive memory locations at offset from first byte Sometimes packing needed for alignment typedef struct { char name[10]; int age; } Person;
Person p;
name
age
4 bytes/32 bits
โholesโ
Spring 09 CSCI 4430, A Milanova/BG Ryder 27
Fortran: equivalence Algol68 and C: unions Pascal: variant records
Pascal and C are not type-safe Algol68 is type-safe! Uses run-time checks
Mutually exclusive value access
Spring 09 CSCI 4430, A Milanova/BG Ryder 28
union data { int k; char c; } d1,d2; Operations Assignment d1.k=3; d2=d1; d2.c=โbโ; Type checking if (n>0) d1.k=5 else d1.c=โaโ; if (d1.k<10)โฆ/meaningful?
Spring 09 CSCI 4430, A Milanova/BG Ryder 29
program main(input,output); type paytype = (salaried,hourly); var employee : record id : integer; dept: integer; age : integer; case payclass: paytype of salaried: (monthlyrate real; startdate : integer); hourly: (rateperhour : real; reghours : integer; overtime : integer); end;
begin employee.id:=001234; employee.dept:=12; employee.age:=38; employee.payclass:=hourly; employee.rateperhour:= 2.75; employee.reghours := 40; employee.overtime:=3; writeln(employee.rateperhour, employee.reghours, employee.overtime); {this should bomb as there is no monthlyrate because payclass=hourly} writeln(employee.monthlyrate);
Type tag
Output: 2.750000E+00 40 3 2.750000E+00 Spring 09 CSCI 4430, A Milanova/BG Ryder^30
type paytype = (salaried,hourly); var employee : record id : integer; dept: integer; age : integer; case payclass: paytype of salaried:( monthlyrate :real; startdate : integer); hourly: ( rateperhour : real; reghours : integer; overtime : integer); end;
employee.payclass:=salaried; employee.monthlyrate:=575.0; employee.startdate:=13085; {this should bomb as there are no rateperhour, etc. because payclass=salaried} writeln(employee.rateperhour, employee.reghours employee.overtime); writeln(employee.monthlyrate); end.
Output: 5.750000E+02 13085 3 5.750000E+
Spring 09 CSCI 4430, A Milanova/BG Ryder 37
Row-major order and column-major order
y[0,0], y[0,1], y[0,2], โฆ, y[1,], y[2,],โฆ**
addr( y[low1,low2] ) + ( hi2-low2 +1)E( j-low1 )+( k-low2 )*E #locs per row #rows in front # elements in row j in of row j front of element [j,k] Spring 09 CSCI 4430, A Milanova/BG Ryder 38
Consider y[0:2, 0:5] int matrix. Assume row-major order and find the address of y[1,3]. address of y[1,3] = addr( y[0,0] )+(5-0+1)4(1-0)+(3-0)* 6 elements per row 1 row before row 1 3 elements in row 1 before 3 = addr( y[0,0] )+24+ = addr( y[0,0] )+
Spring 09 CSCI 4430, A Milanova/BG Ryder 39
Spring 09 CSCI 4430, A Milanova/BG Ryder 40
Can be declared with a maximum length Can have unlimited length (Algol68)
โagoโ|โbeโ โโโโ โagobeโ, concatenation index(โabcโ,โxyabcdโ) โโโโ 2, returns start position in 2nd string argument of the 1st string argument substr(a,j,k) returns substring of a starting at position j of k characters in length
Spring 09 CSCI 4430, A Milanova/BG Ryder 41
Max length fixed at compile-time packed array[1..n] of char Used in assignments and relational compares Operations: pack and unpack to get at individual chars with function call overhead
Length is number of charโs contained ( strlen ) strcpy(), strcat(), strstr(), etc.