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

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

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

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

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

CSE505, Fall 2012, Midterm Examination October 30, 2012

CSE 505, Fall 2008, Midterm Examination 29 October Please do not turn the page until everyone is ready.

Harvard School of Engineering and Applied Sciences Computer Science 152

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

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

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

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

CSE341, Spring 2013, Final Examination June 13, 2013

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

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Programming Languages

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

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

A Concepts-Focused Introduction to Functional Programming Using Standard ML Sample Exam #1 Draft of August 12, 2010

CSE341, Fall 2011, Midterm Examination October 31, 2011

Programming Languages Lecture 15: Recursive Types & Subtyping

Programming Languages Lecture 14: Sum, Product, Recursive Types

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

CSE341 Autumn 2017, Final Examination December 12, 2017

CSE341 Spring 2017, Final Examination June 8, 2017

CSE 303, Spring 2005, Midterm Examination 29 April Please do not turn the page until everyone is ready.

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341 Spring 2017, Final Examination June 8, 2017

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

CSE341 Spring 2016, Final Examination June 6, 2016

CSE-321 Programming Languages 2010 Final

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CSE-321 Programming Languages 2011 Final

Variables. Substitution

CSE331 Spring 2015, Final Examination June 8, 2015

CSE 303, Winter 2006, Final Examination 16 March Please do not turn the page until everyone is ready.

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

Part VI. Imperative Functional Programming

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

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

CS152: Programming Languages. Lecture 24 Bounded Polymorphism; Classless OOP. Dan Grossman Spring 2011

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 Autumn 2017, Midterm Examination October 30, 2017

Object Oriented Issues in VDM++

CSE-321 Programming Languages 2012 Midterm

CSEP505 Programming Languages, Autumn 2016, Final Exam December, Programming Languages for a World of Change

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

1 Introduction. 3 Syntax

Subtyping. Lecture 13 CS 565 3/27/06

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

Subtyping and Objects

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

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

ERC 4th Workshop KAIST. Sukyoung Ryu KAIST. August 26, 2010

CSE373 Fall 2013, Midterm Examination October 18, 2013

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Simply-Typed Lambda Calculus

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

CSE331 Fall 2014, Final Examination December 9, 2014 Please do not turn the page until 2:30. Rules:

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

λ calculus is inconsistent

Polymorphism. Lecture 19 CS 565 4/17/08

CSE-321 Programming Languages 2014 Final

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

Recursive Types and Subtyping

Part III. Chapter 15: Subtyping

CSE351 Winter 2016, Final Examination March 16, 2016

Programming Languages Assignment #7

Featherweight Java (FJ)

Unifying Nominal and Structural Ad-Hoc Polymorphism

CSE332 Summer 2012 Final Exam, August 15, 2012

Mutable References. Chapter 1

From IMP to Java. Andreas Lochbihler. parts based on work by Gerwin Klein and Tobias Nipkow ETH Zurich

Programming Language Concepts: Lecture 19

cs173: Programming Languages

CSE341 Spring 2017, Midterm Examination April 28, 2017

User-Defined Algebraic Data Types

CSE-505: Programming Languages. Lecture 18 Existential Types. Zach Tatlock 2016

Some instance messages and methods

Dependent Types. Announcements. Project Presentations. Recap. Dependent Types. Dependent Type Notation

Lecture Notes on Aggregate Data Structures

Runtime Behavior of Conversion Interpretation of Subtyping

Constrained Types and their Expressiveness

GADTs meet Subtyping

MIT Semantic Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

CIS 341 Final Examination 4 May 2017

Part III Chapter 15: Subtyping

The Typed λ Calculus and Type Inferencing in ML

Programming Languages and Compilers Qualifying Examination. Answer 4 of 6 questions.1

CSE 505: Concepts of Programming Languages

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

Type assignment for intersections and unions in call-by-value languages

Recursive Types and Subtyping

Subsumption. Principle of safe substitution

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE 131 Introduction to Computer Science Fall 2016 Exam I. Print clearly the following information:

Theoretical Corner: The Non-Interference Property

Transcription:

CSE 505, Fall 2008, Final Examination 11 December 2008 Please do not turn the page until everyone is ready. Rules: The exam is closed-book, closed-note, except for one side of one 8.5x11in piece of paper. Please stop promptly at 10:20. You can rip apart the pages. There are 100 points total, distributed among 6 questions. The questions have multiple parts. Advice: Read questions carefully. Understand a question before you start writing. Write down thoughts and intermediate steps so you can get partial credit. The questions are not necessarily in order of difficulty. Skip around. In particular, make sure you get to all the problems. If you have questions, ask. Relax. You are here to learn. 1

For your reference (page 1 of 2): and Γ e : τ and τ 1 τ 2 e ::= λx. e x e e c {l 1 = e 1,..., l n = e n } e.l i fix e v ::= λx. e c {l 1 = v 1,..., l n = v n } τ ::= int τ τ {l 1 : τ 1,..., l n : τ n } (λx. e) v e[v/x] e 1 e 1 e 1 e 2 e 1 e 2 e 2 e 2 v e 2 v e 2 fix e fix e fix λx. e e[fix λx. e/x] {l 1 = v 1,..., l n = v n }.l i v i e i e i {l 1 = v 1,..., l i 1 = v i 1, l i = e i,..., l n = e n } {l 1 = v 1,..., l i 1 = v i 1, l i = e i,..., l n = e n } Γ c : int Γ x : Γ(x) Γ, x : τ 1 e : τ 2 Γ λx. e : τ 1 τ 2 Γ e 1 : τ 2 τ 1 Γ e 2 : τ 2 Γ e 1 e 2 : τ 1 Γ e : τ τ Γ fix e : τ Γ e 1 : τ 1... Γ e n : τ n labels distinct Γ {l 1 = e 1,..., l n = e n } : {l 1 : τ 1,..., l n : τ n } Γ e : {l 1 : τ 1,..., l n : τ n } Γ e.l i : τ i 1 i n Γ e : τ τ τ Γ e : τ {l 1 :τ 1,..., l n :τ n, l:τ} {l 1 :τ 1,..., l n :τ n } {l 1 :τ 1,..., l i 1 :τ i 1, l i :τ i,..., l n :τ n } {l 1 :τ 1,..., l i :τ i, l i 1 :τ i 1,..., l n :τ n } τ i τ i {l 1 :τ 1,..., l i :τ i,..., l n :τ n } {l 1 :τ 1,..., l i :τ i,..., l n :τ n } τ 3 τ 1 τ 2 τ 4 τ 1 τ 2 τ 3 τ 4 τ τ τ 1 τ 2 τ 2 τ 3 τ 1 τ 3 e ::= c x λx:τ. e e e Λα. e e[τ] τ ::= int τ τ α α.τ v ::= c λx:τ. e Λα. e Γ ::= Γ, x:τ ::=, α and ; Γ e : τ e e 2 e e 2 v e v e e[τ] e [τ] (λx:τ. e) v e[v/x] (Λα. e)[τ] e[τ/α] ; Γ x : Γ(x) ; Γ c : int ; Γ, x:τ 1 e : τ 2 τ 1 ; Γ λx:τ 1. e : τ 1 τ 2, α; Γ e : τ 1 ; Γ Λα. e : α.τ 1 ; Γ e 1 : τ 2 τ 1 ; Γ e 2 : τ 2 ; Γ e : α.τ 1 τ 2 ; Γ e 1 e 2 : τ 1 ; Γ e[τ 2 ] : τ 1 [τ 2 /α] 2

and ; Γ e : τ e ::=... A(e) B(e) (match e with Ax. e Bx. e) roll τ e unroll e τ ::=... τ 1 + τ 2 µα.τ v ::=... A(v) B(v) roll τ v match A(v) with Ax. e 1 By. e 2 e 1 [v/x] match B(v) with Ax. e 1 By. e 2 e 2 [v/y] A(e) A(e ) B(e) B(e ) match e with Ax. e 1 By. e 2 match e with Ax. e 1 By. e 2 unroll (roll µα.τ v) v roll µα.τ e roll µατ e unroll e unroll e ; Γ e : τ 1 + τ 2 ; Γ, x:τ 1 e 1 : τ ; Γ, y:τ 2 e 2 : τ ; Γ match e with Ax. e 1 By. e 2 : τ ; Γ e : τ 1 ; Γ e : τ 2 ; Γ e : τ[(µα.τ)/α] ; Γ A(e) : τ 1 + τ 2 ; Γ B(e) : τ 1 + τ 2 ; Γ roll µα.τ e : µα.τ ; Γ e : µα.τ ; Γ unroll e : τ[(µα.τ)/α] Module Thread: type t val create : ( a -> b) -> a -> t val join : t -> unit Module Mutex: type t val create : unit -> t val lock : t -> unit val unlock : t -> unit Module Event: type a channel type a event val new_channel : unit -> a channel val send : a channel -> a -> unit event val receive : a channel -> a event val choose : a event list -> a event val wrap : a event -> ( a -> b) -> b event val sync : a event -> a 3

1. (20 points) Assume we have a typed programming language formally defined by a small-step operational semantics and a typing judgment. Assume the appropriate Preservation and Progress Theorems hold for this language. Consider each question below separately and explain your answers briefly. (a) Suppose we change the operational semantics by adding a new inference rule. i. Is it possible that the Preservation Theorem is now false? ii. Is it possible that the Progress Theorem is now false? (b) Suppose we change the type system by adding a new inference rule. i. Is it possible that the Preservation Theorem is now false? ii. Is it possible that the Progress Theorem is now false? (c) Suppose we change the operational semantics by replacing one of the inference rules with a rule that is just like it except it has some additional hypothesis. i. Is it possible that the Preservation Theorem is now false? ii. Is it possible that the Progress Theorem is now false? (d) Suppose we change the type system by replacing one of the inference rules with a rule that is just like it except it has some additional hypothesis. i. Is it possible that the Preservation Theorem is now false? ii. Is it possible that the Progress Theorem is now false? 4

2. (15 points) Consider a typed λ-calculus with recursive types, sum types, pair types, int, and unit. Assume the language uses explicit roll and unroll coercions (not subtyping) for recursive types. (a) Define a type t1 for lists where each list element contains either one int or a pair of ints. Use a sum type to tag list elements to distinguish these two possibilities. (b) Write a typed λ-calculus program of the form fix (λsum:t1 int. λlst:t1. ) for adding up all the integers in a list of type t1. For example, a list holding the pair (2,3) and the integer 7 would have a sum of 12. Assume, of course, the expression language has addition. (c) Define a type t2 for a list where list elements alternate between holding an int and a pair of ints, starting with an int. That is, the first, third, fifth, etc. elements are ints and the second, fourth, sixth elements, etc. are pairs of ints. However, the list may have any number of total elements (including 0 or an odd number). Do not use a sum type for list elements (although you still need sum types for the list itself). (d) Write a typed λ-calculus program of the form fix (λsum:t2 int. λlst:t2. ) for adding up all the integers in a list of type t2. For example, a list holding the pair (2,3) and the integer 7 would have a sum of 12. Assume, of course, the expression language has addition. 5

3. (20 points) In this problem you will use CML to implement futures. Futures have this interface: type a promise val future : (unit -> a) -> a promise val force : a promise -> a Recall a future runs in parallel and force blocks until the result is ready. It is permissable to call force with the same promise multiple times; if the result has already been computed it is simply returned immediately. (a) Provide an implementation of the interface above in CML. Do not use mutation. Remember to include the definition of type a promise. (b) Consider an extended interface with: val force_any : a promise list -> a Extend your implementation to provide this function. It should block only until one of the promises in the list is ready. If more than one is ready, it can return the result of any of the ready ones. 6

4. (15 points) Consider a class-based OOP language like we did in class. Suppose it has abstract methods. Recall that a class that defines or inherits abstract methods cannot have instances unless all abstract methods are overridden. In this problem, we consider a bad idea for a new language feature: If class C inherits abstract method m, then C s definition can include remove m to mean instances of C do not have method m. This allows us to create instances of C unless there are other abstract methods that are neither overridden nor removed. (a) Explain why this new feature is unsound if subclasses are subtypes. Give an example with a superclass, a subclass, and client code that makes an instance of the subclass. Your example should not require any concrete methods in the superclass. (b) Explain why this new feature is unsound even if subclasses that remove methods are not subtypes of their superclasses. Give an example with a superclass, a subclass, and client code that makes an instance of the subclass. 7

5. (15 points) This problem considers the code below in a class-based OOP language. Assume classes A, B, C, and D have no subclasses except those shown. class A {} class B extends A {} class C extends B {} class D { int m(a x, A y) { return 1; } int m(a x, B y) { return 2; } int m(a x, C y) { return 3; } int m(b x, A y) { return 4; } int m(b x, B y) { return 5; } int m(b x, C y) { return 6; } int m(c x, A y) { return 7; } int m(c x, B y) { return 8; } int m(c x, C y) { return 9; } int foo(a a, C b) { return self.m(a,b); } } (a) Suppose our language has multimethods. What are the possible values that a call (new D()).foo(e2,e3) could return? (b) Suppose our language has static overloading. What are the possible values that a call (new D()).foo(e2,e3) could return? (c) How, if at all, do your answers change if subclasses of D might exist but subclasses of C do not? (d) How, if at all, do your answers change if subclasses of C might exist but subclasses of D do not? 8

6. (15 points) This problem considers a typed language with support for bounded parametric polymorphism, i.e., types of the form α < τ 1. τ 2. (a) The System F typing rule for type application: ; Γ e : α.τ 1 τ 2 ; Γ e[τ 2 ] : τ 1 [τ 2 /α] is no longer appropriate because (1) e does not have a bounded polymorphic type and (2) τ 2 is not checked against a bound. Replace this typing rule with a more appropriate one. (b) Suppose our language has addition, records with mutable fields, and e 1 ; e 2 for sequencing. Then this term type-checks: λx:{l 1 :int, l 2 :int}. ((x.l 1 := x.l 1 + x.l 2 ); x) Use bounded polymorphism to give a value v that has the same run-time behavior as this term but has a much more general type. Use the syntax Λα < τ.e to create a bounded polymorphic expression. (c) Using the v you defined in part (b), give a τ and v 2 such that v [τ] v 2 type-checks (using the rule you defined in part (a) and the rule for function application). Choose a v 2 that takes advantage of the subtyping allowed by bounded polymorphism. (d) What is the type of the expression (v [τ] v 2 ) in your answer to part (c)? You do not need to write down a typing derivation. 9