Programming Languages and Compilers CS 421 Sasa Misailovic

  • Slides: 78
Download presentation
Programming Languages and Compilers (CS 421) Sasa Misailovic 4110 SC, UIUC https: //courses. engr.

Programming Languages and Compilers (CS 421) Sasa Misailovic 4110 SC, UIUC https: //courses. engr. illinois. edu/cs 421/fa 2017/CS 421 A Based in part on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, and Elsa L Gunter 10/17/2021 1

Terminology n Type: A type t defines a set of possible data values E.

Terminology n Type: A type t defines a set of possible data values E. g. short in C is {x| 215 - 1 x -215} n A value in this set is said to have type t n n Type system: rules of a language assigning types to expressions 10/17/2021 2

Why Data Types? n Data types play a key role in: Data abstraction in

Why Data Types? n Data types play a key role in: Data abstraction in the design of programs n Type checking in the analysis of programs n Compile-time code generation in the translation and execution of programs n n 10/17/2021 Data layout (how many words; which are data and which are pointers) dictated by type 3

Types as Specifications n n n Types describe properties Different type systems describe different

Types as Specifications n n n Types describe properties Different type systems describe different properties: n Data is read-write versus read-only n Operation has authority to access data n Data came from “right” source n Operation might or could not raise an exception Common type systems focus on types describing same data layout and access methods 10/17/2021 4

Sound Type System n n Type: A type t defines a set of possible

Sound Type System n n Type: A type t defines a set of possible data values 15 - 1 x -215} n E. g. short in C is {x| 2 n A value in this set is said to have type t Type system: rules of a language assigning types to expressions If an expression is assigned type t, and it evaluates to a value v, then v is in the set of values defined by t n SML, OCAML, Scheme and Ada have sound type systems n Most implementations of C and C++ do not 10/17/2021 5 n

Strongly Typed Language n When no application of an operator to arguments can lead

Strongly Typed Language n When no application of an operator to arguments can lead to a run-time type error, language is strongly typed n n Eg: 1 + 2. 3; ; Depends on definition of “type error” 10/17/2021 6

Strongly Typed Language n n C++ claimed to be “strongly typed”, but n Union

Strongly Typed Language n n C++ claimed to be “strongly typed”, but n Union types allow creating a value at one type and using it at another n Type coercions may cause unexpected (undesirable) effects n No array bounds check (in fact, no runtime checks at all) SML, OCAML “strongly typed” but still must do dynamic array bounds checks, runtime type case analysis, and other checks 10/17/2021 7

Static vs Dynamic Types • • Static type: type assigned to an expression at

Static vs Dynamic Types • • Static type: type assigned to an expression at compile time Dynamic type: type assigned to a storage location at run time Statically typed language: static type assigned to every expression at compile time Dynamically typed language: type of an expression determined at run time 10/17/2021 8

Type Checking n n n When is op(arg 1, …, argn) allowed? Type checking

Type Checking n n n When is op(arg 1, …, argn) allowed? Type checking assures that operations are applied to the right number of arguments of the right types n Right type may mean same type as was specified, or may mean that there is a predefined implicit coercion that will be applied Used to resolve overloaded operations 10/17/2021 9

Type Checking Type checking may be done statically at compile time or dynamically at

Type Checking Type checking may be done statically at compile time or dynamically at run time n Dynamically typed (aka untyped) languages (eg LISP, Prolog, Java. Script) do only dynamic type checking n Statically typed languages can do most type checking statically n 10/17/2021 10

Dynamic Type Checking Performed at run-time before each operation is applied n Types of

Dynamic Type Checking Performed at run-time before each operation is applied n Types of variables and operations left unspecified until run-time n n Same variable may be used at different types 10/17/2021 11

Dynamic Type Checking Data object must contain type information n Errors aren’t detected until

Dynamic Type Checking Data object must contain type information n Errors aren’t detected until violating application is executed (maybe years after the code was written) n 10/17/2021 12

Static Type Checking Performed after parsing, before code generation n Type of every variable

Static Type Checking Performed after parsing, before code generation n Type of every variable and signature of every operator must be known at compile time n 10/17/2021 13

Static Type Checking Can eliminate need to store type information in data object if

Static Type Checking Can eliminate need to store type information in data object if no dynamic type checking is needed n Catches many programming errors at earliest point n Can’t check types that depend on dynamically computed values n n Eg: array bounds 10/17/2021 14

Static Type Checking n Typically places restrictions on languages Garbage collection n References instead

Static Type Checking n Typically places restrictions on languages Garbage collection n References instead of pointers n All variables initialized when created n Variable only used at one type n n 10/17/2021 Union types allow for work-arounds, but effectively introduce dynamic type checks 15

Type Declarations n Type declarations: explicit assignment of types to variables (signatures to functions)

Type Declarations n Type declarations: explicit assignment of types to variables (signatures to functions) in the code of a program Must be checked in a strongly typed language n Often not necessary for strong typing or even static typing (depends on the type system) n 10/17/2021 16

Type Inference n Type inference: A program analysis to assign a type to an

Type Inference n Type inference: A program analysis to assign a type to an expression from the program context of the expression Fully static type inference first introduced by Robin Miller in ML n Haskel, OCAML, SML all use type inference n Records are a problem for type inference n 10/17/2021 17

Format of Type Judgments n n A type judgement has the form |- exp

Format of Type Judgments n n A type judgement has the form |- exp : is a typing environment n n n Supplies the types of variables (and function names when function names are not variables) is a set of the form { x : , . . . } For any x at most one such that (x : ) exp is a program expression is a type to be assigned to exp |- pronounced “turnstyle”, or “entails” (or “satisfies” or, informally, “shows”) 10/17/2021 18

Axioms - Constants |- n : int (assuming n is an integer constant) |-

Axioms - Constants |- n : int (assuming n is an integer constant) |- true : bool n n |- false : bool These rules are true with any typing environment , n are meta-variables 10/17/2021 19

Axioms – Variables (Monomorphic Rule) Notation: Let (x) = if x : Note: if

Axioms – Variables (Monomorphic Rule) Notation: Let (x) = if x : Note: if such exits, its unique Variable axiom: |- x : 10/17/2021 if (x) = 20

Simple Rules - Arithmetic Primitive operators ( { +, -, *, …}): |- e

Simple Rules - Arithmetic Primitive operators ( { +, -, *, …}): |- e 1: 1 |- e 2: 2 ( ): 1 2 3 |- e 1 e 2 : 3 Relations ( { < , > , =, <=, >= }): ˜ |- e 1 : |- e 2 : |- e 1 e 2 : bool ˜ For the moment, think is int 10/17/2021 21

Example: {x: int} |- x + 2 = 3 : bool What do we

Example: {x: int} |- x + 2 = 3 : bool What do we need to show first? {x: int} |- x: int {x: int} |- 2: int {x : int} |- x + 2 : bool {x: int} |- 3 : int {x: int} |- x + 2 = 3 : bool 10/17/2021 22

Example: {x: int} |- x + 2 = 3 : bool What do we

Example: {x: int} |- x + 2 = 3 : bool What do we need for the left side? {x: int} |- x: int {x: int} |- 2: int {x : int} |- x + 2 : int {x: int} |- 3 : int Rel {x: int} |- x + 2 = 3 : bool 10/17/2021 23

Example: {x: int} |- x + 2 = 3 : bool How to finish?

Example: {x: int} |- x + 2 = 3 : bool How to finish? {x: int} |- x: int {x: int} |- 2: int AO {x : int} |- x + 2 : int {x: int} |- 3 : int Rel {x: int} |- x + 2 = 3 : bool 10/17/2021 24

Example: {x: int} |- x + 2 = 3 : bool Complete Proof (type

Example: {x: int} |- x + 2 = 3 : bool Complete Proof (type derivation) Var Const {x: int} |- x: int {x: int} |- 2: int AO {x : int} |- x + 2 : int {x: int} |- 3 : int Rel {x: int} |- x + 2 = 3 : bool 10/17/2021 25

Simple Rules - Booleans Connectives |- e 1 : bool |- e 2 :

Simple Rules - Booleans Connectives |- e 1 : bool |- e 2 : bool |- e 1 && e 2 : bool |- e 1 : bool |- e 2 : bool |- e 1 || e 2 : bool 10/17/2021 26

Type Variables in Rules n n n If_then_else rule: |- e 1 : bool

Type Variables in Rules n n n If_then_else rule: |- e 1 : bool |- e 2 : |- e 3 : |- (if e 1 then e 2 else e 3) : is a type variable (meta-variable) Can take any type at all All instances in a rule application must get same type Then branch, else branch and if_then_else must all have same type 10/17/2021 27

Function Application n n Application rule: |- e 1 : 1 2 |- e

Function Application n n Application rule: |- e 1 : 1 2 |- e 2 : 1 |- (e 1 e 2) : 2 If you have a function expression e 1 of type 1 2 applied to an argument e 2 of type 1, the resulting expression e 1 e 2 has type 2 10/17/2021 28

Fun Rules describe types, but also how the environment may change n Can only

Fun Rules describe types, but also how the environment may change n Can only do what rule allows! n fun rule: {x : 1 } + |- e : 2 |- fun x -> e : 1 2 n 10/17/2021 29

Fun Examples {y : int } + |- y + 3 : int |-

Fun Examples {y : int } + |- y + 3 : int |- fun y -> y + 3 : int {f : int bool} + |- f 2 : : [true] : bool list |- (fun f -> f 2 : : [true]) : (int bool) bool list 10/17/2021 30

(Monomorphic) Let and Let Rec n n let rule: |- e 1 : 1

(Monomorphic) Let and Let Rec n n let rule: |- e 1 : 1 {x : 1} + |- e 2 : 2 |- (let x = e 1 in e 2 ) : 2 let rec rule: {x: 1} + |- e 1: 1 {x: 1} + |- e 2: 2 |- (let rec x = e 1 in e 2 ) : 2 10/17/2021 31

Example n Which rule do we apply? ? |- (let rec one = 1

Example n Which rule do we apply? ? |- (let rec one = 1 : : one in let x = 2 in fun y -> (x : : y : : one) ) : int list 10/17/2021 32

Example Let rec rule: 2 {one : int list} |1 (let x = 2

Example Let rec rule: 2 {one : int list} |1 (let x = 2 in {one : int list} |fun y -> (x : : y : : one)) (1 : : one) : int list |- (let rec one = 1 : : one in let x = 2 in fun y -> (x : : y : : one) ) : int list n 10/17/2021 33

Proof of 1 n Which rule? {one : int list} |- (1 : :

Proof of 1 n Which rule? {one : int list} |- (1 : : one) : int list 10/17/2021 34

Proof of 1 n Application 3 4 {one : int list} |((: : )

Proof of 1 n Application 3 4 {one : int list} |((: : ) 1): int list one : int list {one : int list} |- (1 : : one) : int list 10/17/2021 35

Proof of 3 Constants Rule {one : int list} |(: : ) : int

Proof of 3 Constants Rule {one : int list} |(: : ) : int list 1 : int {one : int list} |- ((: : ) 1) : int list 10/17/2021 36

Proof of 4 n Rule for variables {one : int list} |- one :

Proof of 4 n Rule for variables {one : int list} |- one : int list 10/17/2021 37

Proof of 2 5 {x: int; one : int list} |n Constant fun y

Proof of 2 5 {x: int; one : int list} |n Constant fun y -> (x : : y : : one)) {one : int list} |- 2 : int list {one : int list} |- (let x = 2 in fun y -> (x : : y : : one)) : int list 10/17/2021 38

Proof of 5 ? {x: int; one : int list} |- fun y ->

Proof of 5 ? {x: int; one : int list} |- fun y -> (x : : y : : one)) : int list 10/17/2021 39

Proof of 5 ? {y: int; x: int; one : int list} |- (x

Proof of 5 ? {y: int; x: int; one : int list} |- (x : : y : : one) : int list {x: int; one : int list} |- fun y -> (x : : y : : one)) : int list 10/17/2021 40

Proof of 5 6 7 {y: int; x: int; one: int list} |- ((:

Proof of 5 6 7 {y: int; x: int; one: int list} |- ((: : ) x): int list |- (y : : one) : int list {y: int; x: int; one : int list} |- (x : : y : : one) : int list {x: int; one : int list} |- fun y -> (x : : y : : one)) : int list 10/17/2021 41

Proof of 6 Constant Variable {…} |- (: : ) : int list {…;

Proof of 6 Constant Variable {…} |- (: : ) : int list {…; x: int; …} |- x: int {y: int; x: int; one : int list} |- ((: : ) x) : int list 10/17/2021 42

Proof of 7 Like Pf of 6 [replace x w/ y] Variable {y: int;

Proof of 7 Like Pf of 6 [replace x w/ y] Variable {y: int; …} |- ((: : ) y) {…; one: int list} |: int list one: int list {y: int; x: int; one : int list} |- (y : : one) : int list 10/17/2021 43

Curry - Howard Isomorphism n n Type Systems are logics; logics are type systems

Curry - Howard Isomorphism n n Type Systems are logics; logics are type systems Types are propositions; propositions are types Terms are proofs; proofs are terms Function space arrow corresponds to implication; application corresponds to modus ponens 10/17/2021 44

Curry - Howard Isomorphism n Modus Ponens A B • Application |- e 1

Curry - Howard Isomorphism n Modus Ponens A B • Application |- e 1 : |- e 2 : |- (e 1 e 2) : 10/17/2021 45

Mea Culpa n n n The above system can’t handle polymorphism as in OCAML

Mea Culpa n n n The above system can’t handle polymorphism as in OCAML No type variables in type language (only metavariable in the logic) Would need: n Object level type variables and some kind of type quantification n let and let rec rules to introduce polymorphism n Explicit rule to eliminate (instantiate) polymorphism 10/17/2021 46

Support for Polymorphic Types n Monomorpic Types ( ): n n Basic Types: int,

Support for Polymorphic Types n Monomorpic Types ( ): n n Basic Types: int, bool, float, string, unit, … Type Variables: , , g, d, e Compound Types: , int * string, bool list, … Polymorphic Types: n n Monomorphic types Universally quantified monomorphic types 1, … , n. Can think of as same as. A 10/17/2021 A n 47

Support for Polymorphic Types n Typing Environment supplies polymorphic types (which will often just

Support for Polymorphic Types n Typing Environment supplies polymorphic types (which will often just be monomorphic) for variables Free variables of monomorphic type just type variables that occur in it n n Free variables of polymorphic type removes variables that are universally quantified n n Write Free. Vars( ) Free. Vars( 1, … , n. ) = Free. Vars( ) – { 1, … , n } A n Free. Vars( ) = all Free. Vars of types in range of 10/17/2021 48

Monomorphic to Polymorphic n n n Given: n type environment n monomorphic type n

Monomorphic to Polymorphic n n n Given: n type environment n monomorphic type n shares type variables with Want most polymorphic type for that doesn’t break sharing type variables with Gen( , ) = 1, … , n. where { 1, … , n} = free. Vars( ) – free. Vars( ) 10/17/2021 49

Polymorphic Typing Rules n n A type judgement has the form |- exp :

Polymorphic Typing Rules n n A type judgement has the form |- exp : n uses polymorphic types n still monomorphic Most rules stay same (except use more general typing environments). Rules that change: n n Variables Let and Let Rec Allow polymorphic constants Worth noting functions again 50

Polymorphic Let and Let Rec n n let rule: |- e 1 : 1

Polymorphic Let and Let Rec n n let rule: |- e 1 : 1 {x : Gen( 1, )} + |- e 2 : 2 |- (let x = e 1 in e 2 ) : 2 let rec rule: {x : 1} + |- e 1: 1 {x: Gen( 1, )} + |- e 2: 2 |- (let rec x = e 1 in e 2 ) : 2 10/17/2021 51

Polymorphic Variables (Identifiers) Variable axiom: n n if (x) = 1, … , n.

Polymorphic Variables (Identifiers) Variable axiom: n n if (x) = 1, … , n. Where j replaces all occurrences of 1, … , n by monotypes 1, … , n Note: Monomorphic rule special case: |- x : n A |- x : j( ) if (x) = Constants treated same way 10/17/2021 52

Fun Rule Stays the Same n fun rule: {x : 1} + |- e

Fun Rule Stays the Same n fun rule: {x : 1} + |- e : 2 |- fun x -> e : 1 2 Types 1, 2 monomorphic n Function argument must always be used at same type in function body n 10/17/2021 53

Polymorphic Example n n n Assume additional constants: hd : . list -> tl:

Polymorphic Example n n n Assume additional constants: hd : . list -> tl: . list -> list is_empty : . list -> bool : : : . -> list [] : . list 10/17/2021 54

Polymorphic Example n Show: ? {} |- let rec length = fun l ->

Polymorphic Example n Show: ? {} |- let rec length = fun l -> if is_empty l then 0 else 1 + length (tl l) in length ((: : ) 2 []) + length((: : ) true []) : int 10/17/2021 55

Polymorphic Example: Let Rec Rule (Repeat) Show: (1) (2) {length: list -> int} {length:

Polymorphic Example: Let Rec Rule (Repeat) Show: (1) (2) {length: list -> int} {length: . list -> int} |- fun lst -> … |- length ((: : ) 2 []) + : list -> int length((: : ) true []) : int {} |- let rec length = fun lst -> if is_empty lst then 0 else 1 + length (tl lst) in 10/17/2021 length ((: : ) 2 []) + length((: : ) true []) : int 56 n

Polymorphic Example (1) n Show: ? {length: list -> int} |fun lst -> if

Polymorphic Example (1) n Show: ? {length: list -> int} |fun lst -> if is_empty lst then 0 else 1 + length (tl lst) : list -> int 10/17/2021 57

Polymorphic Example (1): Fun Rule Show: (3) {length: list -> int, lst: list }

Polymorphic Example (1): Fun Rule Show: (3) {length: list -> int, lst: list } |if is_empty lst then 0 else length (hd l) + length (tl lst) : int {length: list -> int} |fun lst -> if is_empty lst then 0 else 1 + length (tl lst) : list -> int n 10/17/2021 58

Polymorphic Example (3) n n Let ={length: list -> int, lst: list } Show

Polymorphic Example (3) n n Let ={length: list -> int, lst: list } Show ? |- if is_empty l then 0 else 1 + length (tl lst) : int 10/17/2021 59

Polymorphic Example (3): If. Then. Else n n Let ={length: list -> int, lst:

Polymorphic Example (3): If. Then. Else n n Let ={length: list -> int, lst: list } Show (4) (5) (6) |- is_empty lst |- 0: int |- 1 + length (tl lst) : bool : int |- if is_empty l then 0 else 1 + length (tl lst) : int 10/17/2021 60

Polymorphic Example (4) n n Let ={length: list -> int, lst: list } Show

Polymorphic Example (4) n n Let ={length: list -> int, lst: list } Show ? |- is_empty lst : bool 10/17/2021 61

Polymorphic Example (4): Application n n Let ={length: list -> int, lst: list }

Polymorphic Example (4): Application n n Let ={length: list -> int, lst: list } Show ? ? |- is_empty : list -> bool |- lst : list |- is_empty lst : bool 10/17/2021 62

Polymorphic Example (4) n n Let ={length: list -> int, lst: list } Show

Polymorphic Example (4) n n Let ={length: list -> int, lst: list } Show By Const since list -> bool is instance of . list -> bool ? A |- is_empty : list -> bool |- lst : list |- is_empty lst : bool 10/17/2021 63

Polymorphic Example (4) n n Let ={length: list -> int, l: list } Show

Polymorphic Example (4) n n Let ={length: list -> int, l: list } Show By Const since list -> bool is By Variable instance of . list -> bool (lst) = list A |- is_empty : list -> bool |- lst : list |- is_empty lst : bool n This finishes (4) 10/17/2021 64

Polymorphic Example (3): If. Then. Else (Repeat) n n Let ={length: list -> int,

Polymorphic Example (3): If. Then. Else (Repeat) n n Let ={length: list -> int, lst: list } Show (4) (5) (6) |- is_empty lst |- 0: int |- 1 + length (tl lst) : bool : int |- if is_empty l then 0 else 1 + length (tl lst) : int 10/17/2021 65

Polymorphic Example (5): Const Let ={length: list -> int, lst: list } n Show

Polymorphic Example (5): Const Let ={length: list -> int, lst: list } n Show By Const Rule n |- 0: int 10/17/2021 66

Polymorphic Example (6): Arith Op Let ={length: list -> int, lst: list } n

Polymorphic Example (6): Arith Op Let ={length: list -> int, lst: list } n Show By Variable (7) |- length |- (tl lst) By Const : list -> int : list |- l : int |- length (tl lst) : int |- 1 + length (tl lst) : int n 10/17/2021 67

Polymorphic Example (7): App Rule Let ={length: list -> int, lst: list } n

Polymorphic Example (7): App Rule Let ={length: list -> int, lst: list } n Show By Const By Variable |- (tl lst) : list -> list |- lst : list |- (tl lst) : list n By Const since list -> list is instance of . list -> list A 10/17/2021 68

Polymorphic Example: Let Rec Rule (Repeat) Show: (1) (2) {length: list -> int} {length:

Polymorphic Example: Let Rec Rule (Repeat) Show: (1) (2) {length: list -> int} {length: . list -> int} |- fun l -> … |- length ((: : ) 2 []) + : list -> int length((: : ) true []) : int {} |- let rec length = fun l -> if is_empty l then 0 else 1 + length (tl l) in 10/17/2021 length ((: : ) 2 []) + length((: : ) true []) : int 69 n

Polymorphic Example: (2) by Arith. Op n Let ’ = {length: . list ->

Polymorphic Example: (2) by Arith. Op n Let ’ = {length: . list -> int} Show: A n (8) (9) ’ |length ((: : ) 2 []) : int length((: : ) true []) : int {length: . list -> int} |- length ((: : ) 2 []) + length((: : ) true []) : int 10/17/2021 70

Polymorphic Example: (8)App. Rule n Let ’ = {length: . list -> int} Show:

Polymorphic Example: (8)App. Rule n Let ’ = {length: . list -> int} Show: A n ’ |- length : int list ->int ’ |- ((: : )2 []) : int list ’ |- length ((: : ) 2 []) : int 10/17/2021 71

Polymorphic Example: (8)App. Rule Let ’ = {length: . list -> int} n Show:

Polymorphic Example: (8)App. Rule Let ’ = {length: . list -> int} n Show: By Var since int list -> int is instance of . list -> int (10) ’ |- length : int list ->int ’ |- ((: : )2 []): int list ’ |- length ((: : ) 2 []) : int A n A 10/17/2021 72

Polymorphic Example: (10)App. Rule Let ’ = {length: . list -> int} n Show:

Polymorphic Example: (10)App. Rule Let ’ = {length: . list -> int} n Show: n By Const since list is instance of . list (11) ’|-((: : ) 2) : int list -> int list ’ |- [] : int list ’ |- ((: : ) 2 []) : int list A n A 10/17/2021 73

Polymorphic Example: (11)App. Rule Let ’ = {length: . list -> int} n Show:

Polymorphic Example: (11)App. Rule Let ’ = {length: . list -> int} n Show: n By Const since list is instance of . list By Const ’ |- (: : ) : int -> int list ’ |- 2 : int ’ |- ((: : ) 2) : int list -> int list A n A 10/17/2021 74

Polymorphic Example: (9)App. Rule n Let ’ = {length: . list -> int} Show:

Polymorphic Example: (9)App. Rule n Let ’ = {length: . list -> int} Show: A n ’ |length: bool list ->int ((: : ) true []): bool list ’ |- length ((: : ) true []) : int 10/17/2021 75

Polymorphic Example: (9)App. Rule Let ’ = {length: . list -> int} n Show:

Polymorphic Example: (9)App. Rule Let ’ = {length: . list -> int} n Show: By Var since bool list -> int is instance of . list -> int (12) ’ |length : bool list ->int ((: : ) true []) : bool list ’ |- length ((: : ) true []) : int A n A 10/17/2021 76

Polymorphic Example: (12)App. Rule Let ’ = {length: . list -> int} n Show:

Polymorphic Example: (12)App. Rule Let ’ = {length: . list -> int} n Show: n By Const since list is instance of . list (13) ’|-((: : )true): bool list ->bool list ’|- []: bool list ’ |- ((: : ) true []) : bool list A n A 10/17/2021 77

Polymorphic Example: (13)App. Rule Let ’ = {length: . list -> int} n Show:

Polymorphic Example: (13)App. Rule Let ’ = {length: . list -> int} n Show: By Const since bool list is instance of . list By Const ’ |(: : ): bool ->bool list true : bool ’ |- ((: : ) true) : bool list -> bool list A n A 10/17/2021 78