1 / 93

Compiler Construction 2010, Lecture 7

http://lara.epfl.ch. Compiler Construction 2010, Lecture 7. Type Analysis. Compiler. Id3 = 0 while (id3 < 10) { println (“”,id3); id3 = id3 + 1 }. Construction. source code. Compiler ( scalac , gcc ) . i d3 = 0 LF w. id3 = 0 while ( id3 < 10 ). assign.

yovela
Download Presentation

Compiler Construction 2010, Lecture 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. http://lara.epfl.ch Compiler Construction 2010, Lecture 7 Type Analysis

  2. Compiler Id3 = 0 while (id3 < 10) { println(“”,id3); id3 = id3 + 1 } Construction source code Compiler (scalac, gcc) id3 = 0LF w id3=0while(id3 <10) assign i 0 • < while • i parser • 10 lexer assign • + a[i] • 3 • * • 7 • i trees characters words (tokens) making sense of trees

  3. Today • Type Checking Idea • Evaluation and Types • Type Rules for Ground Expressions • Type Environments • Assignments • Arrays

  4. Evaluating an Expression scala prompt: defmin1(x : Int, y : Int) : Int = { if (x < y) x else y+1 }min1: (x: Int,y: Int)Intmin1(10,5)res1: Int = 6 How can we think about this evaluation? x  10y  5if (x < y) x else y+1

  5. Each Value has a Type scala prompt: defmin1(x : Int, y : Int) : Int = { if (x < y) x else y+1 }min1: (x: Int,y: Int)Intmin1(10,5)res1: Int= 6 How can we think about this evaluation? x : Int 10y : Int 5if (x < y) x else y+1

  6. We can compute types without values scala prompt: defmin1(x : Int, y : Int) : Int = { if (x < y) x else y+1 }min1: (x: Int,y: Int)Intmin1(10,5)res1: Int= 6 How can we think about this evaluation? x : Inty : Intif (x < y) x else y+1

  7. We do not like trees upside-down

  8. Leaves are Up type rules move from leaves to root

  9. Type Rules as Local Tree Constraints x : Inty : Int Type Rules for every type T, if b has type Boolean, and ... then

  10. Type Rules with Environment x : Inty : Int Type Rules

  11. if the free variables of e have types given by gamma,then e (correctly) type checks and has type T If e1 type checks in gamma and has type T1 and ...and en type checks in gamma and has type Tn then e type checks in gamma and has type T

  12. Derivation Using Type Rules x : Inty : Int

  13. Type Rule for Function Application We can treat operators as variables that have function type We can replace many previous rules with application rule:

  14. Computing the Environment of a Class object World {var data : Intvar name : Stringdef m(x : Int, y : Int) : Boolean { ... }def n(x : Int) : Int { if (x > 0) p(x – 1) else 3 }def p(r : Int) : Int = {var k = r + 2 m(k, n(k)) }} Type check each function m,n,p in this global environment

  15. Extending the Environment class World {var data : Intvar name : Stringdef m(x : Int, y : Int) : Boolean { ... }def n(x : Int) : Int { if (x > 0) p(x – 1) else 3 }def p(r : Int) : Int = {var k:Int = r + 2 m(k, n(k)) }}

  16. Type Checking Expression in a Body class World {var data : Intvar name : Stringdef m(x : Int, y : Int) : Boolean { ... }def n(x : Int) : Int { if (x > 0) p(x – 1) else 3 }def p(r : Int) : Int = {var k:Int = r + 2 m(k, n(k)) }}

  17. Remember Function Updates {(x,T1),(y,T2)} {(x,T3)} = {(x,T3),(y,T2)}

  18. Type Rule for Method Bodies Type Rule for Assignments Type Rules for Block: { var x1:T1 ... varxn:Tn; s1; ... sm; e }

  19. Blocks with Declarations in the Middle just expression empty declaration is first statement is first

  20. Rule for While Statement

  21. Rule for Method Call

  22. Example to Type Check class World {var z : Booleanvar u : Intdef f(y : Boolean) : Int {z = y if (u > 0) { u = u – 1var z : Int z = f(!y) + 3z+z} else {0 } } }

  23. Overloading of Operators Not a problem for type checking from leaves to root

  24. Arrays Using array as an expression, on the right-hand side Assigning to an array

  25. Example with Arrays def next(a : Array[Int], k : Int) : Int = { a[k] = a[a[k]]}

  26. Type Rules (1) constant variable function application plus if assignment while

  27. Type Rules (2) array use arrayassignment

  28. Type Rules (3) top-level environment of class C method invocation field use field assignment

  29. Type Rules (1) constant variable function application plus if assignment while

  30. Type Rules (2) array use arrayassignment

  31. Type Rules (3) top-level environment of class C method invocation field use field assignment

  32. Meaning of Types • Types can be viewed as named entities • explicitly declared classes, traits • their meaning is given by methods they have • constructs such as inheritance establishe relationships between classes • Types can be viewed as sets of values • Int = { ..., -2, -1, 0, 1, 2, ... } • Boolean = { false, true } • Int Int = { f : Int -> Int | f is computable }

  33. Types as Sets • Sets so far were disjoint • Sets can overlap

  34. SUBTYPING

  35. Subtyping • Subtyping corresponds to subset • Systems with subtyping have non-disjoint sets • T1<: T2 means T1 is a subtype of T2 • corresponds to T1 T2 in sets of values • Main rule for subtyping corresponds to

  36. Types for Positive and Negative Ints Int = { ... , -2, -1, 0, 1, 2, ... }Pos = { 1, 2, ... }Neg = { ..., -2, -1 }

  37. More Rules

  38. Making Rules Useful • Let x be a variable if (y > 0) { if (x > 0) {var z : Pos = x * y res = 10 / z} }

  39. Subtyping Example Pos <: Intdef f(x:Int) : Pos = { if (x < 0) –x else x+1}var p : Posvar q : Intq = f(p) - type checks

  40. Using Subtyping Pos <: Intdef f(x:Pos) : Pos = { if (x < 0) –x else x+1}varp : Intvar q : Intq = f(p) - does not type check

  41. What Pos/Neg Types Can Do defmultiplyFractions(p1 : Int, q1 : Pos, p2 : Int, q2 : Pos) : (Int,Pos) { (p1*q1, q1*q2) } defaddFractions(p1 : Int, q1 : Pos, p2 : Int, q2 : Pos) : (Int,Pos) { (p1*q2 + p2*q1, q1*q2) } defprintApproxValue(p : Int, q : Pos) = { print(p/q) // no division by zero } More sophisticated types can track intervals of numbers and ensure that a program does not crash with an array out of bounds error.

  42. Subtyping and Product Types

  43. Using Subtyping Pos <: Intdef f(x:Pos) : Pos = { if (x < 0) –x else x+1}varp : Intvar q : Intq = f(p) - does not type check

  44. Subtyping for Products

  45. Analogy with Cartesian Product

  46. Subtyping and Function Types

  47. Subtyping for Function Types covariance contravariance Consequence:

  48. Function Space as Set To get the appropriate behavior we need to assign sets to function types like this:T1 T2 = We can prove

  49. Proof

More Related