/
Type Systems Type Systems

Type Systems - PowerPoint Presentation

myesha-ticknor
myesha-ticknor . @myesha-ticknor
Follow
376 views
Uploaded On 2016-07-21

Type Systems - PPT Presentation

CSE 340 Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University http adamdoupecom Type Systems Informally a type in a programming language specifies a set of values and operations that can be applied on those values ID: 413752

struct int float pointer int struct pointer float type structurally equivalent equivalence true structural array types false table integer

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Type Systems" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Type Systems

CSE 340

– Principles of Programming

Languages

Fall 2015

Adam Doupé

Arizona State University

http://

adamdoupe.comSlide2

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 typesA type system consists ofBasic typesType constructorsType inferenceType compatibility

2Slide3

Type Declaration

Programming language will typically include

Basic types

Included in the programming language and available to any program written in that languageType constructorsWay for a programmer to define new types3Slide4

Type Constructors

Pointer to T, where T is a type

struct

{ a1: T1; a2 : T2; …, ak: T

k

; }

Where a

i

is a field name and T

i is a previously defined typearray range of TWhere range can be single or multi dimensionalfunction of T1, T2, ..., Tk returns TType is a function, the types of the parameters are T1 ... Tk and the return type is T

4Slide5

Using Type Constructors

Declaring Types

Type

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;

5Slide6

Type Compatibility

Which assignments are allowed by the type

system?

a = b;?int a; float b;float a; int b;

6Slide7

Type Inference

Types of expressions or other constructs as a function of

subexpression

typesa + ba int; b float Returns a float in CError in MLa * ba string; b int

Error in most languages

Returns a string in Python

7Slide8

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?

8Slide9

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

9Slide10

Name Equivalence

a: array [0..4] of

int

;b: array [0..4] of int;a = b?Not allowed under name equivalence

10Slide11

Name Equivalence

a, b:

array [0..4] of

int;a = b?Not allowed because array [0..4] of int is not named11Slide12

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

12Slide13

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

13Slide14

Structural Equivalence

Same built-in types

Pointers to structurally equivalent types

Type cm : integer;Type inch : integer;

cm x;

inch y;

x =

y?

// Allowed!

14Slide15

Structural Equivalence

int

* a;

float* b;a = b?Not structurally equivalent, because int and float are not structurally equivalent

15Slide16

Structural Equivalence

Determining

struct

structural equivalenceTwo structuresst1 { x1: W1, x2: W2

,

…, x

k

: W

k

}st2 { y1: Q1, y2: Q2, ..., yk: Qk }st1 and st2 are structurally equivalent iffW1 structurally equivalent to Q1

W

2

structurally equivalent to Q2...Wk structurally equivalent to Qk16Slide17

Structural Equivalence

struct

A { a:

int, b: float }struct B { b: int, a: float }A foo;B bar;foo = bar?

17Slide18

Structural Equivalence

struct

A { a:

int, b: float }struct B { b: float, a: int }

A foo;

B bar;

a = b?

18Slide19

Structural Equivalence

Determining array structural equivalence

Two Arrays

T1 = array range1 of t1T2 = array range2 of t2T1 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

t

1

and t

2

are structurally equivalent19Slide20

Structural Equivalence

Determining function structural equivalence

Two functions

T1 = function of (t1, t2, t3, …, tk

) returns t

T2 = function of (v

1

, v

2

, v3, ..., vk) returns vT1 and T2 are structurally equivalent iff:For all i from 1 to k, ti is structurally equivalent to vit is structurally equivalent to v20Slide21

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 T221Slide22

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

22Slide23

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 otherwiseAlgorithm is fairly simpleSet the n X n table to have each entry as trueWhile table has not changed

Check each entry

i

, j in the table, and if

T

i

and Tj are not structurally equivalent, then set the entry i, j in the table to falseNote that Ti and Tj are the ith and

j

th

types in the program23Slide24

T1 =

struct

{ a:

int; p: pointer to T2;

}

T2 =

struct

{ c:

int

; q: pointer to T3; }T3 = struct { a : float; p: pointer to T1; }

24

T1

T2

T3

T1

true

true

true

T2

true

true

true

T3

true

true

trueSlide25

T1 =

struct

{ a:

int, p: pointer to T2 }

T2 =

struct

{ c:

int

,

q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }

25

T1

T2

T3

T1

true

true

true

T2

true

true

true

T3

true

true

trueSlide26

T1 =

struct

{ a:

int, p: pointer to T2 }

T2 =

struct

{ c:

int

,

q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }

26

T1

T2

T3

T1

true

true

true

T2

true

true

true

T3

true

true

trueSlide27

T1 =

struct

{ a:

int, p: pointer to T2 }T2 =

struct

{ c:

int

,

q: pointer to T3 }

T3 = struct { a: float, p: pointer to T1 }27

T1

T2

T3

T1

true

true

true

T2

true

true

true

T3

true

true

trueSlide28

T1 =

struct

{ a:

int, p: pointer to T2 }

T2 =

struct

{ c:

int

,

q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }

28

T1

T2

T3

T1

true

true

false

T2

true

true

true

T3

true

true

trueSlide29

T1 =

struct

{ a:

int, p: pointer to T2 }

T2 =

struct

{ c:

int

,

q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }

29

T1

T2

T3

T1

true

true

false

T2

true

true

true

T3

false

true

trueSlide30

T1 =

struct

{ a:

int, p: pointer to T2 }

T2 =

struct

{ c:

int

,

q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }

30

T1

T2

T3

T1

true

true

false

T2

true

true

true

T3

false

true

trueSlide31

T1 =

struct

{ a:

int, p: pointer to T2 }

T2 =

struct

{ c:

int

,

q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }

31

T1

T2

T3

T1

true

true

false

T2

true

true

false

T3

false

true

trueSlide32

T1 =

struct

{ a:

int, p: pointer to T2 }

T2 =

struct

{ c:

int

,

q: pointer to T3 }T3 = struct { a: float, p: pointer to T1 }

32

T1

T2

T3

T1

true

true

false

T2

true

true

false

T3

false

false

trueSlide33

T1 =

struct

{ a:

int, p: pointer to T2 }T2 =

struct

{ c:

int

,

q: pointer to T3 }

T3 = struct { a: float, p: pointer to T1 }33

T1

T2

T3

T1

true

true

false

T2

true

true

false

T3

false

false

trueSlide34

T1 =

struct

{ a:

int, p: pointer to T2 }T2 =

struct

{ c:

int

,

q: pointer to T3 }

T3 = struct { a: float, p: pointer to T1 }34

T1

T2

T3

T1

true

false

false

T2

true

true

false

T3

false

false

trueSlide35

T1 =

struct

{ a:

int, p: pointer to T2 }T2 =

struct

{ c:

int

,

q: pointer to T3 }

T3 = struct { a: float, p: pointer to T1 }35

T1

T2

T3

T1

true

false

false

T2

false

true

false

T3

false

false

trueSlide36

T1 =

struct

{ a:

int, p: pointer to T2 }T2 =

struct

{ c:

int

,

q: pointer to T3 }

T3 = struct { a: float, p: pointer to T1 }36

T1

T2

T3

T1

true

false

false

T2

false

true

false

T3

false

false

true