type systems cse 340 – principles of programming languages fall 2015 adam doupé arizona state...
DESCRIPTION
Adam Doupé, Principles of Programming Languages Type Declaration Programming language will typically include –Basic types Included in the programming language and available to any program written in that language –Type constructors Way for a programmer to define new types 3TRANSCRIPT
![Page 1: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/1.jpg)
Type Systems
CSE 340 – Principles of Programming LanguagesFall 2015
Adam DoupéArizona State Universityhttp://adamdoupe.com
![Page 2: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/2.jpg)
2Adam Doupé, Principles of Programming Languages
Type Systems• Informally, a type in a programming language
specifies a set of values and operations that can be applied on those values– A type can be associated with a variables or a constant– Values are not necessarily numeric values, for example
we can specify function types• A type system consists of
– Basic types– Type constructors– Type inference– Type compatibility
![Page 3: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/3.jpg)
3Adam Doupé, Principles of Programming Languages
Type Declaration
• Programming language will typically include– Basic types
• Included in the programming language and available to any program written in that language
– Type constructors• Way for a programmer to define new types
![Page 4: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/4.jpg)
4Adam Doupé, Principles of Programming Languages
Type Constructors
• Pointer to T, where T is a type• struct { a1: T1; a2 : T2; …, ak: Tk; }
– Where ai is a field name and Ti is a previously defined type
• array range of T– Where range can be single or multi dimensional
• function of T1, T2, ..., Tk returns T– Type is a function, the types of the parameters are
T1 ... Tk and the return type is T
![Page 5: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/5.jpg)
5Adam Doupé, Principles of Programming Languages
Using Type Constructors
• Declaring TypesType cm : integer;Type RGBA : array [0..4] of int;Type png : array [0..256] of RGBA;
• Anonymous Typesarray [0..4] of int x;struct {int a;char b;} y;
![Page 6: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/6.jpg)
6Adam Doupé, Principles of Programming Languages
Type Compatibility
• Which assignments are allowed by the type system?– a = b;?– int a; float b;– float a; int b;
![Page 7: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/7.jpg)
7Adam Doupé, Principles of Programming Languages
Type Inference
• Types of expressions or other constructs as a function of subexpression types– a + b
• a int; b float – Returns a float in C– Error in ML
– a * b• a string; b int
– Error in most languages– Returns a string in Python
![Page 8: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/8.jpg)
8Adam Doupé, Principles of Programming Languages
Type Compatibility
• Principally about type equivalence– How to determine if two types are equal?
Type cm : integer;Type inch : integer;cm x;inch y;x = y?
![Page 9: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/9.jpg)
9Adam Doupé, Principles of Programming Languages
Name Equivalence
• Types must have the exact same name to be equivalent
Type cm : integer;Type inch : integer;cm x;inch y;x = y?// ERROR
![Page 10: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/10.jpg)
10Adam Doupé, Principles of Programming Languages
Name Equivalence
a: array [0..4] of int;b: array [0..4] of int;
• a = b?– Not allowed under name equivalence
![Page 11: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/11.jpg)
11Adam Doupé, Principles of Programming Languages
Name Equivalence
a, b: array [0..4] of int;
• a = b?– Not allowed because array [0..4] of int is not
named
![Page 12: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/12.jpg)
12Adam Doupé, Principles of Programming Languages
Name Equivalence
Type A: array [0..4] of int;a: A;b: A;
• a = b?– Allowed, because both a and b have the
same name
![Page 13: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/13.jpg)
13Adam Doupé, Principles of Programming Languages
Internal Name Equivalence• If the program interpreter gives the same internal name to
two different variables, then they share the same type
a, b: array [0..4] of int;c: array [0..4] of int;
• a = b?– Yes, because interpreter/compiler gives the same internal name
to a and b• a = c?
– No, because interpreter/compiler gives different internal name to c than to a and b
![Page 14: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/14.jpg)
14Adam Doupé, Principles of Programming Languages
Structural Equivalence
1. Same built-in types2. Pointers to structurally equivalent typesType cm : integer;Type inch : integer;cm x;inch y;x = y? // Allowed!
![Page 15: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/15.jpg)
15Adam Doupé, Principles of Programming Languages
Structural Equivalence
int* a;float* b;
• a = b?– Not structurally equivalent, because int and
float are not structurally equivalent
![Page 16: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/16.jpg)
16Adam Doupé, Principles of Programming Languages
Structural Equivalence
3. Determining struct structural equivalence– Two structures– st1 { x1: W1, x2: W2, …, xk: Wk }– st2 { y1: Q1, y2: Q2, ..., yk: Qk }– st1 and st2 are structurally equivalent iff
• W1 structurally equivalent to Q1
• W2 structurally equivalent to Q2
• ...• Wk structurally equivalent to Qk
![Page 17: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/17.jpg)
17Adam Doupé, Principles of Programming Languages
Structural Equivalence
struct A { a: int, b: float }struct B { b: int, a: float }
A foo;B bar;• foo = bar?
![Page 18: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/18.jpg)
18Adam Doupé, Principles of Programming Languages
Structural Equivalence
struct A { a: int, b: float }struct B { b: float, a: int }
A foo;B bar;• a = b?
![Page 19: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/19.jpg)
19Adam Doupé, Principles of Programming Languages
Structural Equivalence
4. Determining array structural equivalence– Two Arrays– T1 = array range1 of t1
– T2 = array range2 of t2
– T1 and T2 are structurally equivalent iff:• range1 and range2 have (1) the same number of
dimensions and (2) the same number of entries in each dimension
• t1 and t2 are structurally equivalent
![Page 20: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/20.jpg)
20Adam Doupé, Principles of Programming Languages
Structural Equivalence
5. Determining function structural equivalence
– Two functions– T1 = function of (t1, t2, t3, …, tk) returns t– T2 = function of (v1, v2, v3, ..., vk) returns v– T1 and T2 are structurally equivalent iff:
• For all i from 1 to k, ti is structurally equivalent to vi
• t is structurally equivalent to v
![Page 21: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/21.jpg)
21Adam Doupé, Principles of Programming Languages
Determining Structural Equivalence
• The goal is to determine, for every pair of types in the program, if they are structurally equivalent
• Seems fairly simple, just keep applying the previous 5 rules until the base case 1 or 2 is reached
• How to handle the following case:T1 = struct { a: int; p: pointer to T2; }T2 = struct { a: int; p: pointer to T1; }
• Applying the rules states that T1 is structurally equivalent to T2 iff pointer to T1 is structurally equivalent to pointer to T2, which is true if T1 is structurally equivalent to T2
![Page 22: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/22.jpg)
22Adam Doupé, Principles of Programming Languages
Structural Equivalence Algorithm
• The way to break the stalemate is to assume that T1 and T2 are structurally equivalent because no rule contradicts them being structurally equivalent
• Our goal is to create an n X n table, where n is the number of types in the program, and each entry in the table is true if the types are structurally equivalent and false otherwise
![Page 23: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/23.jpg)
23Adam Doupé, Principles of Programming Languages
Structural Equivalence Algorithm
• To support cyclical definitions, we first initialize all entries in the table to true– We assume that types are structurally equivalent unless
we have proof otherwise• Algorithm is fairly simple
– Set the n X n table to have each entry as true– While table has not changed
• Check each entry i, j in the table, and if Ti and Tj are not structurally equivalent, then set the entry i, j in the table to false
• Note that Ti and Tj are the ith and jth types in the program
![Page 24: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/24.jpg)
25Adam Doupé, Principles of Programming Languages
T1 = struct { a: int, p: pointer to T2 }T2 = struct { c: int, q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }
T1 T2 T3T1 true true trueT2 true true trueT3 true true true
![Page 25: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/25.jpg)
26Adam Doupé, Principles of Programming Languages
T1 = struct { a: int, p: pointer to T2 }T2 = struct { c: int, q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }
T1 T2 T3T1 true true trueT2 true true trueT3 true true true
![Page 26: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/26.jpg)
27Adam Doupé, Principles of Programming Languages
T1 = struct { a: int, p: pointer to T2 }T2 = struct { c: int, q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }
T1 T2 T3T1 true true trueT2 true true trueT3 true true true
![Page 27: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/27.jpg)
28Adam Doupé, Principles of Programming Languages
T1 = struct { a: int, p: pointer to T2 }T2 = struct { c: int, q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }
T1 T2 T3T1 true true falseT2 true true trueT3 true true true
![Page 28: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/28.jpg)
29Adam Doupé, Principles of Programming Languages
T1 = struct { a: int, p: pointer to T2 }T2 = struct { c: int, q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }
T1 T2 T3T1 true true falseT2 true true trueT3 false true true
![Page 29: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/29.jpg)
30Adam Doupé, Principles of Programming Languages
T1 = struct { a: int, p: pointer to T2 }T2 = struct { c: int, q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }
T1 T2 T3T1 true true falseT2 true true trueT3 false true true
![Page 30: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/30.jpg)
31Adam Doupé, Principles of Programming Languages
T1 = struct { a: int, p: pointer to T2 }T2 = struct { c: int, q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }
T1 T2 T3T1 true true falseT2 true true falseT3 false true true
![Page 31: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/31.jpg)
32Adam Doupé, Principles of Programming Languages
T1 = struct { a: int, p: pointer to T2 }T2 = struct { c: int, q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }
T1 T2 T3T1 true true falseT2 true true falseT3 false false true
![Page 32: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/32.jpg)
33Adam Doupé, Principles of Programming Languages
T1 = struct { a: int, p: pointer to T2 }T2 = struct { c: int, q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }
T1 T2 T3T1 true true falseT2 true true falseT3 false false true
![Page 33: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/33.jpg)
34Adam Doupé, Principles of Programming Languages
T1 = struct { a: int, p: pointer to T2 }T2 = struct { c: int, q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }
T1 T2 T3T1 true false falseT2 true true falseT3 false false true
![Page 34: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/34.jpg)
35Adam Doupé, Principles of Programming Languages
T1 = struct { a: int, p: pointer to T2 }T2 = struct { c: int, q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }
T1 T2 T3T1 true false falseT2 false true falseT3 false false true
![Page 35: Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University](https://reader036.vdocument.in/reader036/viewer/2022062905/5a4d1b097f8b9ab0599897f9/html5/thumbnails/35.jpg)
36Adam Doupé, Principles of Programming Languages
T1 = struct { a: int, p: pointer to T2 }T2 = struct { c: int, q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }
T1 T2 T3T1 true false falseT2 false true falseT3 false false true