Type Inference; Parametric Polymorphism; Records and Subtyping Section and Practice Problems Mar 20-23, 2018

Similar documents
Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Harvard School of Engineering and Applied Sciences Computer Science 152

CSE-321 Programming Languages 2011 Final

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

We defined congruence rules that determine the order of evaluation, using the following evaluation

The Polymorphic Blame Calculus and Parametricity

CSE-321 Programming Languages 2012 Midterm

CSE-321 Programming Languages 2010 Midterm

Dynamic Types, Concurrency, Type and effect system Section and Practice Problems Apr 24 27, 2018

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Programming Languages Lecture 15: Recursive Types & Subtyping

Programming Languages

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

CSE-321 Programming Languages 2014 Final

CSE-321 Programming Languages 2010 Final

If a program is well-typed, then a type can be inferred. For example, consider the program

Mutable References. Chapter 1

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

λ calculus is inconsistent

Functional Languages and Higher-Order Functions

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

Introduction to System F. Lecture 18 CS 565 4/20/09

Chapter 5: The Untyped Lambda Calculus

Lecture 13: Subtyping

CIS 500 Software Foundations Midterm I

Gradual Typing with Inference

Lambda Calculus. Lambda Calculus

Introduction to Lambda Calculus. Lecture 7 CS /08/09

Type Systems. Parametric Polymorphism. 1. Recall Let-Polymorphism. 1. Recall Let-Polymorphism. Lecture 9 Dec. 15th, 2004 Sebastian Maneth

Symmetry in Type Theory

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

Concepts of programming languages

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012

Second-Order Type Systems

1 Introduction. 3 Syntax

Mathematical Programming and Research Methods (Part II)

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Lecture 9: Typed Lambda Calculus

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

CIS 500 Software Foundations Fall September 25

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

Lambda Calculus. Lecture 4 CS /26/10

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

Supplementary Notes on Recursive Types

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

Inductive datatypes in HOL. lessons learned in Formal-Logic Engineering

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Types. Lecture Notes on. for Part II of the Computer Science Tripos. Prof. Andrew M. Pitts University of Cambridge Computer Laboratory

Chapter 22: Type Reconstruction (Type Inference)

CSE-505: Programming Languages. Lecture 20.5 Recursive Types. Zach Tatlock 2016

Let Arguments Go First

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

Polymorphism. Lecture 19 CS 565 4/17/08

Lecture #3: Lambda Calculus. Last modified: Sat Mar 25 04:05: CS198: Extra Lecture #3 1

Lecture #23: Conversion and Type Inference

Fundamentals and lambda calculus

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Lambda Calculus and Type Inference

Advances in Programming Languages

Abstract register machines Lecture 23 Tuesday, April 19, 2016

Exercise 1 ( = 18 points)

CMSC 330: Organization of Programming Languages

CSE505, Fall 2012, Midterm Examination October 30, 2012

Linking Types: Secure compilation of multi-language programs

CMSC 330: Organization of Programming Languages. Lambda Calculus

Let Arguments Go First

Type Systems Winter Semester 2006

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Lecture Notes on Data Representation

Calculus INT. Value Types A, B ::= types of the low-level language Interactive Types X, Y ::= [A] A X X Y A X

Modelling Unique and Affine Typing using Polymorphism

High-performance defunctionalization in Futhark

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

Subsumption. Principle of safe substitution

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Exercise 1 ( = 24 points)

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

Collage of Static Analysis

Verifying Program Invariants with Refinement Types

Advanced Topics in Programming Languages Lecture 3 - Models of Computation

Variables. Substitution

Functional programming languages

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

CMSC 330: Organization of Programming Languages. Lambda Calculus

Introductory Example

Uniqueness Typing Redefined

CSE 505, Fall 2007, Final Examination 10 December Please do not turn the page until everyone is ready.

Lambda calculus. Chapter 2

Official Survey. Cunning Plan: Focus On Objects. The Need for a Calculus. Object Calculi Summary. Why Not Use λ-calculus for OO?

CSE 505, Fall 2006, Final Examination 11 December Please do not turn the page until everyone is ready.

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

Lecture 5: Lazy Evaluation and Infinite Data Structures

Part III. Chapter 15: Subtyping

Transcription:

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Type Inference; Parametric Polymorphism; Records and Subtyping Mar 20-23, 2018 1 Type Inference (a) Recall the constraint-based typing judgment. Give inference rules for products and sums. That is, for the following expressions. (e 1, e 2 ) #1 e #2 e inl τ1+τ 2 e inr τ1+τ 2 e case e 1 of e 2 e 3 Note that in all of the rules below except for the rule for pairs (e 1, e 2 ), the types in the premise and conclusion are connected only through constraints. The reason for this is the same as in the typing rule for function application, and for addition: we may not be able to derive that the premise has the appropriate type, e.g., for a projection #1 e, we may not be able to derive that Γ e:τ 1 τ 2 C. We instead use constraints to ensure that the derived type is appropriate. Γ e 1 :τ 1 C 1 Γ e 2 :τ 2 C 2 Γ (e 1, e 2 ):τ 1 τ 2 C 1 C 2 Γ #1 e:x C {τ X Y } X, Y are fresh Γ e:τ C Γ #2 e:y C {τ X Y } X, Y are fresh Γ inl τ1+τ 2 e:τ 1 + τ 2 C {τ τ 1 } Γ inr τ1+τ 2 e:τ 1 + τ 2 C {τ τ 2 } Γ e 1 :τ 1 C 1 Γ e 2 :τ 2 C 2 Γ e 3 :τ 3 C 3 Γ case e 1 of e 2 e 3 :Z C 1 C 2 C 3 {τ 1 X + Y, τ 2 X Z, τ 3 Y Z} X, Y, Z are fresh (b) Determine a set of constraints C and type τ such that λx:a. λy :B. (#1 y) + (x (#2 y)) + (x 2) :τ C and give the derivation for it.

C = {B X Y, X int, B Z W, A W U, U int, A int V, V int} τ A B int To see how we got these constraints, we will consider the subexpressions in turn (rather than trying to typeset a really really big derivation). The expression #1 y requires us to add a constraint that the type of y (i.e., B) is equal to a product type for some fresh variables X and Y, thus constraint B X Y. (And expression #1 y has type X.) The expression (#2 y) similarly requires us to add a constraint that the type of y (i.e., B) is equal to a product type for some fresh variables Z and W, thus constraint B Z W. (And expression #2 y has type W.) The expression x (#2 y) requires us to add a constraint that unifies the type of x (i.e., A) with a function type W U (where W is the type of #2 y and U is a fresh type variable). The expression x 2 requires us to add a constraint that unifies the type of x (i.e., A) with a function type int V (where int is the type of expression 2 and V is a fresh type). The addition operations leads us to add constraints X int, U int, and V int (i.e., the types of expressions (#1 y), (x (#2 y)) and (x 2) must all unify with int. (c) Recall the unification algorithm from Lecture 14. What is the result of unify(c) for the set of constraints C from Question 1(b) above? The result is a substitution equivalent to [A int int, B int int, X int, Y int, Z int, W int, U int, V int] 2 Parametric polymorphism (a) For each of the following System F expressions, is the expression well-typed, and if so, what type does it have? (If you are unsure, try to construct a typing derivation. Make sure you understand the typing rules.) ΛA. λx:a int. 42 λy : X. X X. (y [int]) 17 ΛY. ΛZ. λf :Y Z. λa:y. f a ΛA. ΛB. ΛC. λf :A B C. λb:b. λa:a. f a b ΛA. λx:a int. 42 has type A. (A int) int λy : X. X X. (y [int]) 17 has type ( X. X X) int Page 2 of 5

ΛY. ΛZ. λf :Y Z. λa:y. f a has type Y. Z. (Y Z) Y Z ΛA. ΛB. ΛC. λf :A B C. λb:b. λa:a. f a b has type A. B. C. (A B C) B A C (b) For each of the following types, write an expression with that type. X. X (X X) ( C. D. C D) ( E. int E) X. X ( Y. Y X) X. X (X X) is the type of ΛX. λx:x. λy :X. y ( C. D. C D) ( E. int E) is the type of λf : C. D. C D. ΛE. λx:int. (f [int] [E]) x X. X ( Y. Y X) is the type of ΛX. λx:x. ΛY. λy :Y. x 3 Records and Subtyping (a) Assume that we have a language with references and records. (i) Write an expression with type { cell : int ref, inc : unit int } such that invoking the function in the field inc will increment the contents of the reference in the field cell. The following expression has the appropriate type. let x = ref 14 in { cell = x, inc = λu:unit. x := (!x + 1) } (ii) Assuming that the variable y is bound to the expression you wrote for part (i) above, write an expression that increments the contents of the cell twice. let z = y.inc () in y.inc () Page 3 of 5

(b) The following expression is well-typed (with type int). Show its typing derivation. (Note: you will need to use the subsumption rule.) (λx:{dogs : int, cats : int}. x.dogs + x.cats) {dogs = 2, cats = 7, mice = 19} For brevity, let e 1 λx:{dogs : int, cats : int}. x.dogs+x.cats) and let e 2 {dogs = 2, cats = 7, mice = 19}. The derivation has the following form. T-APP. 1 e 1 :{dogs : int, cats : int} int e 1 e 2 :int. 2 e 2 :{dogs : int, cats : int} The derivation of e 1 is straight forward: T-VAR x : {dogs : int, cats : int} x : {dogs : int, cats : int} x : {dogs : int, cats : int} x.dogs : int T-VAR T-FIELD x : {dogs : int, cats : int} x.dogs + x.cats : int e 1 : {dogs : int, cats : int} int x : {dogs : int, cats : int} x : {dogs : int, cats : int} x : {dogs : int, cats : int} x.cats : int T-FIELD T-ADD T-ABS Page 4 of 5

The derivation of e 2 requires the use of subsumption, since we need to show that e 2 {dogs = 2, cats = 7, mice = 19} has type {dogs : int, cats : int}. 2 : int 7 : int 19 : int {dogs = 2, cats = 7, mice = 19} : {dogs : int, cats : int, mice : int} {dogs : int, cats : int, mice : int} {dogs : int, cats : int} {dogs = 2, cats = 7, mice = 19} : {dogs : int, cats : int} Page 5 of 5