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

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

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

CSE 505, Fall 2008, Final Examination 11 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.

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

CSE505, Fall 2012, Midterm Examination October 30, 2012

CSE341 Autumn 2017, Final Examination December 12, 2017

CSE341, Spring 2013, Final Examination June 13, 2013

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

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

CSE341, Fall 2011, Midterm Examination October 31, 2011

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

Harvard School of Engineering and Applied Sciences Computer Science 152

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341, Fall 2011, Midterm Examination October 31, 2011

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

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

CSE341 Spring 2017, Final Examination June 8, 2017

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

CSE 332, Spring 2010, Midterm Examination 30 April 2010

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.

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

CSE351 Winter 2016, Final Examination March 16, 2016

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

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

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

Programming Languages Lecture 15: Recursive Types & Subtyping

CSE373 Fall 2013, Midterm Examination October 18, 2013

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

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

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

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CSE341 Spring 2017, Midterm Examination April 28, 2017

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

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

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

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

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

CSE331 Spring 2015, Final Examination June 8, 2015

Programming Languages Lecture 14: Sum, Product, Recursive Types

CSE341 Spring 2016, Final Examination June 6, 2016

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

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

CSE332 Summer 2012 Final Exam, August 15, 2012

CSE373 Winter 2014, Midterm Examination January 29, 2014

CSE 341 : Programming Languages Midterm, Spring 2015

CSE332, Spring 2012, Final Examination June 5, 2012

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

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

CSE 303, Spring 2006, Final Examination 6 June Please do not turn the page until everyone is ready.

1 Introduction. 3 Syntax

In addition to the correct answer, you MUST show all your work in order to receive full credit.

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

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

Programming Languages

CSE351 Spring 2018, Midterm Exam April 27, 2018

Subtyping. Lecture 13 CS 565 3/27/06

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

Today. CSE341: Programming Languages. Late Binding in Ruby Multiple Inheritance, Interfaces, Mixins. Ruby instance variables and methods

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

CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping. Dan Grossman Winter 2013

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

CSE-321 Programming Languages 2010 Final

CSE 505: Concepts of Programming Languages

CSE341, Fall 2011, Lecture 26 Summary

Lecture Notes on Data Representation

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE331 Winter 2014, Final Examination March 17, 2014 Please do not turn the page until 8:30. Rules:

CSE 332, Spring 2010, Final Examination 8 June 2010

CSE-321 Programming Languages 2012 Midterm

Type Checking and Type Equality

Lecture 13: Subtyping

Don t Believe the Hype. CS152: Programming Languages. Lecture 21 Object-Oriented Programming. Class-based OOP. So what is OOP?

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

CSE332 Summer 2010: Final Exam

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

Implementing objects as in Javascript, Lua, 164 is expensive because object attributes are implemented as hashtable accesses:

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Some instance messages and methods

CSE 303, Autumn 2006, Final Examination 12 December 2006

CSE 303, Winter 2007, Midterm Examination 9 February Please do not turn the page until everyone is ready.

CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods. Dan Grossman Autumn 2018

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

Variables. Substitution

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Suppose that the following is from a correct C++ program:

Object Oriented Issues in VDM++

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

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

Featherweight Java (FJ)

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

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

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

CSE351 Spring 2018, Final Exam June 6, 2018

Nominal games: a semantics paradigm for effectful languages

ALGORITHMS EXAMINATION Department of Computer Science New York University December 17, 2007

Transcription:

CSE 505, Fall 2007, Final Examination 10 December 2007 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 12:20. You can rip apart the pages, but please write your name on each page. There are 96 points total, distributed unevenly among 5 questions. Most questions have multiple parts and the last question is much shorter and worth fewer points. 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

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. (25 points) This problem uses System F extended with addition. (a) Give the appropriate System F typing rule for addition expressions of the form e 1 + e 2. (This should be easy and unrelated to the other problems.) (b) Consider a typing context where: There are no type variables in scope. x is the only term variable in scope and it has type α.α α. i. What does τ need to be for the program fragment x [τ] (λy : int. y + 7) 11 to typecheck? (Recall application of types or terms associates to the left.) ii. Given your choice for τ, what type does x [τ] (λy : int. y + 7) 11 have? iii. Give a typing derivation for just x [τ] (not the entire program fragment; that s too much work) using the typing context described above. (c) If v is an arbitrary value of type α.α α, then what might v [τ] (λy : int. y + 7) 11 evaluate to? (d) If v is an arbitrary value such that v (λy : int. y + 7) 11 type-checks (notice v is no longer polymorphic), then: i. What type does v have? ii. What might v (λy : int. y + 7) 11 evaluate to? 4

2. (20 points) Consider a typed λ-calculus with sum types, pair types, recursive types, unit, and int. (a) Define a type t1 for a binary tree of integers where: Each interior node has one integer and two children. Each leaf node has no data. (b) Give a type t2 for a binary tree of integers where: Each node has one integer and two optional children (meaning each child may or may not be another binary tree). (c) Explain in English how there is exactly one value of type t1 that cannot be translated to an equivalent value of type t2. (d) Define the value you described in the previous problem using an actual λ-calculus expression. Make sure your value has type t1. 5

3. (25 points) Use Concurrent ML to complete an implementation of infinite arrays (in the sense that no index is out of bounds), without using Caml s references or arrays. More specifically, implement the new_array function for this code: (* Interface *) type a myarray val new_array : a -> a myarray (* Initially, every index maps to a *) val set : a myarray -> int -> a -> unit (* change value in index *) val get : a myarray -> int -> a (* return current value in index *) (* Implementation *) open Event open Thread type a myarray = ((int * ( a channel)) channel) * ((int * a) channel) let new_array init = (* for you *) let set (_,c) i v = sync (send c (i,v)) let get (c,_) i = let ret = new_channel() in sync (send c (i,ret)); sync (receive ret) Hints: Do not worry about being efficient. Have set work in O(1) time and get work in (worst-case) O(n) time where n is the number of set operations preceding it. Use an association list. Sample solution is about 15 lines, including a short helper function for traversing a list. Use choose and wrap. 6

4. (20 points) Consider a class-based OOP language like we did in class, but suppose methods do not have implicit access to self (also known as this). More specifically: As usual, subclasses inherit the fields and methods of superclasses and can override methods. The method-lookup rules are the same. As usual, we confuse classes and types, so a subclass is a subtype. Unlike in OOP, a method in class C has to take an explicit argument of type C to access any fields/methods in its body. For example, instead of having a method like int get_sum() { return self.x + self.y } and calling it like e.get_sum() (assuming the method is defined in a class C with fields x and y), we instead would have to do something like int get_sum(c obj) { return obj.x + obj.y } and call it like e.get_sum(e). So, this is less convenient than OOP (notice how e.get_sum(e) has to repeat e), but more flexible (since callers are not required to use the same e in both places). (a) In this strange language, give an example showing how you want covariant subtyping on method arguments in order for overriding methods to use the fact that they are defined in the subclass. (b) Show how covariant subtyping on method arguments is unsound by giving a use of your example from part (a) in which the program gets stuck even though it typechecks with covariant subtyping. (c) In 1 3 sentences, explain why normal OOP does not have this covariant subtyping is unsound, but contravariant subtyping is not what you want problem. 7

5. (6 points) Consider a prototype-based (classless) approach to OOP as we did in class. Particularly recall how method-lookup is defined in this approach. Explain how to create an object obj such that any object derived from obj (either directly by have a parent slot holding obj or transitively) can have any method called on it without a method not found error occurring. Hints: This is a very short problem. Nontermination. 8