Presentation is loading. Please wait.

Presentation is loading. Please wait.

PPL Static Verification: Type Inference Lecture Notes: Chapter 2.

Similar presentations


Presentation on theme: "PPL Static Verification: Type Inference Lecture Notes: Chapter 2."— Presentation transcript:

1 PPL Static Verification: Type Inference Lecture Notes: Chapter 2

2

3 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

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

5 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

6 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

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

8 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)

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

10 Union What is the type of (if (= y 0) 'error (/ 1 y)) ? Symbol union Number

11 Union No value constructor Type Constrcutor is union Simplification rules – S union S = S – S union T = T union S

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

13 Void Singleton set {void} Cannot be embedded in a composite expression

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

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

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

17 Procedures and Type Polymorphism What is the type of: (lambda (f x) (f x)) [[T 1 -> T 2 ]*T 1 -> T 2 ] (lambda (f x) ( (f x) x)) [[T 1 -> [T 1 -> T 2 ]]*T 1 -> T 2 ] Instantiations, type variables renaming...

18 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

19 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

20 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} {} ◦ {x 1 :T 1,...,x n :T n } = {x 1 :T 1,...,x n :T n } – Extension pre-condition: new variables are different from old ones.

21 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:[T 1 –> T 2 ], y:T 1 } |- (x y):T 2 – A typing statement can be instantiated by consistent replacement of type variables

22 Restricted Scheme (syntax) -> -> | -> | | -> | -> Numbers -> ’#t’ | ’#f’ -> sequences of letters -> | -> ’(’ + ’)’ -> ’(’ ’lambda’ ’(’ * ’)’ + ’)’ -> ’(’ ’quote’ ’)’ For now: no ‘if’s, no ‘define’s and no recursive procedures

23 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.

24 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] …

25 Typing Rules Typing rule Procedure : For all _xi,_Si,_bi,U_i…. If Tenv ° {_x1<-_S1,..., _xn<-_Sn} |- _b:_Ui for 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

26 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

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

28 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

29 Expression Tree Example Tree for (+ 2 (+ 5 7)) (+ 2 (+ 5 7)) +(+ 5 7)2 +57

30 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!

31 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.

32 Example Derive a typing statement for (+ 2 ( + 5 7)) (+ 2 (+ 5 7)) +(+ 5 7)2 +57

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

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

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

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

37 Type-Derivation Algorithm 1.Rename e 2.Let pool = {} 3.Apply axiom for all leaves. 4.Apply rules for all sub-expressions 5.If pool contains ts Tenv |- e:t 1.Output: 2.Else output FAIL

38 Example ((λ (x) (+ x 3)) 5) 5 (λ (x) (+ x 3)) (+ x 3) 3x +

39 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 x i e i ) i =1…n, in which e i has type T i, 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 x i :T i ’ for T i ’, an instance of T i. 3. No repeated definition for a variable are allowed.

40 Throwing in definitions (define x 1) (define y (+ x 1))

41 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

42

43 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 ei has type Ti, Tenv = Tenv’ °{f:[S1*...Sn –> S]} where Tenv’ may include only type assumptions xi:Ti’ where Ti’ is an instance of Ti.

44 Examples On the board, also in lecture notes

45 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)

46 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. If a 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.

47 Example On the board & in the notes


Download ppt "PPL Static Verification: Type Inference Lecture Notes: Chapter 2."

Similar presentations


Ads by Google