CSE 130, Fall 2006: Final Examination

Similar documents
CSE 130, Fall 2005: Final Examination

CMSC 330, Fall 2013, Practice Problems 3

Homework 3 COSE212, Fall 2018

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

CSE341 Spring 2017, Midterm Examination April 28, 2017

CSE341 Autumn 2017, Final Examination December 12, 2017

CSE 130, Winter 2011: Final Examination

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341, Spring 2013, Final Examination June 13, 2013

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Lists. Michael P. Fourman. February 2, 2010

CSE 341 : Programming Languages Midterm, Spring 2015

UNIVERSITETET I OSLO

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341 Spring 2017, Final Examination June 8, 2017

CSE 341 Section 5. Winter 2018

CIS 120 Midterm I October 2, Name (printed): Username (login id):

Types, Type Inference and Unification

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CMSC330 Spring 2017 Midterm 2

CS 11 Ocaml track: lecture 2

Programming Languages and Techniques (CIS120)

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CompSci 220. Programming Methodology 12: Functional Data Structures

Programming in Standard ML: Continued

Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

Programming Languages

Life in a Post-Functional World

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

CSE341, Fall 2011, Midterm Examination October 31, 2011

CS3110 Spring 2016 Lecture 6: Modules

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

CSE 130 [Winter 2014] Programming Languages

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

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

Programming Languages

Name: Pennkey: CIS 120 Midterm II November 18, 2011

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Parametricity. Leo White. February Jane Street 1/ 70

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CS558 Programming Languages

Modules and Representation Invariants

CSE341 Spring 2016, Final Examination June 6, 2016

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Introduction to OCaml

CS558 Programming Languages

Some Advanced ML Features

CSE3322 Programming Languages and Implementation

Two Problems. Programming Languages and Compilers (CS 421) Type Inference - Example. Type Inference - Outline. Type Inference - Example

Bibliography. Analyse et Conception Formelle. Lesson 5. Crash Course on Scala. Scala in a nutshell. Outline

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

CS558 Programming Languages

Programming Languages Assignment #7

Mini-ML. CS 502 Lecture 2 8/28/08

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

Principles of Programming Languages

Programming Languages

CS558 Programming Languages

Recap: Functions as first-class values

Programming Languages

CSCI-GA Final Exam

Handout 2 August 25, 2008

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

CS558 Programming Languages

Metaprogramming assignment 3

Midterm 2. CMSC 430 Introduction to Compilers Fall 2018

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Exercise 6 Multiple Inheritance, Multiple Dispatch and Linearization November 3, 2017

Exercise 1: Graph coloring (10 points)

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

UNIVERSITETET I OSLO

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

CS558 Programming Languages

Overloading, Type Classes, and Algebraic Datatypes

From last time. News. Example. Example. Typechecking let. Typechecking let. Pick up your first two quizzes after class. Assignment #2 due tomorrow

List are immutable Lists have recursive structure Lists are homogeneous

CMSC330 Spring 2014 Midterm #2. Score 1 OCaml types & type

CMSC 330, Fall 2018 Midterm 1

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

CMSC 330: Organization of Programming Languages

CSE3322 Programming Languages and Implementation

CIS 120 Midterm II March 29, 2013 SOLUTIONS

Programming Languages and Techniques (CIS120)

Lists. Prof. Clarkson Fall Today s music: "Blank Space" by Taylor Swift

CSE-321 Programming Languages 2012 Midterm

Generic polymorphism on steroids

CIS 120 Midterm I February 16, 2015 SOLUTIONS

CMSC 330, Fall 2018 Midterm 1

UNIVERSITETET I OSLO

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Transcription:

CSE 130, Fall 2006: Final Examination Name: ID: Instructions, etc. 1. Write your answers in the space provided. 2. Wherever it says explain, write no more than three lines as explanation. The rest will be ignored. 3. The points for each problem are a rough indicator (when converted to minutes), of how long you should take for the problem. 4. Good luck! 1 (15) 2 (15) 3 (10) 4 (20) 5 (20) 6 (30) 7 (15) 8 (10) Total (135) 1

1. [15 Points] For each of the following Ocaml programs, if the code is well-typed, write down the value of ans, otherwise, if the code has a type problem, write type error. (a) let ans = let x = 10 in let f y = let a = x + 1 in let b = y + a in a + b in f 100 (b) let ans = let f n = 10 in let f n = if n > 0 then n + (f (n-1)) else 0 in f 5 (c) let ans = let f g x = g (g x) in let h0 = fun x -> x * x in let h1 = f h0 in let h2 = f h1 in h2 2 2. [15 Points] For each of the following Ocaml programs, write down the type of ans. (a) let ans = let f f = f + 1 in f (b) let ans f g x = if x > 0 then f x else g x (c) let ans l = match l with 2

[] -> [] (hx,hy)::t -> (hx hy)::(ans t) 3

3. Consider the Ocaml module described below: module Set : SETSIG = struct exception Duplicates type a set = a list let new x = [x] let rec mem s x = match s with [] -> false h::t -> if x <> h then mem t x else if mem t x then raise Duplicates else true let add s x = if mem s x then s else (x::s) let union s1 s2 = match s1 with [] -> s2 h::t -> union t (add s2 h) let choose s = match s with [] -> None h::t -> Some (h,t) end and the two possible signatures: (A) (B) module type SETSIG = module type SETSIG = sig sig type a set = a list type a set val new : a -> a set val new : a -> a set val mem : a set -> a -> bool val mem : a set -> a -> bool val add : a set -> a -> a set val choose : a set -> ( a * a set) option val choose : a set -> ( a * a set) opti val union : a set -> a set -> a set val union : a set -> a set -> a set end end (a) [5 Points] For which one of the signatures (A) or (B), can a client can cause the exception Duplicates to get raised? Write down a client expression that would cause this exception to get raised. For the other signature explain why the exception will never get raised. Signature: Client Expression: Explanation: 4

(b) [5 Points] Recall the filter function described in class: let rec filter f l = match l with [] -> [] h::t -> if f h then h::(filter f t) else filter f t Consider the client function: let intersection s1 s2 = filter (mem s2) s1 For one of the signatures (A) or (B), the the client function intersection compiles, i.e. is well typed. Which one? What is the inferred type of intersection using this signature? Signature: Inferred Type: intersection : -> -> (c) [10 Points] Write an equivalent version of intersection that would compile with both signatures. 5

4. Consider the following Ocaml datatype used to represent trees. type a tree = Leaf of a Node a tree * a tree (a) [5 Points] Write the value of type int tree that corresponds to the following pictorial representation of a tree.. / \ / \ / \.. / \ / \ / \ / \ 1 2 3 4 (b) [5 Points] Consider the following function: let rec tf f b t = match t with Leaf x -> f (b,x) Node (t1,t2) -> tf f (tf f b t1) t2 What is the typeof the function tf? Answer this by filling in the blanks: -> -> -> (c) [5 Points] Fill in the blanks below to obtain an implementation of: to_list : a tree -> a list that returns the list of values occurring as leaves of the tree. let to_list t = let f = in let b = in tf f b t (d) [5 Points] Fill in the blanks below to obtain an implementation of: size : a tree -> int that returns the list of values occurring as leaves of the tree. let size t = let f = in let b = in 6

tf f b t (e) [5 Points] Write a tail-recursive version of tf. Hint: This is difficult. You may need a helper function. 7

5. For each of the following Scala programs, write down the value of ans, or write error together with an explanation, if an error occurs. Write your answers on the blank space on the right. (a) [5 Points] val x = Array(1,2,3) val y = Array("a","b","c") def f(y: Any) { val x = y val _ = f(x) val ans = x(0) (b) [5 Points] def f(x: Int) : Int => Int = { def g(y: Int) = { a(x+y) g val a: Int => Int = f(10) val ans = a(0) (c) [8 Points] val a = Array(0) def f(x: Int) = { val a: Array[Int] = Array(10) val g = (y:int) => { a(0) += (x + y) a(0) g val foo = f(10) val _ = foo(1000) val ans = (a(0), foo(1)) 8

(d) [12 Points] trait A { var x : List[String] = List() def d(): Unit def a(){ x = x ++ List("a") d() trait B extends A { def b() { x = x ++ List("b") trait C extends A { def c() { x = x ++ List("c") class D extends B with C { def d() { x = x ++ List("d") b() c() val o = new D val _ = o.a val ans = o.x (e) [5 Points] def foo(n: Int) = { var xs : List[Int] = List() var i = 1 while (i <= n) { xs = i :: xs i += i xs var ans = 0 for (i <- foo(10)) { ans += i 9

6. (a) [5 Points] Use yield to write a function def elementandrest[a](xs: List[A]): Iterator[(A, List[A])] that takes a list as input and returns an iterator over tuples which consist of an element of the list, and the list with that element removed. The elements of the list should be in the same order as in the original list. The function elementandrest should not return a list. When you are done, the following: scala> for (t <- elementandrest(list(1,2,3,4,5))) println(t) (1,List(2, 3, 4, 5)) (2,List(1, 3, 4, 5)) (3,List(1, 2, 4, 5)) (4,List(1, 2, 3, 5)) (5,List(1, 2, 3, 4)) Write the function by filling in the 3 blanks below with suitable expressions. def elementandrest[a](xs: List[A]): Iterator[(A, List[A])] = { val iter = for (i <- iter ; x = ; y = ) yield (x, y) Hint: List(0, 10, 20, 30, 40, 50, 60, 70).slice(2, 4) == List(20, 30) (b) [10 Points] Write a function permutations which takes a list as input and returns an iterator over permutations of the given list. The function should not compute all permutations before returning. When you are done, you should get the following behavior: scala> for (p <- permutations(list(1,2,3))) println(p) List(1,2,3) List(1,3,2) List(2,1,3) List(2,3,1) List(3,1,2) List(3,2,1) The body of the function should be at most 5 lines long. Write it by filling in the blanks below: 10

def permutations[a](xs: List[A]): Iterator[List[A]] = xs match { case Nil => case x::rest => 11

7. Recall that P <: Q if P is a structural subtype of Q. Assume that everything is a subtype of Any. trait A { val a: Any trait B { val a: Int val b: Int trait C { def f(x: B): A trait D { def f (x: /* IN */ ) : /* OUT */ (a) [2 Points] True or False: A <: B? (b) [2 Points] True or False: B <: A? (c) [6 Points] Write four possible ways of filling in the blanks in the definition of D (i.e. of completing the type of f) such that D <: C. i. /* IN */, /* OUT */ ii. /* IN */, /* OUT */ iii. /* IN */, /* OUT */ iv. /* IN */, /* OUT */ 8. [5 Points] Consider the following C-like code. int y = 1; void f(int x){ int y; y = x + 1; x = x + 10; g(x); printf("x = %d \n",x); void g(int x){ y = x + 1; void main(){ f(y); printf("y = %d \n",y) What is the output of executing this code under 12

(a) static scoping? (b) dynamic scoping? 13

9. Consider the following Prolog code: actor(xmen,jackman). actor(xmen,berry). actor(scoop,jackman). actor(scoop,johanssen). actor(lost_in_translation,murray). actor(lost_in_translation,johanssen). actor(ghostbusters,murray). actor(ghostbusters,akroyd). actor(batmanreturns,bale). actor(batmanreturns,caine). actor(dirtyrottenscoundrels,martin). actor(dirtyrottenscoundrels,caine). actor(shopgirl,danes). actor(shopgirl,martin). (a) [2 Points] Write a predicate costar(x,y) that is true when X,Y have acted in the same movie. (b) [3 Points] Write a predicate busy(x) that is true when X has acted in more than one movie. (c) [5 Points] Write a predicate bacon(x,y) that is true when there is a sequence of actors Z 1, Z 2,..., Z n such that for each i, the pair Z i, Z i+1 have acted in the same movie, and X is Z 1 and Y is Z n. 14

10. For this problem, you will write Prolog code to implement the magic algorithm whereby ML is able to infer the types of all expressions. First, we shall encode (nano) ML expressions as Prolog terms via the following grammar. expr ::= const(i) var(x) plus(expr, expr) leq(expr, expr) ite(expr, expr) letin(x, expr, expr) fun(var(x), expr) app(expr, expr) Similarly, we shall encode ML types as Prolog terms using the following grammar: type ::= int bool arrow(type, type) The table below shows several examples of Ocaml expressions, the Prolog term encoding that expression, and the Prolog term encoding the type of the expression. ML Expression Prolog Expression Term Prolog Type Term 2 const(2) int x var(x) 2 + 3 plus(const(2),const(3)) int 2 <= 3 leq(const(2),const(3)) bool fun x -> x <= 4 fun(var(x),leq(var(x),const(4))) arrow(int,bool) fun x y -> fun(var(x),fun(var(y), if x then y else 0 ite(var(x),var(y),const(0)))) arrow(bool,arrow(int,int)) let x = 10 in x letin(var(x),const(10),var(x)) int fun x -> fun(var(x), let y = x in letin(var(y),var(x) y + y plus(var(y),var(y)))) arrow(int,int) (a) [5 Points] Write a Prolog predicate envtype(env,x,t), such that envtype([[x1,t1],[x2,t2],...,[xn,vn]] is true if X equals the first term xi corresponding to variable xi and T equals the corresponding ti corresponding to the type of the variable xi in the type environment ti. When you are done, you should get the following behavior:?- envtype([[x,int],[y,bool]],x,t). T = int?- envtype([[x,int],[x,bool]],x,t). T = int?- envtype([[x,int],[x,bool]],x,bool). No 15

(b) [20 Points] Write a Prolog predicate typeof(env,e,t) that is true when the term T is the correct ML type of the ML expression corresponding the term E in the type environment corresponding to the list Env. Write your solution by filling in the grid below: typeof(env,const(i),t) :- typeof(env,var(x),t) :- typeof(env,plus(e1,e2),t) :- typeof(env,leq(e1,e2),t) :- typeof(env,ite(e1,e2,e3),t) :- typeof(env,letin(var(x),e1,e2),t) :- typeof(env,fun(var(x),e),t) :- typeof(env,app(e1,e2),t) :- When you are done, you should get the following output:?- typeof([[x,int],[y,bool]],var(x),t). T = int?- typeof([],plus(const(2),const(3)),t). T = int?- typeof([],leq(const(2),const(3)),t). T = bool?- typeof([],fun(var(x),leq(var(x),const(4))),t). T = arrow(int,bool)?- typeof([],fun(var(x),fun(var(y),ite(var(x),var(y),const(0)))),t). T = arrow(bool,arrow(int,int))?- typeof([],letin(var(x),const(10),var(x)),t). T = int?- typeof([],fun(var(x),letin(var(y),var(x),plus(var(y),var(y)))),t). T = arrow(int, int)?- typeof([],app(fun(var(x),plus(var(x),const(1))),const(19)),t). T = int (c) [[5] Points] Does your predicate infer polymorphic types? In other words, using your implementation of typeof will the result of the following query be or No? Explain.?- typeof([],letin(var(id),fun(var(x),var(x)), letin(var(y),app(var(id),leq(const(2),const(3))), 16

app(var(id),const(1)))),t). (d) [[Extra Credit] Points] Extend your solution so that the the above query succeeds. type inference is polymorphic. That is, it should successfully find an appropriate solution for T for the query above. 17