CSE 130, Fall 2005: Final Examination

Similar documents
CSE 130, Fall 2006: Final Examination

CMSC 330, Fall 2013, Practice Problems 3

CSE 130, Winter 2011: Final Examination

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

Types, Type Inference and Unification

CMSC330 Spring 2017 Midterm 2

CSE341 Spring 2017, Midterm Examination April 28, 2017

CSE341, Spring 2013, Final Examination June 13, 2013

CSE341 Autumn 2017, Final Examination December 12, 2017

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CSE 341 : Programming Languages Midterm, Spring 2015

CSE 341 Section 5. Winter 2018

CSE341, Fall 2011, Midterm Examination October 31, 2011

Homework 3 COSE212, Fall 2018

CS3110 Spring 2016 Lecture 6: Modules

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

Programming Languages

CSE341 Spring 2017, Final Examination June 8, 2017

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

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

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

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

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

CSE341, Fall 2011, Midterm Examination October 31, 2011

UNIVERSITETET I OSLO

Introduction to OCaml

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

CS 11 Ocaml track: lecture 2

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

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

Lists. Michael P. Fourman. February 2, 2010

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

Programming Languages

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.

F28PL1 Programming Languages. Lecture 14: Standard ML 4

MIDTERM EXAMINATION - CS130 - Spring 2003

CIS 120 Midterm II March 29, 2013 SOLUTIONS

Programming Languages and Techniques (CIS120)

Modules and Representation Invariants

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

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

CMSC330 Spring 2014 Midterm 2 Solutions

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

CSE3322 Programming Languages and Implementation

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

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

Midterm 2. CMSC 430 Introduction to Compilers Fall 2018

Some Advanced ML Features

CMSC 330: Organization of Programming Languages

Programming Languages

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

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

UNIVERSITETET I OSLO

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

Metaprogramming assignment 3

CS558 Programming Languages

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

COMPUTER SCIENCE TRIPOS

CS558 Programming Languages

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

CSc 372, Fall 2006; Quiz 1 Time: 3:00; Value: 2 points. 1. What is a fundamental difference between the if-then-else constructs in Java (or C) and ML?

Programming in Standard ML: Continued

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

CS558 Programming Languages

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

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

Overloading, Type Classes, and Algebraic Datatypes

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

CS558 Programming Languages. Winter 2013 Lecture 3

CSE3322 Programming Languages and Implementation

CSE-321 Programming Languages 2012 Midterm

Programming Languages and Techniques (CIS120)

CSE 130 [Winter 2014] Programming Languages

Programming Languages Assignment #7

CMSC330 Fall 2014 Midterm 2 Solutions

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

CMSC330 Spring 2008 Final Exam

Programming Languages

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

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

Exercise 1 ( = 24 points)

Functors signature Order = sig functor name type elem (structname:signature) = structure definition; val le : elem*elem -> bool end; elem

Back to OCaml. Summary of polymorphism. Example 1. Type inference. Example 2. Example 2. Subtype. Polymorphic types allow us to reuse code.

Handout 2 August 25, 2008

Recap: Functions as first-class values

CSE341 Spring 2016, Final Examination June 6, 2016

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

List Functions, and Higher-Order Functions

Programming Languages. Tail Recursion. CSE 130: Winter Lecture 8: NOT TR. last thing function does is a recursive call

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

Transcription:

CSE 130, Fall 2005: 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

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 [] -> [] (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 of a * 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 tf f b t 6

(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 Python 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] x = [1,2,3] y = ["a","b","c"] def f(x): x = y f(x) ans = x[0] (b) [5 Points] def f(x): def g(y): return a(x+y) return g a = f(10) ans = a(0) (c) [8 Points] a = [0] def f(x): a = [10] def g(y): a[0] = a[0] + x + y return a[0] return g foo = f(10) foo(1000) ans = (a[0],foo(1)) 8

(d) [7 Points] class A(): def init (self): self.x = [] def a(self): self.x += ["a"] self.d() class B(A): def b(self): self.x += ["b"] class C(A): def a(self): self.x += ["ca"] def c(self): self.x += ["c"] class D(B,C): def d(self): self.x += ["d"] self.b() self.c() o = D() o.a() ans = o.x (e) [5 Points] def foo(n): i = 1 while (i <= n): i += i yield i ans = 0 x = foo(10) for i in x: ans += i (f) [5 Points] something with decorators? 9

(a) [5 Points] Use yield to write a function element_and_rest which 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 element_and_rest should not return a list. When you are done, the following: >>> for t in element_and_rest([1,2,3,4,5]): print t should result in: (1, [2, 3, 4, 5]) (2, [1, 3, 4, 5]) (3, [1, 2, 4, 5]) (4, [1, 2, 3, 5]) (5, [1, 2, 3, 4]) The body of the function should be at most 3 lines long. Write it by filling in the blanks below: def element_and_rest(l): (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, the following: for p in permutations([1,2,3]): print p should result in: [1,2,3] [1,3,2] [2,1,3] [2,3,1] [3,1,2] [3,2,1] The body of the function should be at most 5 lines long. Write it by filling in the blanks below: def permutations(l): 10

11

6. Recall that we say P <: Q if P is a structural subtype of Q. Consider the following Java code. interface A { Object a; } interface B { int a; int b; } interface C { A f(b x); } interface D { /* OUT */ f ( /* IN */ x); } (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 */ 12

7. [5 Points] In less than three lines, explain how decorators are different from aspects. 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 (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 Pr 2 const(2) in x var(x) 2 + 3 plus(const(2),const(3)) in 2 <= 3 leq(const(2),const(3)) bo fun x -> x <= 4 fun(var(x),leq(var(x),const(4))) ar fun x -> fun y -> if x then y else 0 fun(var(x),fun(var(y),ite(var(x),var(y),const(0)))) ar let x = 10 in x letin(var(x),const(10),var(x)) in fun x -> let y = x in y + y fun(var(x),letin(var(y),var(x),plus(var(y),var(y)))) ar (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 = 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))), app(var(id),const(1)))),t). 16

(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