lecture 9: types (part 2) programming...

43
1 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 9: Types (Part 2)

Upload: others

Post on 01-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

1

Prof. Dr. Michael Pradel

Software Lab, University of StuttgartWinter 2019/2020

Programming Paradigms

Lecture 9: Types (Part 2)

Page 2: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

2 - 1

Wake-up Exercise

What does the following C++ code print?

#include<iostream>using namespace std;int N = 5;

int main(){static int x = 1;if (cout << x << " " && x++ < N && main()){ }return 0;

}

https://ilias3.uni-stuttgart.de/vote/0ZT9

Page 3: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

2 - 2

Wake-up Exercise

What does the following C++ code print?

#include<iostream>using namespace std;int N = 5;

int main(){static int x = 1;if (cout << x << " " && x++ < N && main()){ }return 0;

}

https://ilias3.uni-stuttgart.de/vote/0ZT9

Result: 1, 2, 3, 4, 5

Page 4: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

2 - 3

Wake-up Exercise

What does the following C++ code print?

#include<iostream>using namespace std;int N = 5;

int main(){static int x = 1;if (cout << x << " " && x++ < N && main()){ }return 0;

}

https://ilias3.uni-stuttgart.de/vote/0ZT9

Result: 1, 2, 3, 4, 5

Recursive call of main

Page 5: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

2 - 4

Wake-up Exercise

What does the following C++ code print?

#include<iostream>using namespace std;int N = 5;

int main(){static int x = 1;if (cout << x << " " && x++ < N && main()){ }return 0;

}

https://ilias3.uni-stuttgart.de/vote/0ZT9

Result: 1, 2, 3, 4, 5

Variable keeps its value acrossinvocations of the function

Page 6: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

2 - 5

Wake-up Exercise

What does the following C++ code print?

#include<iostream>using namespace std;int N = 5;

int main(){static int x = 1;if (cout << x << " " && x++ < N && main()){ }return 0;

}

https://ilias3.uni-stuttgart.de/vote/0ZT9

Result: 1, 2, 3, 4, 5

Print currentvalue of x

Page 7: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

2 - 6

Wake-up Exercise

What does the following C++ code print?

#include<iostream>using namespace std;int N = 5;

int main(){static int x = 1;if (cout << x << " " && x++ < N && main()){ }return 0;

}

https://ilias3.uni-stuttgart.de/vote/0ZT9

Result: 1, 2, 3, 4, 5

Increment x untilit reaches 5

Page 8: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

3

Overview

� Introduction to Types and TypeSystems

� Type Checking

� Type Equivalence

� Type Compatibility

� Formal Definition of Type Systems

� Type Inference

� Equality Testing and Assignments

Page 9: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

4

Type Compatibility

� Check whether combining two valuesis valid according to their types

� “Combining” may mean� Assignment: Are left-hand side and right-hand

side compatible?

� Operators: Are operands compatible with the

operator and with each other?

� Function calls: Are actual arguments and

formal parameters compatible?

Page 10: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

5

Compatible 6= Equal

Most PLs: Types may be compatibleeven when not the same

Example (C):double d = 2.3;float f = d * 2;int i = f;printf("%d\n", i);

Page 11: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

6

Compatible 6= Equal (2)

� Rules of PL define which types arecompatible

� Examples of rules� Can assign subtype to supertype:

lhs = rhs;

� Different number types are compatible with

each other

� Collections of same type are compatible, even

if length differs

Page 12: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

7

Type Conversions

When types aren’t equal, they must beconverted

� Option 1: Cast = explicit type conversion

� Programmer changes value’s type from T1 to T2

� Option 2: Coercion = implicit type conversion

� PL allows values of type T1 in situation where type

T2 expected

� Both options: Actual conversion happens at

runtime

Page 13: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

8

Runtime Behavior of Conversions

What happens during conversion?

Three cases:� Types are structurally equivalent: Conversion is

only conceptual, no instructions executed

� Types have different sets of values, but arerepresented in the same way in memory: Mayneed check that value is in target type

� Different low-level representations: Need

special instructions for conversion

Page 14: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

35

Page 15: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

10 - 1

Coercions in C

� Most primitive types are coercedwhenever needed

� Some coercions may looseinformation� float to int: Loose fraction

� int to char: Causes char to overflow (and

will give unexpected result)

� Enable compiler warnings to avoidsurprises

Page 16: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

10 - 2

Coercions in C

� Most primitive types are coercedwhenever needed

� Some coercions may looseinformation� float to int: Loose fraction

� int to char: Causes char to overflow (and

will give unexpected result)

� Enable compiler warnings to avoidsurprises

Source: geeksforgeeks.org

Page 17: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

11

Coercions in C: Demo

Demo: coercions.ccompile with gcc -Wconversion

Page 18: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

12

Coercions in JavaScript

� Almost all types are coerced whenneeded� Rationale: Websites shouldn’t crash

� Some coercions make sense:� "number:" + 3 yields "number:3"

� Many others are far from intuitive:� [1, 2] << "2" yields 0

More details and examples:The Good, the Bad, and the Ugly: An Empirical Study of Implicit TypeConversions in JavaScript. Pradel and Sen. ECOOP 2015

Page 19: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

13

Overview

� Introduction to Types and TypeSystems

� Type Checking

� Type Equivalence

� Type Compatibility

� Formal Definition of Type Systems

� Type Inference

� Equality Testing and Assignments

Page 20: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

14

Formally Defined Type Systems

� Type systems are� implemented in a compiler

� formally described

� and sometimes both

� Active research area with dozens ofpapers each year� Focus: New languages and strong type

guarantees

� Example here: Typed expressions

Page 21: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

36

Page 22: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

16

Not All Expressions Make Sense

� Only some expressions can beevaluated

� Other don’t make sense

� Implementation of the language would getstuck or throw a runtime error

Page 23: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

18

Types to the Rescue

� Use types to check whether anexpression is meaningful

� If term t has a type T , then its evaluation won’t

get stuck

� Written as t : T

� Two types

� Nat .. natural numbers

� Bool .. Boolean values

”has type”

Page 24: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

37

Page 25: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

38

Page 26: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

21

Type Checking Expressions

� Typing relation: Smallest binaryrelation between terms and types thatsatisfies all instances of the rules

� Term t is typable (or well typed) ifthere is some T such that t : T

� Type derivation: Tree of instances ofthe typing rules that shows t : T

Page 27: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

39

Page 28: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

40

Page 29: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

23

Quiz: Typing Derivation

Find the typing derivation for thefollowing expression:

if false then (pred(pred 0)) else (succ 0)

How many axioms and rules do youneed?

https://ilias3.uni-stuttgart.de/vote/0ZT9

Page 30: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

41

Page 31: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

25

Overview

� Introduction to Types and TypeSystems

� Type Checking

� Type Equivalence

� Type Compatibility

� Formal Definition of Type Systems

� Type Inference

� Equality Testing and Assignments

Page 32: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

26

Type Inference

Some PLs are statically typed but allowprogrammers to omit some typeannotations

� Get guarantees of static type checking

� Without paying the cost of full type annotations

� Different from gradual typing, where programmer

decides when and where to annotate types

Page 33: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

27 - 1

Example

// Scalavar businessName = "Montreux Jazz Cafe"

def squareOf(x: Int) = x * x

businessName = squareOf(23)

Page 34: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

27 - 2

Example

// Scalavar businessName = "Montreux Jazz Cafe"

def squareOf(x: Int) = x * x

businessName = squareOf(23)

Inferred tobe a String

Inferred toreturn an Int

Page 35: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

27 - 3

Example

// Scalavar businessName = "Montreux Jazz Cafe"

def squareOf(x: Int) = x * x

businessName = squareOf(23)

Inferred tobe a String

Inferred toreturn an Int

Compile-time type error:Can’t assign Int to String variable

Page 36: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

28

Unifying Partial Type Information

� Type inference algorithm:Unifies partial type information for twovalues whenever type rules expectthem to be the same

� Type checking:Find unique type for each value withno contradictions and no ambiguousoccurrences of overloaded names

Page 37: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

29

Overview

� Introduction to Types and TypeSystems

� Type Checking

� Type Equivalence

� Type Compatibility

� Formal Definition of Type Systems

� Type Inference

� Equality Testing and Assignments

Page 38: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

30

Equality Testing

� Equality operator: Many possiblemeanings� E.g., for two strings

• Are the strings aliases?

• Are they bitwise identical?

• Do they have the same sequence of chars?

• Do they look the same if printed?

� Meaning depends on PL and types ofvalues

Page 39: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

31

Assignments

� Assignment operator: Multiplepossible meanings

� Deep vs. shallow

� (Deep) copy of right-hand side

to left-hand side’s location

� Mostly used for primitive types

� Rare for objects, but useful,e.g., for remote procedure calls

� Copy reference to

right-hand side to

left-hand side

� Mostly used forobjects

Page 40: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

32 - 1

Quiz: Types

Which of the following statements istrue?

� Types are compatible if and only if they are equal

� Coercions mean that a programmer casts a value

from one type to another type

� Type conversions are guaranteed to preserve the

meaning of a value

� PLs with type inference may provide static type

guaranteeshttps://ilias3.uni-stuttgart.de/vote/0ZT9

Page 41: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

32 - 2

Quiz: Types

Which of the following statements istrue?

� Types are compatible if and only if they are equal

� Coercions mean that a programmer casts a value

from one type to another type

� Type conversions are guaranteed to preserve the

meaning of a value

� PLs with type inference may provide static type

guaranteeshttps://ilias3.uni-stuttgart.de/vote/0ZT9

Page 42: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

33

Overview

� Introduction to Types and TypeSystems

� Type Checking

� Type Equivalence

� Type Compatibility

� Formal Definition of Type Systems

� Type Inference

� Equality Testing and Assignments

Page 43: Lecture 9: Types (Part 2) Programming Paradigmssoftware-lab.org/teaching/winter2019/pp/lecture_types2.pdf · 2019. 12. 6. · 1 Prof. Dr. Michael Pradel Software Lab, University of

34

Overview

� Introduction to Types and TypeSystems

� Type Checking

� Type Equivalence

� Type Compatibility

� Formal Definition of Type Systems

� Type Inference

� Equality Testing and Assignments 4