CSE-321 Programming Languages 2010 Final

Similar documents
CSE-321 Programming Languages 2011 Final

CSE-321 Programming Languages 2014 Final

CSE-321 Programming Languages 2012 Midterm

Mutable References. Chapter 1

CSE-321 Programming Languages 2010 Midterm

Programming Languages Lecture 14: Sum, Product, Recursive Types

Programming Languages Lecture 15: Recursive Types & Subtyping

Programming Languages

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

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

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

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

1 Introduction. 3 Syntax

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

Concepts of programming languages

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

Harvard School of Engineering and Applied Sciences Computer Science 152

Lambda Calculus. Lecture 4 CS /26/10

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

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

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

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

CSE 505: Concepts of Programming Languages

Type-indexed functions in Generic Haskell

Typed Lambda Calculus and Exception Handling

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

CIS 500 Software Foundations Midterm I

References and Exceptions. CS 565 Lecture 14 4/1/08

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

Lesson 4 Typed Arithmetic Typed Lambda Calculus

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

Let Arguments Go First

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

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

MLW. Henk Barendregt and Freek Wiedijk assisted by Andrew Polonsky. March 26, Radboud University Nijmegen

dynamically typed dynamically scoped

λ calculus is inconsistent

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

The Polymorphic Blame Calculus and Parametricity

Arbitrary-rank polymorphism in (GHC) Haskell

The Untyped Lambda Calculus

Exercise 1 (2+2+2 points)

The Typed λ Calculus and Type Inferencing in ML

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

Lecture #23: Conversion and Type Inference

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

Lecture Notes on Data Representation

Chapter 5: The Untyped Lambda Calculus

ML F Raising ML to the Power of System F

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

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Lambda Calculus and Extensions as Foundation of Functional Programming

Types and Programming Languages. Lecture 5. Extensions of simple types

More Lambda Calculus and Intro to Type Systems

上海交通大学试卷 ( A 卷 ) ( 2015 至 2016 学年第 2 学期 )

The Untyped Lambda Calculus

Lecture Note: Types. 1 Introduction 2. 2 Simple Types 3. 3 Type Soundness 6. 4 Recursive Types Subtyping 17

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

Subtyping. Lecture 13 CS 565 3/27/06

Formal Systems and their Applications

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

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

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

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

CIS 500 Software Foundations Fall October 2

CSE505, Fall 2012, Midterm Examination October 30, 2012

Second-Order Type Systems

The Untyped Lambda Calculus

Where is ML type inference headed?

Symmetry in Type Theory

1 Scope, Bound and Free Occurrences, Closed Terms

Exercise 1 ( = 24 points)

Type Checking and Type Inference

Let Arguments Go First

Lambda Calculus and Type Inference

Intersections and Unions of Session Types

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Higher-Order Intensional Type Analysis. Stephanie Weirich Cornell University

Unifying Nominal and Structural Ad-Hoc Polymorphism

4.5 Pure Linear Functional Programming

4.6 Recursive Types. 4.6 Recursive Types 105

CSc 520 final exam Wednesday 13 December 2000 TIME = 2 hours

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

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

Polymorphism. Lecture 19 CS 565 4/17/08

Programming Languages Fall 2014

Lecture 5: The Untyped λ-calculus

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

Recursive Types and Subtyping

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Gradual Typing with Union and Intersection Types

Part III. Chapter 15: Subtyping

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Fundamentals and lambda calculus

Lambda Calculus. Variables and Functions. cs3723 1

Preuves Interactives et Applications

Polymorphism and System-F (OV)

Transcription:

Name: Hemos ID: CSE-321 Programming Languages 2010 Final Prob 1 Prob 2 Prob 3 Prob 4 Prob 5 Prob 6 Total Score Max 18 28 16 12 36 40 150 There are six problems on 16 pages, including two work sheets, in this exam. The maximum score for this exam is 150 points, and there is an extracredit problem. Be sure to write your name and Hemos ID. You have three hours for this exam. Instructor-Thank-Students-Problem [Extracredit] State Yes if you attended all the lectures in this course, without missing a single lecture. PL 2010 λ [Extracredit] State Yes if you wear the PL 2010 λ T-shirt. PL 2010 Tekken Match [Extracredit] State Yes if you played in PL 2010 Tekken Match. Who did you beat in PL 2010 Tekken Match? 1

1 Mutable references [18 pts] Consider the following simply-typed λ-calculus extended with mutable references. type A ::= P A A int ref A expression e ::= x λx:a. e e e let x = e in e ref e!e e := e 0 1 value v ::= λx:a. e l 0 1 store ψ ::= ψ, l v typing context Γ :; = Γ, x : A store typing context Ψ ::= Ψ, l A Question 1. [8 pts] We want to represent an array of integers as a function taking an index (of type int) and returning a corresponding elements of the array. We choose a functional representation of arrays by defining type iarray for arrays of integers as follows: We need the following constructs for arrays: iarray = ref (int int) new : unit iarray for creating a new array. new () returns a new array of indefinite size; all elements are initialized as 0. access : iarray int int for accessing an array. access a i returns the i-th element of array a. update : iarray int int unit for updating an array. update a i n updates the i-the element of array a with integer n. Exploit the constructs for mutable references to implement new, access and update. Fill in the blank: new = λ :unit. ref λi:int. 0 access = λa:iarray. λi:int. (!a) i update = λa:iarray. λi:int. λn:int. let old =!a in a := λj :int. if i = j then n else old j 2

Question 2. [10 pts] State progress and type preservation theorems. In your statements, use the following judgments: A typing judgment Γ Ψ e : A means that expression e has type A under typing context Γ and store typing context Ψ. A reduction judgment e ψ e ψ means that e with store ψ reduces to e with ψ. A store judgment ψ :: Ψ means that Ψ corresponds to ψ. Theorem (Progress). Suppose that expression e satisfies Ψ e : A for some store typing context Ψ and type A. Then either: (1) e is a value, or (2) for any store ψ such that ψ :: Ψ, there exist some expression e and store ψ such that e ψ e ψ. Γ Ψ e : A Theorem (Type preservation). Suppose ψ :: Ψ. e ψ e ψ Γ Ψ e : A Then there exists a store typing context Ψ such that Ψ Ψ. ψ :: Ψ 3

2 Evaluation context and environment [28 pts] Consider the following fragment of the simply-typed λ-calculus. type A ::= P A A base type P ::= bool expression e ::= x λx:a. e e e true false if e then e else e Question 1. [5 pts] Give the definition of evaluation contexts for the call-by-value strategy. evaluation context κ ::= Question 2. [5 pts] Give the definition of evaluation contexts for the call-by-name strategy. evaluation context κ ::= Question 3. [5 pts] Under the call-by-value strategy, give an expression e such that e = κ e where e is the redex, and e reduces to e 0 that is decomposed to κ e where e is the redex for the next reduction. Question 4. [5 pts] Under the call-by-value strategy, give an expression e such that e = κ e where e is the redex, and e reduces to e 0 that is decomposed to κ e where e is the redex for the next reduction and κ κ. 4

Question 5. [8 pts] The key idea behind the environment semantics is to postpone a substitution [v/x]e by storing a pair of value v and variable x in an environment. We use the following definition of environment: environment η ::= η, x v denotes an empty environment, and x v means that variable x is to be replaced by value v. We use an environment evaluation judgment of the form η e v: η e v e evaluates to v under environment η Give the definition of values for the simply-typed λ-calculus given in the beginning of this section. value v ::= Complete the following three rules for the environment evaluation judgment η e v corresponding to the call-by-value strategy. η x η λx:a. e η e 1 e 2 5

3 Subtyping [16 pts] Question 1. [6 pts] Complete subtyping rules for function and reference types. A B A B Fun ref A ref B Ref Question 2. [10 pts] The Java language adopts the following subtyping rule for array types: A B array A array B Array While it is controversial whether the rule Array is a flaw in the design of the Java language, using the rule Array for subtyping on array types incurs a runtime overhead which would otherwise be unnecessary. State specifically when and why such runtime overhead occurs in terms of dynamic tag-checks which inspect type information of each object at runtime. You may write in Korean. 6

4 Recursive types [12 pts] Consider the following simply-typed λ-calculus extended with recursive types: type A ::= unit A A A+A α µα.a expression e ::= x λx:a. e e e inl A e inr A e case e of inl x. e inr y. e fold C e unfold C e typing context Γ ::= Γ, x : A Γ, α type Question 1. [6 pts] Give typing rules for fold C e and unfold C e. C = µα.a Γ fold C e : Γ C type Fold C = µα.a Γ unfold C e : Unfold Question 2. [6 pts] Consider the following recursive datatype for natural numbers: datatype nat = Zero Succ of nat Using a recursive type, we encode type nat as µα.unit+α. Encode Zero and Succ e. Zero = Succ e = 7

Question 3. [Extracredit] We want to translate an expression e in the untyped λ-calculus into an expression e in the simply typed λ-calculus extended with recursive types. We treat all expressions in the untyped λ-calculus alike by assigning a unique type Ω (i.e., e is to have type Ω). If every expression is assigned type Ω, we may think that λx. e is assigned type Ω Ω as well as type Ω. Or, in order for e 1 e 2 to be assigned type Ω, e 1 must be assigned not only type Ω but also type Ω Ω because e 2 is assigned type Ω. Thus Ω must be identified with Ω Ω. Use recursive types and their constructs to complete the definition of Ω and e. Fill in the blank: Ω = x = x (λx. e) = (e 1 e 2 ) = 8

5 Polymorphism (36 pts) The following shows the abstract syntax for System F: type A ::= A A α α.a expression e ::= x λx:a. e e e Λα. e e A Below we define an erasure function erase( ) which takes an expression in System F and erases all type annotations in it to produce a corresponding expression in untyped λ-calculus: erase(x) = x erase(λx: A. e) = λx. erase(e) erase(e 1 e 2 ) = erase(e 1 ) erase(e 2 ) erase(λα. e) = erase(e) erase(e A ) = erase(e) Question 1. [5 pts] Give a well-typed closed expression e in System F such that erase(e) = λx. x x. If there is no such expression, state so. Question 2. [5 pts] Give a well-typed closed expression e in System F such that erase(e) = (λx. x x) (λx. x x). If there is no such expression, state so. Question 3. [6 pts] A Church numeral n takes a function f and returns another function f n which applies f exactly n times. In order for f n to be well-typed, its argument type and return type must be identical. Hence we define the base type nat in System F as follows: nat = α.(α α) (α α) Encode a zero zero of type nat and a successor function succ of type nat nat: zero = succ = 9

The following shows the abstract syntax for the let-polymorphism system: monotype A ::= A A α polytype U ::= A α.u expression e ::= x λx:a. e e e Λα. e e A let x:u = e in e Below we define an erasure function erase( ) which takes an expression in the let-polymorphism system and erases all type annotations in it to produce a corresponding expression in the implicit let-polymorphism system: erase(x) = x erase(λx: A. e) = λx. erase(e) erase(e 1 e 2 ) = erase(e 1 ) erase(e 2 ) erase(λα. e) = erase(e) erase(e A ) = erase(e) erase(let x:u = e in e ) = let x = erase(e) in erase(e ) Question 4. [5 pts] Give a well-typed closed expression e in the let-polymorphism system such that erase(e) = let f = λx. x in (f true, f 0). Assume two monotypes bool for boolean values and int for integers. Question 5. [10 pts] Explain value restriction. You may write in Korean. Question 6. [5 pts] Give a well-typed closed expression e in the let-polymorphism system with value restriction such that erase(e) = let f = (λx. x) (λy. y) in (f true, f 1). If there is no such expression, explain why. You may write in Korean. 10

6 Type reconstruction [40 pts] Consider the implicit let-polymorphic type system given in the Course Notes. monotype A ::= A A α polytype U ::= A α.u expression e ::= x λx. e e e let x = e in e typing context Γ ::= Γ, x : U type substitution S ::= id {A/α} S S type equations E ::= E, A = A S U and S Γ denote applications of S to U and Γ, respectively. ftv(γ) denotes the set of free type variables in Γ; ftv(u) denotes the set of free type variables in U. We write Γ + x : U for Γ {x : U }, x : U if x : U Γ, and for Γ, x : U if Γ contains no type binding for variable x. Question 1. [6 pts] The type reconstruction algorithm, called W, takes a typing context Γ and an expression e as input, and returns a pair of a type substitution S and a monotype A as output. State the soundness theorem of the algorithm W. Use the typing judgment of the form Γ x : U in your statement. (Soundness of W). If W(Γ, e) = (S, A), then. 11

Question 2. [14 pts] Assume that you are given the unification algorithm Unify(E) and an auxiliary function Gen Γ (A) which generalizes monotype A to a polytype after taking into account free type variables in typing context Γ. The following specifies the algorithm W. Complete the specification: W(Γ, x) = (id, { β/ α} A) x : α.a Γ and fresh β W(Γ, λx. e) = let (S, A) = W(Γ + x : α, e) in fresh α W(Γ, e 1 e 2 ) = let (S 1, A 1 ) = W(Γ, e 1 ) in let let W(Γ, let x = e 1 in e 2 ) = let let 12

Question 3. [6 pts] Given an application e 1 e 2, the algorithm W reconstructs first the type of e 1 and then the type of e 2. Modify the algorithm W so that it reconstructs first the type of e 2 and then the type of e 1. W(Γ, e 1 e 2 ) = let (S 2, A 2 ) = W(Γ, e 2 ) in let let Question 4. [6 pts] Now we add a product type A 1 A 2 and an untyped pair construct (e 1, e 2 ). The typing rule for (e 1, e 2 ) is as follows: Γ e 1 : A 1 Γ e 2 : A 2 Γ (e 1, e 2 ) : A 1 A 2 I Complete the case for (e 1, e 2 ) in the algorithm W: W(Γ, (e 1, e 2 )) = 13

Question 5. [8 pts] What is the result of W(, let f = λx. x in (f 0, f true))? Assume two monotypes bool for boolean values and int for integers. Substitution = Type = For each type variable in the resultant type substitution, indicate where it is produced. 14

Work sheet 15

Work sheet 16