1 / 47

PPL

PPL. Lecture Notes: Chapter 2. Static Verification: Type Inference. Midterm 2011. נתונות הפרוצדורות foo ו- goo: (define foo (lambda (x) (display x) (newline) (+ x 2))) (define goo (lambda (x) (display 5) (newline) (+ x 1))) א. בחישוב של הביטוי > ( foo (goo 0))

susan
Download Presentation

PPL

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. PPL Lecture Notes: Chapter 2 Static Verification: Type Inference

  2. Midterm 2011 נתונות הפרוצדורות fooו-goo: (define foo (lambda (x) (display x) (newline) (+ x 2))) (define goo (lambda (x) (display 5) (newline) (+ x 1))) א. בחישוב של הביטוי > (foo (goo 0)) התקבל הפלט הבא על המסך: 5 1 5 3 מהי שיטת החישוב של האינטרפרטר? סמן בעיגול תשובה יחידה. 1. applicative order 2. normal order

  3. Type Inference • Based on S. Krishnamurthi. Programming Languages: Application and Interpretation 2007. Chapters 24-26 Lecture notes chapter 2.3

  4. Program Correctness • Type correctness • Verify that the types of expressions in the program are “correct” – well-typing • E.g. + is applied to numbers • In other languages: we should also check that the type of value stored in a variable correspond to the variable declared type • Program Verification • Verify that the program halts and produces the “correct” output • Somewhat easier with design-by-contract, where it can be done in a modular fashion

  5. Languages and Types • Fully typed (every value has a type) • C, Pascal, Java.. • Semi-typed (allow typeless structures of values) • Scheme • Untyped • Prolog • Well-typing rules • Define relationships between types

  6. Static/Dynamic Type Check • Static • Based on program code – can be applied off-line • Dynamic • Needs concrete data – done at runtime

  7. Type Checking and Inference Type Checking. Given an expression, and a “goal” type T, verify that the expression type is T. Example: Given the expression (+ (f 3) 7), we need to verify that (f 3) is a number. Type Inference. Infer the type of an expression Example: (f 3)

  8. Type Language • We need a way to specify types • We know: Number, booleans, symbol • New: Union, Void, Tuple • Type polymorphism

  9. Union What is the type of (if (= y 0) 'error (/ 1 y))? Symbol unionNumber

  10. Union • No value constructor • Type Constrcutor is union • Simplification rules • S union S = S • S union T = T union S

  11. Void What is the type of (λ (x) (display (+ 1 x))) ? [Number -> Void]

  12. Void • Singleton set {void} • Cannot be embedded in a composite expression

  13. Tuples What is the type of: (λ (x y) (+ x y)) ? We know that: [Number * Number -> Number] What about (λ () 5) ?

  14. Types in Scheme (cnt’) • Tuple Type • Procedures with any number of parameters: n-tuples. • Empty for 0-tuples. • T1*…*Tn for n-tuples. • * is type constructor • Cannot be a returned type

  15. Procedures and Type Polymorphism • What is the type of: (lambda (x) x) ? [Number -> Number] [Boolean -> Boolean] [[Empty -> Number] -> [Empty -> Number]] ... [T -> T]

  16. Procedures and Type Polymorphism • What is the type of: (lambda (f x) (f x)) [[T1 -> T2]*T1 -> T2] (lambda (f x) ( (f x) x)) [[T1 -> [T1 -> T2]]*T1 -> T2] • Instantiations, type variables renaming...

  17. Type Language Type -> ’Void’ | Non-void Non-Void -> Atomic | Composite | Type-variable Atomic -> ’Number’ | ’Boolean’ | ’Symbol’ Composite -> Procedure | Tuple | Union Procedure -> ’[’ Tuple ’->’ Type ’]’ Tuple -> (Non-void ’*’ )* Non-void | ’Empty’ Union -> Type ’union’ Type Type-variable -> A symbol

  18. Static Type Inference • Infers the type of an expression • Checks for correctness of types (well-typeness) • Example: type inference for (+ x 5) states that given x is a Number, the type is Number. • Similar to logic inference: • Definitions • Axioms • Rules • Algorithm

  19. Terms • Type Environment • Mapping variables to types • Example: Tenv = {x:Number, y:[Number –> T]} • Notation: Tenv(x) = Number • Can be extended: {x:Number, y:[Number –> T]} ◦ {z:boolean} = {x:Number, y:[Number –> T], z:boolean} {} ◦ {x1:T1,...,xn:Tn} = {x1:T1,...,xn:Tn} • Extension pre-condition: new variables are different from old ones.

  20. Terms • Typing statement • Tenv |- e:T • Under Tenv, the expression e has the type T • {x<-Number} |- (+ x 5):Number • Type variables (as well as unbound variables)used in such statements are defined to be universally quantified • {x:[T1 –> T2], y:T1} |- (x y):T2 • A typing statement can be instantiated by consistent replacement of type variables

  21. Restricted Scheme (syntax) <scheme-exp> -> <exp> <exp> -> <atomic> | <composite> <atomic> -> <number> | <boolean> | <variable> <composite> -> <special> | <form> <number> -> Numbers <boolean> -> ’#t’ | ’#f’ <variable> -> sequences of letters <special> -> <lambda> | <quote> <form> -> ’(’ <exp>+ ’)’ <lambda> -> ’(’ ’lambda’ ’(’ <variable>* ’)’ <exp>+ ’)’ <quote> -> ’(’ ’quote’ <variable> ’)’ For now: no ‘if’s, no ‘define’s and no recursive procedures

  22. Typing Axioms • To infer the type of an expression (as well as whether or not it is well-typed), we need to define rules • To be used on sub-expressions, to derive types of the more complex expression • Only abbreviated forms are given here, see lecture notes for full description of rules.

  23. Typing axioms Typing axiom Number: For every type environment _Tenv and number _n: _Tenv|- _n:Number Typing axiom Boolean : For every type environment _Tenv and boolean_ b: _Tenv |- _b:Boolean Typing axiom Variable : For type environment _Tenv and variable _v: _Tenv |- _v: _Tenv(_v) Typing axioms Primitive procedure : _Tenv |- +:[Number* ... *Number -> Number] _Tenv |- not:[S -> Boolean] where S is a type variable. _Tenv |- display:[S -> Void] …

  24. Typing Rules Typing rule Procedure: For all _xi,_Si,_bi,U_i…. If Tenv° {_x1<-_S1, ..., _xn<-_Sn} |- _b:_Uifor all i=1..m, Then Tenv |- (lambda (_x1 ... _xn) _b1 ..._ bm):[_S1*...*_Sn-> _Um] Typing rule Application : If Tenv |- f:[S1*...*Sn -> S], Tenv |- e1:S1, ..., Tenv |- en:Sn Then Tenv |- (f e1 ... en):S

  25. More properties Monotonicity: Type environments in typing statements in derivations can be extended. • That is, addition of type environments to a type environment does not invalidate an already derived typing statement for that type environment • If a type derivation includes Tenv |- e:T, then it can include also Tenv° {v : S} |- e:T for every variable v not in Tenv, and every type expression S. • We try to derive the “maximal” type

  26. More properties Instantiation: Every instance of a derived typing statement in a derivation is also a derived typing statement (an instance typing statement).

  27. Expression Trees The nesting of expressions can be viewed as a tree Sub-trees correspond to composite expressions For lambda expressions, their body expressions reside in their children Leaves correspond to atomic ones

  28. Expression Tree Example Tree for (+ 2 (+ 5 7)) (+ 2 (+ 5 7)) + 2 (+ 5 7) + 5 7

  29. Type Derivation (inference) Algorithm Main idea: go bottom-up on the expression tree, deriving new type statements by using the “type-statements-pool”, rules and substitution Add the result to the type-statements-pool Declare T if you get |-e:T, FAIL otherwise See exact algorithm in lecture notes!

  30. Well-typeness • Definition: If Type-derivation(e) ends with { } |- e:t, We say that e is well typed, and its type is t. The sequence of typing statements in derived-ts-pool is a type derivation for e.

  31. Example Derive a typing statement for (+ 2 ( + 5 7)) (+ 2 (+ 5 7)) + 2 (+ 5 7) + 5 7

  32. We start with the leaves: we use Number and primitives axioms. 1. { } |- 5:Number 2. { } |- 7:Number 3. { } |- 2:Number 4. { } |- +:[Number*Number -> Number]

  33. Next we deal with (+ 5 7): Application axiom. For every: type environment _Tenv, expressions _f, _e1, ..., _en, n > 0 , and type expressions _S1, ..., _Sn, _S: Procedure with parameters (n > 0): If _Tenv |- _f:[_S1*...*_Sn -> _S], _Tenv |- _e1:_S1, ..., _Tenv |- _en:_Sn Then _Tenv |- (_f _e1 ... _e):_S Application of typing rule Application to typing statements 4,1,2, with type substitution {_S1=Number, _S2=Number, _S=Number}: 5. { } |- (+ 5 7):Number

  34. Application of typing rule Application to typing statements 4,3,5, with type substitution {_S1=Number, _S2=Number, _S=Number}: 6. { } |- (+ 2 (+ 5 7)):Number Done

  35. Type-Derivation Algorithm • Input: expression e • Output: Typing statement and derivation (or FAIL)

  36. Type-Derivation Algorithm • Rename e • Let pool = {} • Apply axiom for all leaves. • Apply rules for all sub-expressions • If pool contains tsTenv |- e:t • Output: <ts, pool> • Else output FAIL

  37. Example ((λ (x) (+ x 3)) 5) (λ (x) (+ x 3)) 5 (+ x 3) + x 3

  38. Throwing in definitions 1. A definition expression (define x e) is well typed if e is well typed. 2. An expression e, that follows (occurs after) well typed definitions (define xiei) i =1…n, in which ei has type Ti, is well typed and has type S, if Type-derivation(e) outputs a typing statement Tenv |- e:S, where Tenv may include only the type assumptions xi:Ti’ for Ti’, an instance of Ti. 3. No repeated definition for a variable are allowed.

  39. Throwing in definitions (define x 1) (define y (+ x 1))

  40. Throwing in control flow (if) For every type environment _Tenv, expressions _p, _c, _a, and type expression _S: If _Tenv |- _p:Boolean and _Tenv |- _c:_S and _Tenv |- _a:_S Then _Tenv |- (if _p _c _a):S

  41. Throwing in recursion Changing the rule for (define f e): e is well typed and has type [S1*...Sn–> S], for n>0 or [Empty –> S] for n=0, if Type-derivation(e) outputs a typing statement Tenv|- e:[S1*...Sn –> S] (alternatively Tenv |- e:[Empty –> S]), where Tenv satisfies: If there are no previous well typed definitions, Tenv = {f:[S1*...Sn –> S]} for n>0, or Tenv = {f:[Empty –> S]} for n=0. If there are m previous well typed definitions (define xi ei) (m>0), in which eihas type Ti, Tenv= Tenv’ °{f:[S1*...Sn –> S]} where Tenv’ may include only type assumptions xi:Ti’ where Ti’ is an instance of Ti.

  42. Examples • On the board, also in lecture notes

  43. Type constraints approach • Create a (meta-) variable for each sub-expression • Construct type equations over these variables • 1. Numbers, booleans, quoted symbols, primitive procedures: Construct equations using their types.. • 2. Intuitive rules for lambda and applications (on the blackboard)

  44. Solving the equations 1. Apply the current substitution on the equation, i.e., replace variables by their substituting expressions. 2. If the equation includes atomic types on both sides then, if they are the same the equation is ignored, and if they are different, the type inference fails: Output FAIL. 3. If one side of the equation is a variable and the other side is not the same variable, apply the equation to the current substitution, and add the equation to the substitution. Ifa circular substitution is created, i.e., a type variable T is substituted by a type expression that includes T, output FAIL. 4. If both sides of the equation are composite and have the same type constructor, split the equation into equations between corresponding components.

  45. Example • On the board & in the notes

More Related