CS 611 Advanced Programming Languages Andrew Myers Cornell University Lecture 27 Type inference 2 Nov 05.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Functional Programming Lecture 10 - type checking.
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Letrec fact(n) = if zero?(n) then 1 else *(n, (fact sub1(n))) 4.4 Type Inference Type declarations aren't always necessary. In our toy typed language,
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
CSE-321 Programming Languages Predicative Polymorphic -Calculus POSTECH May 23, 2007 박성우.
Catriel Beeri Pls/Winter 2004/5 last 55 Two comments on let polymorphism I. What is the (time, space) complexity of type reconstruction? In practice –
1 CS 312 – Lecture 28 Continuations –Probably the most confusing thing you’ve seen all semester… Course summary –Life after CS 312.
10 December 2002BU CAS CS520 1 Principles of Programming Languages (Final Review) Hongwei Xi Comp. Sci. Dept. Boston University.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
6/18/2015 4:21 AM Information Flow James Hook CS 591: Introduction to Computer Security.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
1 Combining verification and analysis. 2 CONCLUSIONS ON VERIFICATION  denotational abstract interpreters have the extra-value of being easily transformed.
Type Inference II David Walker COS 441. Type Inference Goal: Given unannotated program, find its type or report it does not type check Overview: generate.
Midterm & Concept Review CS 510, Fall 2002 David Walker.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CS 3240: Languages and Computation Context-Free Languages.
Type checking and inference Applications of typing axioms / rules are replaced with type equations. A solution to the equations assigns types for every.
Type Inference II David Walker COS 441. Type Inference Goal: Given unannotated program, find its type or report it does not type check Overview: generate.
CS 611: Lecture 6 Rule Induction September 8, 1999 Cornell University Computer Science Department Andrew Myers.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
2/6/20161 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
ONE STEP EQUATIONS Multiplication and Division ONE STEP EQUATIONS Example 1 Solve 3x = 12 Variable? x 3x = 12 x x 3 ÷ 3 Inverse operation? Operation?
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Notes 6.5, Date__________ (Substitution). To solve using Substitution: 1.Solve one equation for one variable (choose the variable with a coefficient of.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Lecture 9 : Universal Types
Lesson 10 Type Reconstruction
CS5205: Foundation in Programming Languages Type Reconstruction
CS5205: Foundations in Programming Languages
Compiler Design – CSE 504 Type Checking
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 11 Type Inference
Equations Quadratic in form factorable equations
ML: a quasi-functional language with strong typing
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
Programming Languages and Compilers (CS 421)
Solve a system of linear equation in two variables
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Advanced Functional Programming
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
CSE341: Programming Languages Lecture 11 Type Inference
CS 242 Types John Mitchell.
CS 242 Types John Mitchell Reading: Chapter 6.
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Equations Quadratic in form factorable equations
Solving Systems by Substitution
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Presentation transcript:

CS 611 Advanced Programming Languages Andrew Myers Cornell University Lecture 27 Type inference 2 Nov 05

Cornell University CS 611 Fall'04 -- Andrew Myers2 Type inference (reconstruction) Simple typed language: e ::= x | b | x: . e | e 1 e 2 | e 1 + e 2 | if e 0 then e 1 else e 2 | let x=e 1 in e 2 | rec y:  1  2.( x.e)  ::= unit | bool | int |  1  2 Question: Do we really need to write type declarations? e ::= … | x.e | …| rec y.( x.e)

Cornell University CS 611 Fall'04 -- Andrew Myers3 Typing rules e ::= x | b | x.e | e 1 e 2 | e 1  e 2 | if e 0 then e 1 else e 2 | let x=e 1 in e 2 | rec y. x.e  x:   e :     x. e :    y:    x:   e :     rec y. x. e :   Problem: how does type checker construct proof? Guess   

Cornell University CS 611 Fall'04 -- Andrew Myers4 Example let square = z.z*z in ( f. x. y. if (f x y) then (f (square x) y) else (f x (f x y))) What is the type of this program?

Cornell University CS 611 Fall'04 -- Andrew Myers5 Manual type inference let square = z.z*z in ( f. x. y. if (f x y) then (f (square x) y) else (f x (f x y))) z : int s, square : int  int f :  x  y  bool y :  y = bool x :  x = int Answer: (int  bool  bool)  int  bool  bool

Cornell University CS 611 Fall'04 -- Andrew Myers6 Type inference Goal: reconstruct types even after erasure Idea: run ordinary type-checking algorithm, generate type equations on type variables f:T2, x:T5  f : int  T6f:T2, x:T5  1 : int f:T2, x:T5  f 1 : T6 f:T2  x. f 1 : T1 (=T5  T6) y:T3  y : T4  f. x. f 1 : T2  T1  ( y.y) : T2 (T2=T3  T4)  ( f. x. (f 1)) ( y.y) : T1 T2 = T3  T4, T3 = T4, T1 =T5  T6, T2 = int  T6 (T3=T4)

Cornell University CS 611 Fall'04 -- Andrew Myers7   e 0 :  0   e 1 :  1  0 =  1     e 0 e 1 : T 2 Typing rules  x:T x   e :    x. e : T x    n : int   e 0 :  0   e 1 :  1   e 2 :  2  1 =  2,  0 = bool   if e 0 then e 1 else e 2 :  1   e 1 :  1 , x :  1  e 2 :  2   let x =e 1 in e 2 :  2  x:T 1, y:T 1  T 2  e :  T 2   rec y. x.e : T 1  T 2 Only type metavariables on RHS of premises x  dom(    x :  (x)

Cornell University CS 611 Fall'04 -- Andrew Myers8 Unification How to solve equations? Idea: given equation   =  , unify type expressions to solve for variables in both Example:    int = (bool   )   Result: substitution    bool  ,    int   int =   bool  

Cornell University CS 611 Fall'04 -- Andrew Myers9 Robinson’s algorithm (1965) Unification produces weakest substitution that equates two trees –    int = (bool   )   equated by any    bool  ,    int,     –Defn. S 1 is weaker than S 2 if S 2 = S 3  S 1 for S 3 a non-trivial substitution Unify(E) where E is set of equations gives weakest equating substitution: define recursively Unify(T =  E) = Unify(E{  /T})  [T   ] (if T  FTV(  ) 

Cornell University CS 611 Fall'04 -- Andrew Myers10 Rest of algorithm Unify(T =  E) = Unify(E{  /T})  [T   ] (if T  FTV     Unify(  ) =  Unify(B = B, E) = Unify(E) Unify(B 1 = B 2, E) = ? Unify(T = T, E) = Unify(E) Unify(        , E) = Unify(    ,    , E) Well-founded? Degree = (#vars, size(eqns))

Cornell University CS 611 Fall'04 -- Andrew Myers11 Type inference algorithm R (e, , S) =  , S  means “Reconstructing the type of e in typing context  with respect to substitution S yields type , identical or stronger substitution S  or “S  is weakest substitution no weaker than than S such that S  (  )    e  S  (  )” Define: Unify(E, S) = Unify(SE)  S –solve substituted equations E and fold in new substitutions

Cornell University CS 611 Fall'04 -- Andrew Myers12 Inductive defn of inference R (e, , S) =  , S   “S  is weakest substitution stronger than (or same as) S such that S  (  )    e  S  (  )” Unify(E, S) = Unify(SE)  S R (n, , S) =  int, S  R ( true, , S) =  bool, S  R (x, , S) =   (x), S  R (e 1 e 2, , S) = let  T1, S1  = R (e 1, , S) in let  T2, S2  = R (e 2, , S1) in  T f, Unify(T1=T2  T f, S2)  R ( x.e, , S) = let  T1, S1  = R (e,  x  T f , S) in  T f  T1, S1  where T f is “fresh” (not mentioned anywhere in e, , S)

Cornell University CS 611 Fall'04 -- Andrew Myers13 Example R (( x.x) 1, ,  ) = let  T1, S1  = R ( x.x, ,  ) in let  T2, S2  = R (1, , S1) in  T3, Unify(T1  T3 = T2, S2)  R ( x.x, ,  ) = let  T1, S1  = R (x,  x  T4 ,  ) in  T4  T1, S1  =  T4  T4,   = let  T2, S2  = R (1, ,  ) in  T3, Unify(T2  T3 = T4  T4,  )  =  T3, Unify(int  T3 = T4  T4,  )  =  T3, Unify(int=T4, T3 = T4,  )  =  T3, Unify(T3 = int,  T4  int])  =  T3, [T3  int, T4  int]) 

Cornell University CS 611 Fall'04 -- Andrew Myers14 Implementation Can implement with imperative update: datatype type = Int | Bool | Arrow of type * type | TypeVar of type option ref fun freshTypeVar() = TypeVar(ref NONE) fun resolve(t: type) = case t of TypeVar(ref (SOME t’)) => t’ | _ => t fun unify(t1: type, t2: type) : unit = case (resolve t1, resolve t2) of (TypeVar(r as ref NONE), t2) => r := t2 | (t1, TypeVar(r as ref NONE)) => r := t1 | (Arrow(t1,t2), Arrow(t3,t4)) => unify(t1,t3); unify(t2,t4) | (Int, Int) => () | (Bool,Bool) => () | _ => raise Fail “Can’t unify types”

Cornell University CS 611 Fall'04 -- Andrew Myers15 Polymorphism R ( x.x, ,  ) = let  T1, S1  = R (x,  x  T4 ,  ) in  T4  T1, S1  =  T4  T4,   Reconstruction algorithm doesn’t solve type fully… opportunity! x.x can have type T4  T4 for any T4 –polymorphic (= “many shape”) term –Could reuse same expression multiple places in program, with different types: let id = ( x.x) in … (f id) … (g x id) … id