CS5205: Foundation in Programming Languages Type Reconstruction

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 15 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.
1 Turing Machines and Equivalent Models Section 13.2 The Church-Turing Thesis.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
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.
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
Type checking © Marcelo d’Amorim 2010.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
CS 611 Advanced Programming Languages Andrew Myers Cornell University Lecture 27 Type inference 2 Nov 05.
1 Constraint Programming Maurizio Gabbrielli Universita’ di Bologna Slides by: K. Marriott.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
1 Chapter 1: Constraints What are they, what do they do and what can I use them for.
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.
Reading and Writing Mathematical Proofs
Copyright © 2015, 2011, 2007 Pearson Education, Inc. 1 1 Chapter 4 Systems of Linear Equations and Inequalities.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
CS321 Functional Programming 2 © JAS Type Checking Polymorphism in Haskell is implicit. ie the system can derive the types of all objects. This.
Type Systems CS Definitions Program analysis Discovering facts about programs. Dynamic analysis Program analysis by using program executions.
Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
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.
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.
1 Chapter 1: Constraints What are they, what do they do and what can I use them for.
Lecture 9 : Universal Types
Lesson 10 Type Reconstruction
Lecture 11: Proof by Reflection
Chapter 2: Lambda Calculus
CS5205: Foundations in Programming Languages
What are they, what do they do and what can I use them for.
Conditional Expressions
CS 550 Programming Languages Jeremy Johnson
WARM UP Find each equation, determine whether the indicated pair (x, y) is a solution of the equation. 2x + y = 5; (1, 3) 4x – 3y = 14; (5, 2)
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
Computability and Complexity
A lightening tour in 45 minutes
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Factorization by Cross-method
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Advanced Functional Programming
Higher-Order Procedures
Propositional Calculus: Boolean Algebra and Simplification
PROGRAMMING IN HASKELL
Complexity 6-1 The Class P Complexity Andrei Bulatov.
Typed Arithmetic Expressions
Unification Algorithm ChuChen
Theory Of Computer Science
PROGRAMMING IN HASKELL
Practice session #4: Static Type Correctness Algorithms:
Objectives Identify solutions of linear equations in two variables.
Lecture 10: Fixed Points ad Infinitum M.C. Escher, Moebius Ants
CSE S. Tanimoto Lambda Calculus
Theory of Computation Lecture 4: Programs and Computable Functions II
Algorithms CSCI 235, Spring 2019 Lecture 37 The Halting Problem
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Reasoning with Types.
Presentation transcript:

CS5205: Foundation in Programming Languages Type Reconstruction Type Variables and Susbtitutions Two View of Type Variables Constraint-Based Typing Unification Principal Types Let Polymorphism CS5205 Type Reconstruction

Type Variables and Substitutions In this lecture, we treat uninterpreted base types as type variables. A type X can stand for Nat ! Bool. We may need to substitute X by the desired type Nat ! Bool. A type substitution is a finite mapping from type variables to types. Example: s = [X a T, Y a U ] where dom(s) = {X, Y} range(s) = {T, U} CS5205 Type Reconstruction

Applying Substitutions to Types s (X) = T if (X a T) 2 s = X if X dom(s) s (Nat) = Nat s (Bool) = Bool s (T1 ! T2) = s T1 ! s T2 CS5205 Type Reconstruction

Applying Substitutions to Contexts/Terms Applying it to contexts: s (x1:T1,…,xn:Tn) = (x1: s T1,…,xn: s Tn) Applying it to terms by applying it to all its types. E.g : [X a Bool] (l x:X. x) = l x:Bool. x CS5205 Type Reconstruction

Composing Substitutions Apply g followed by s, as follows: s ± g = X a s(T) for each (X a T ) 2 g = X a T for each (X a T ) 2 s with X dom(g) CS5205 Type Reconstruction

Preservation under Type Substitution If  ` t : T then s  ` s t : s T for any type substitution s CS5205 Type Reconstruction

First View of Type Equation Solving Let t be a term with type variables, and let  be a typing context with type variables. First View: For every s there exists a T such that s  ` s t : s T . “Are all substitution instances of t well-typed?” This view leads to parametric polymorphism. CS5205 Type Reconstruction

Second View of Type Equation Solving Let t be a term with type variables, and let  be a typing context with type variables. Second View: Is there a s such that there is a T whereby s  ` s t : s T . “Is some substitution instance of t well-typed?” This view leads to type reconstruction. CS5205 Type Reconstruction

Type Reconstruction : The Problem Let t be a term and  be a typing context. A solution for (, t) is a pair (s, T) such that s  ` s t : s T CS5205 Type Reconstruction

Example Let  = f:X, a:Y and t = f a Then the possible solutions for (, t) include: ([X a Y ! Nat], Nat) ([X a Y ! Z], Z) ([X a Y ! Z, Z a Nat ], Z) ([X a Y ! Nat ! Nat], Nat ! Nat) ([X a Nat ! Nat, Y a Nat ], Nat) CS5205 Type Reconstruction

Constraint-based Typing Constraint-based typing is an algorithm that computes for (, t) a set of constraints that must be satisfied by any solution for (, t). A constraint set C is a set of solutions {Si=Ti}i 2 1..n. A substitution s unifies an equation S=T if s S and s T are identical, namely s S ´ s T. A substitution unifies (or satisfies) a constraint set C if it unifies every equation in C . CS5205 Type Reconstruction

Constraint-based Typing We define a relation  ` t : T |X C The term t has type T under assumptions  whenever the constraint C are satisfied. X is used to track variables that are introduced along the way. CS5205 Type Reconstruction

Rules for Constraint-Based Typing x :T 2   ` x : T | {} (CT-Var)  ` 0 : Nat | {} (CT-Zero)  ` t : T |X C C’=C [ {T=Nat}  ` succ t : Nat |X C’ (CT-Succ)  ` t : T |X C C’=C [ {T=Nat}  ` pred t : Nat |X C’ (CT-Pred) CS5205 Type Reconstruction

Rules for Constraint-Based Typing (CT-True)  ` true : Bool | {}  ` false : Bool | {} (CT-False)  ` t : T |X C C’=C [ {T=Nat}  ` iszero t : Bool |X C’ (CT-IsZero)  ` t1 : T1 |X1 C1  ` t2 : T2 |X2 C2  ` t3 : T3 |X3 C3 C’=C1 [ C2 [C3 [ {T1=Bool, T2 =T3} X’=X1 [ X2 [ X3  ` if t1 then t2 else t3 : T2 |X’ C’ (CT-If) CS5205 Type Reconstruction

Rules for Constraint-Based Typing , x:T1 ` t2 : T2 |X C  ` l x:T1 . t2 : T1 ! T2 |X C (CT-Abs)  ` t1 : T1 |X1 C1  ` t2 : T2 |X2 C2 fresh V C’=C1 [ C2 [ {T1=T2 ! V} X’=X1 [ X2 [ {V}  ` t1 t2 : V |X’ C’ (CT-App) Note that X1, X2 , FV(T2) , FV(T1) are disjoint. CS5205 Type Reconstruction

Constraint-based Typing (Solution) Suppose that  ` t : T |X C A solution for (,t,S,C) is a pair (s,T) such that s satisfies C and s S=T. Note that is is OK to omit X from discussion as it is simply a set of locally introduced type variables. CS5205 Type Reconstruction

Properties of Constraint-based Typing Soundness: Suppose that  ` t : T |X C . If (s,T) is a solution for (,t,S,C), then it is also a solution for (,t). That is s  ` s t : s T . Completeness: Suppose that  ` t : T |X C . If (s,T) is a solution for (,t) and dom(s) Å X={}, then there is a solution (s’,T) for (,t,S,C) such that s’ \X = s . Note that s \X is a substitution that is undefined for all variables in X, but otherwise behaves like s. CS5205 Type Reconstruction

Correctness of Constraint-based Typing Suppose  ` t : T |X C . There is some solution for (,t) if and only if there is some solution for (,t,S,C) . Correctness = Soundness + Completeness CS5205 Type Reconstruction

More General Substitution A substitution s is more general (or less specific) than a substitution s’, written as s v s’ , if s’ = g ± s for some substitution g. For example: [X a V ! V, Y a W ! W] is less specific than [X a (Nat ! Nat) ! [(Nat ! Nat) , Y a Nat ! Nat] Take g = [V a Nat ! Nat, W a Nat ]. CS5205 Type Reconstruction

Principal Unifier A principal unifier for a constraint set C is a substitution s such that: s satisfies C, and for every s’ that satisfies C, we have s v s’. That is, s is the most general substitution that satisfies C. CS5205 Type Reconstruction

Examples What is the principal unifier of the following? {X=Nat, Y= X ! X} {X ! Y= Y ! Z, Z= U ! W} ) [X a Nat, Y a Nat ! Nat] ) [X a U ! W, Y a U ! W , Z a U ! W ] CS5205 Type Reconstruction

Unification Algorithm This derives principal unifier from a set of constraint unify(C) = if C={} then [] else let {S=T} [ C’=C in if S ´ T then unify(C’) else if S ´ X Æ X FV(T) then unify([X a T]C’) ± [X a T] else if T ´ X Æ X FV(S) then unify([X a S]C’) ± [X a S] else if S ´ S1 ! S2 Æ T ´ T1 ! T2 then unify(C’ [ {S1=T1, S2=T2}) else fail occurs check CS5205 Type Reconstruction

Unification Algorithm (Properties) Let C be an arbitrary constraint set. unify(C) terminates, either with fail or by returning a substitution. If unify(C)=s then s is a unifier for C. If d is a unifier for C, then unify(C)=s for some s such that s v d. CS5205 Type Reconstruction

Principal Types A principal solution for (,t,S,C), is a solution (s,T), such that, whenever (s’,T’) is a solution for (,t,S,C), we have s v s’. When (s,T) is a principal solution, we call T a principal type for t under . CS5205 Type Reconstruction

Unification Finds Principal Solution If (,t,S,C) has any solution, then it has a principal one. The unification algorithm can be used to determine whether (,t,S,C) has a solution and, if so, to calculate a principal solution. CS5205 Type Reconstruction

Let-Polymorphism (Motivation) Consider a function that applies the first argument twice to the second argument: l f. l a. f(f(a)) This function has few assumptions on f and a. Can we apply the function, whenever these conditions are met? CS5205 Type Reconstruction

Let-Polymorphism (Example) We can use let construct to capture more generic code: let double = l f. l a. f(f(a)) in … double (l x. succ(succ(x))) 1 … … double (l x. not(x)) false … However, what type should double have? CS5205 Type Reconstruction

Let-Polymorphism (Initial Idea) Provide type variable for double: let double = l f : X ! X. l a:X. f(f(a)) in … double (l x. succ(succ(x))) 1 … … double (l x. not(x)) false … However, the let typing rule :  ` t1 : T1 , x:T1 ` t2 : T2  ` let x=t1 in t2 : T2 (T-Let) generates the following contradiction! X ! X = Nat ! Nat X ! X = Bool ! Bool CS5205 Type Reconstruction

Let-Polymorphism (Second Idea) Use implicitly annotated lambda abstraction: let double = l f . l a. f(f(a)) in … double (l x:Nat. succ(succ(x))) 1 … … double (l x:Bool. not(x)) false … Typing rule substitute all occurrences of double in body:  ` [x a t1]t2 : T2  ` let x=t1 in t2 : T2 (T-LetPoly) Problems (i) what if x not used in t2 (ii) what if x occurs multiple times CS5205 Type Reconstruction

Let-Polymorphism (Problem 1) What if x is not used in t2? Modify the type rule:  ` t1 : T1  ` [x a t1]t2 : T2  ` let x=t1 in t2 : T2 CS5205 Type Reconstruction

Let-Polymorphism (Problem 2) What if x occurs multiple times? Explicit substitution of each occurrence of variable may result in slow type-checking. Solution : use type schemes. Resulting implementations of type reconstruction run in practice in linear time. In theory, they are exponential as shown by Kfoury, Tiuryn and Urzyczyn (1990) since types can be exponential in size to program! CS5205 Type Reconstruction

Type Scheme for Let When type-checking let x=t1 in t2: Use the constraint typing rules to calculate a type S1 and a set C1 for t1. Use unification to find a most general solution  to the constraint C1 and apply  to S1 (and ) to obtain t1 as principal type T1. Generalise any variables remaining in T1. If X1..Xn are the remaining variables, we write 8 X1..Xn . T1 as the principal type scheme of t1. CS5205 Type Reconstruction

Problem with References Let-polymorphism does not work correctly with references: let r=ref (l x.x) in r:=(l x:Nat. succ x); (!r) true This results in run-time error even though it type-checks. Reason - mismatch between evaluation rule and type rule. Solution : use polymorphism only if the RHS of let is a value. CS5205 Type Reconstruction

Unification Algorithm (Background) Unification is due to J Alan Robinson (1971), and is widely used in computer science. Logic programming is based on unification over first-order terms. It is a generalization of our language of types. Unification is built-in. Occurs check is justified because we consider only finite types (ie. non-recursive types). CS5205 Type Reconstruction