Lesson 10 Type Reconstruction

Slides:



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

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.
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.
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Formal Semantics of Programming Languages 虞慧群 Topic 5: Axiomatic Semantics.
Type checking © Marcelo d’Amorim 2010.
Foundations of Programming Languages: Introduction to Lambda Calculus
CS 611 Advanced Programming Languages Andrew Myers Cornell University Lecture 27 Type inference 2 Nov 05.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Restricted Satisfiability (SAT) Problem
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.
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.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
Chapter 4: A Universal Program 1. Coding programs Example : For our programs P we have variables that are arranged in a certain order: Y 1 X 1 Z 1 X 2.
PPL Static Verification: Type Inference Lecture Notes: Chapter 2.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages.
CS321 Functional Programming 2 © JAS Type Checking Polymorphism in Haskell is implicit. ie the system can derive the types of all objects. This.
Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.
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.
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.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Lesson 3 Formalizing and Implementing Pure Lambda Calculus 1/15/02 Chapters 5.3, 6, 7.
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.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
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.
An IVP would look like Second Order Linear DE’s. Thm. Existence of a Unique Solution Let a 0, a 1, a 2, and g(x) be continuous on an interval containing.
1 Chapter 1: Constraints What are they, what do they do and what can I use them for.
Lecture 9 : Universal Types
CS5205: Foundation in Programming Languages Type Reconstruction
Chapter 2: Lambda Calculus
Lesson 5 Simple extensions of the typed lambda calculus
CS5205: Foundations in Programming Languages
What are they, what do they do and what can I use them for.
Compiler Design – CSE 504 Type Checking
Type Correctness.
CS 550 Programming Languages Jeremy Johnson
ML: a quasi-functional language with strong typing
EQUATION IN TWO VARIABLES:
A lightening tour in 45 minutes
Factorization by Cross-method
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Advanced Functional Programming
Polynomials and Polynomial Functions
Programming Languages and Compilers (CS 421)
Typed Arithmetic Expressions
Carlos Varela Rennselaer Polytechnic Institute September 6, 2016
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
FP Foundations, Scheme In Text: Chapter 14.
Equations in Two Variables
Carlos Varela Rennselaer Polytechnic Institute September 8, 2017
Advanced Functional Programming
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
This Lecture Substitution model
Solving systems of equations by substitution
Carlos Varela Rennselaer Polytechnic Institute September 8, 2015
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Carlos Varela Rennselaer Polytechnic Institute September 10, 2019
Presentation transcript:

Lesson 10 Type Reconstruction 2/26 Chapter 22

Lesson 10: Type Reconstruction substitutions typing with constraint sets (type equations) unification: solving constraint sets principlal types let polymorphism Lesson 10: Type Reconstruction

Lesson 10: Type Reconstruction Type substitutions Language: [Bool, Nat] with type variables A type substitution  is a finite mapping from type variables to types.  = [X => Nat -> Nat, Y => Bool, Z => X -> Nat] Type substitutions can be applied to types: T (X -> Z) = (Nat -> Nat) -> (X -> Nat) This extends pointwise to contexts:   Lesson 10: Type Reconstruction

Lesson 10: Type Reconstruction Type substitutions Language: [Bool, Nat] with type variables A type substitution  is a finite mapping from type variables to types.  = [X => Nat -> Nat, Y => Bool, Z => X -> Nat] Type substitutions can be applied to types: T (X -> Z) = (Nat -> Nat) -> (X -> Nat) This extends pointwise to contexts:   Composition of substitutions  o (X) =  ( X) if X  dom  =  X otherwise Lesson 10: Type Reconstruction

Substitutions and typing Thm: If  |- t: T, then  |- t: T for any type subst. . Prf: induction on type derivation for  |- t: T. Lesson 10: Type Reconstruction

"Solving" typing problems Given  and t, we can ask: For every , does there exist a T s.t.  |- t: T? Does there exist a  and a T s.t.  |- t: T? Question 1 leads to polymorphism, where T = T' and |- t: T'. The type variables are "quantified". Question 2 is the basis for type reconstuction: we think of the type variables as unknowns to be solved for. Defn: A solution for (,t) is a pair (, T) s.t.  |- t: T. Lesson 10: Type Reconstruction

Example: solutions of a typing problem (, x:X. y:Y. z:Z . (x z) (y z)) has solutions [X => Nat -> Bool -> Nat, Y => Nat -> Bool, Z => Nat] [X => X1 -> X2 -> X3, Y => X1 -> X2, Z => X1] Lesson 10: Type Reconstruction

Lesson 10: Type Reconstruction Constraints A constraint set C is a set of equations between types. C = {Si = Ti | i  1,..,n}. A substitution  unifies (or satisfies) a constraint set C if Si = Ti for every equation Si = Ti in C. A constraint typing relation  |- t: T | C  where  is a set of "fresh" type variables used in the constraint set C. This relation (or judgement) is defined by a set of inference rules. Lesson 10: Type Reconstruction

Constraint inference rules Inference rule for application |- t1: T1 | C1  1  |- t2: T2 | C2  2  1   2 =  1  FV(T2) =  2  FV(T1) =  X   1,  2, t1, t2, T1, T2, C1, C2,  C = C1  C2  {T1 = T2 -> X}  =  1   2  {X} |- t1 t2: X | C  Lesson 10: Type Reconstruction

Lesson 10: Type Reconstruction Constraint solutions Defn: Suppose  |- t: S | C . A solution for (, t, S, C) is a pair (, T) s.t.  satisfies C and T = S. Thm: [Soundness of Constraint Typing] Suppose  |- t: S | C X. If (, T) is a solution for (, t, S, C) then it is also a solution for (, t), i.e.  |- t: T. Thm: [Completeness of Constraint Typing] Suppose  |- t: S | C . If (, T) is a solution for (, t) then there is a solution (', T) for (, t, S, C) s.t. '\ = . Cor: Suppose  |- t: S | C . There is a soln for (, t) iff there is a solution for (, t, S, C). Lesson 10: Type Reconstruction

Lesson 10: Type Reconstruction Unification Defn:  < ' if ' =  o  for some . Defn: A principle unifier (most general unifier) for a constraint set C is a substitution  that satisfies C s.t.  < ' for any other ' that satifies C. Lesson 10: Type Reconstruction

Unification algorithm unify C = if C =  then [ ] else let {S = T}  C' = C in if S = T then unify(C') else if S = X and X  FV(T) then unify([X => T]C') o [X => T] else if T = X and X  FV(S) then unify([X => S]C') o [X => S] else if S = S1 -> S2 and T = T1 -> T2 then unify(C'  {S1 = T1, S2 = T2}) else fail Thm: unify always terminates, and either fails or returns the principal unifier if a unifier exists. Lesson 10: Type Reconstruction

Lesson 10: Type Reconstruction Principal Types Defn: A principal solution for (, t, S, C) is a solution (, T) s.t. for any other solution (', T') we gave  < '. Thm: [Principal Types] If (, t, S, C) has a solution, then it has a principal one. The unify algorithm can be used to determine whether (, t, S, C) has a solution, and if so it calculates a principal one. Lesson 10: Type Reconstruction

Lesson 10: Type Reconstruction Implicit Annotations We can extend the syntax to allow lambda abstractions without type annotations: x.t. The corresponding type constraint rule supplies a fresh type variable as an implicit annotation. X   , x: X |- t1: T | C  (CT-AbsInf) , |- x: X. t1: X -> T | C (  {X}) Lesson 10: Type Reconstruction

Lesson 10: Type Reconstruction Let Polymorphism let double = f: Nat -> Nat. x: Nat. f(f x) in double (x: Nat. succ x) 2 let double = f: Bool -> Bool. x: Bool. f(f x) in double (x: not x) false An attempt at a generic double: let double = f: X -> X. x: X. f(f x) in let a = double (x: Nat. succ x) 2 in let b = double (x: not x) false ==> X -> X = Nat -> Nat = Bool -> Bool Lesson 10: Type Reconstruction

Lesson 10: Type Reconstruction Macro-like let rule let double = f: X -> X. x: X. f(f x) in let a = double (x: Nat. succ x) 2 in let b = double (x: not x) false could be typed as: let a = (f: X -> X. x: X. f(f x)) (x: Nat. succ x) 2 in let b = (f: X' -> X'. x: X'. f(f x)) (x: not x) false or, using implicit type annotations: let a = (f. x. f(f x)) (x: Nat. succ x) 2 in let b = (f. x. f(f x)) (x: not x) false Lesson 10: Type Reconstruction

Lesson 10: Type Reconstruction Macro-like let rule  |- t1: T1  |- [x => t1]t2: T2 (T-LetPoly)  |- let x = t1 in t2: T2 The substitution can create multiple independent copies of t1, each of which can be typed independently (assuming implicit annotations, which introduce separate type variables for each copy). Lesson 10: Type Reconstruction

Lesson 10: Type Reconstruction Type schemes Add quantified type schemes: T ::= X | Bool | Nat | T -> T P ::= T | X . P Contexts become finite mappings from term variables to type schemes:  ::=  | , x : P Examples of type schemes: Nat, X -> Nat, X. X -> Nat, X.Y. X -> Y -> X Lesson 10: Type Reconstruction

let-polymorphism rules  |- t1: T1 , x :  .T1 |- t2: T2 (T-LetPoly)  |- let x = t1 in t2: T2 where  ' are the type variables free in T1 but not free in  (T-PolyInst) , x :  .T |- x: [ =>  ']T where  ' is a set of fresh type variables Lesson 10: Type Reconstruction

let-polymorphism example double : X. (X -> X) -> X -> X let double = f. x. f(f x) in let a = double (x: Nat. succ x) 2 in let b = double (x: not x) false in (a,b) (Y -> Y) -> Y -> Y (Z -> Z) -> Z -> Z Then unification yields [Y => Nat, Z => Bool]. Lesson 10: Type Reconstruction

let-polymorphism and references Let ref, !, and := be polymorphic functions with types ref : X. X -> Ref(X) ! : X. Ref(X) -> X := : X. Ref(X) *X -> Unit let r = ref(x. x) in let a = r := (x: Nat. succ x) in let b = !r false in () r : X. Ref(X -> X) Ref(Nat -> Nat) Ref(Bool -> Bool) We've managed to apply (x: Nat. succ x) to false! Lesson 10: Type Reconstruction

The value restriction We correct this unsoundness by only allowing polymorphic generalization at let declarations if the expression is a value. This is called the value restriction. let r = ref(x. x) in let a = r := (x: Nat. succ x) in let b = !r false in () r : Ref(X -> X) Ref(Nat -> Nat) [X => Nat] Ref(Nat -> Nat) Now we get a type error in " !r false ". Lesson 10: Type Reconstruction

Let polymorphism with recursive values Another problem comes when we add recursive value definitions. let rec f = x. t in ... is typed as though it were written let f = fix(f. x. t) in ... where fix : X. (X -> X) -> X except that the type of the outer f can be generalized. Note that the inner f is -bound, not let bound, so it cannot be polymorphic within the body t. Lesson 10: Type Reconstruction

Polymorphic Recursion What can we do about recursive function definitions where the function is polymorphic and is used polymorphically in the body of it's definition? (This is called polymorphic recursion.) let rec f = x. (f true; f 3; x) Have to use a fancier form of type reconstruction: the iterative Mycroft-Milner algorithm. Lesson 10: Type Reconstruction