cs173: Programming Languages Final Exam

Similar documents
cs173: Programming Languages Midterm Exam

cs173: Programming Languages

cs173: Programming Languages

Higher-Order Functions

Implementing Recursion

Higher-Order Functions (Part I)

Typed Recursion {with {mk-rec : (((num -> num) -> (num -> num)) -> (num -> num)) {fun {body : ((num -> num) -> (num -> num))} {{fun {fx :

Implementing Continuations

CPSC 311, 2010W1 Midterm Exam #2

Threads. {seqn {spawn EXPR 1 } {spawn EXPR 2 }} Runs EXPR 1 and EXPR 2 in any order, even interleaved with each other

Type Declarations. [... <id> τ... ] <id> : τ. Γ <num> : number. Γ true : boolean. Γ false : boolean. Γ e 1 : number.

An Introduction to Functions

Cost of Substitution

CPSC 311, 2011W1 Midterm Exam #2 2011/11/09

Non-Functional Procedures

Type Declarations. Γ <num> : num [... <id> τ... ] <id> : τ Γ true : bool Γ false : bool Γ e 1 : num Γ e 2 : num Γ. {+ e 1 e 2 } : num

Type Soundness. Type soundness is a theorem of the form If e : τ, then running e never produces an error

Functions and Recursion. Dr. Philip Cannata 1

State. Substitution relies on an identifer having a fxed value

Running FAE Programs Natively

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CSE341 Spring 2017, Final Examination June 8, 2017

Parsing Scheme (+ (* 2 3) 1) * 1

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

CS558 Programming Languages

CSE341 Spring 2017, Final Examination June 8, 2017

Structure and Interpretation of Computer Programs

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

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

CS 320 Midterm Exam Solution

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

CSE341 Spring 2016, Final Examination June 6, 2016

Principles of Programming Languages

CSE341 Autumn 2017, Final Examination December 12, 2017

"Good" vs. "Bad" Expressions. ; interp-expr FAE... -> FAE-Value

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

CS143 Final Fall 2009

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Understanding Recursion

CS 415 Midterm Exam Spring 2002

Comp 311: Sample Midterm Examination

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Typing Data. Chapter Recursive Types Declaring Recursive Types

YOUR NAME PLEASE: *** SOLUTIONS ***

MIDTERM EXAMINATION - CS130 - Spring 2003

CS3360 Design and Implementation of Programming Languages Final Exam May 16, pm to 12:45pm (2 hour and 40 minutes) NAME:

Typing Control. Chapter Conditionals

CPSC W1 University of British Columbia

7. Introduction to Denotational Semantics. Oscar Nierstrasz

CS 320 Homework One Due 2/5 11:59pm

Types and evaluation

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

Lexical Addresses. let x = 1 y = 2 in let f = proc (x) +(x, y) in (f x) let _ = 1 _ = 2 in let _ = proc (_) +(<0,0>, <1,1>) in (<0,0> <1,0>)

Recap: Functions as first-class values

CS 275 Name Final Exam Solutions December 16, 2016

UNIVERSITY REGULATIONS

CSE 247 Data Structures and Algorithms Fall Exam I

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

CMSC 331 Final Exam Section 0201 December 18, 2000

Name SOLUTIONS EID NOTICE: CHEATING ON THE MIDTERM WILL RESULT IN AN F FOR THE COURSE.

CS 320 Midterm Exam. Fall 2018

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CSE341, Spring 2013, Final Examination June 13, 2013

Midterm II CS164, Spring 2006

COP4020 Spring 2011 Midterm Exam

CMSC 330: Organization of Programming Languages. Operational Semantics

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

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

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

CS 242. Fundamentals. Reading: See last slide

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

CS153: Compilers Lecture 14: Type Checking

University of Virginia Department of Computer Science. CS 4501: Information Retrieval Fall 2015

MIDTERM EXAMINATION - CS130 - Spring 2005

Principles of Programming Languages

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CS 314 Principles of Programming Languages

COSE212: Programming Languages. Lecture 0 Course Overview

Quiz. Question #1: What is the value of the following expression? {+ 1 2} Wrong answer: 0 Wrong answer: 42. Answer: 3 1-4

Type Systems, Type Inference, and Polymorphism

Sample Final Exam Questions

CS 11 Haskell track: lecture 1

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

CS115 - Module 9 - filter, map, and friends

CS150: Exam 2 Due: Mon Apr 20th by 3:30pm (in class) or by 6:30pm (OLS 219) UVA ID: Directions

CMSC 330: Organization of Programming Languages

CSE505, Fall 2012, Midterm Examination October 30, 2012

cs1120: Exam 2 Due: Thursday Nov 29 at 3:30pm (in class)

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Control in Sequential Languages

CS143 Final Spring 2016

ECE 2035 Programming HW/SW Systems Spring problems, 5 pages Exam Three 8 April Your Name (please print clearly)

Programming Languages Third Edition

CS 842 Ben Cassell University of Waterloo

University of Waterloo Midterm Examination Model Solution CS350 Operating Systems

Environments

Functional Programming. Pure Functional Programming

Transcription:

cs173: Programming Languages Final Exam Fall 2002 Please read this page of instructions before you turn the page! This exam is worth 102 points. We will assign partial credit to partial responses, provided we can understand the response and it reflects a correct understanding of course material. Exam rules: Your responses to this exam are due at noon on Tuesday, December 17. Please read the newsgroup for instructions on turning in the exam. On preparing solutions: Solutions may be hand-written, but the burden of legibility rests on you. Unless impossible, please answer the questions on these exam sheets, and within the space provided. If you do use separate sheets of paper, be sure to make clear where your work on a problem begins and ends. Staple your solutions together. Put your name on every page. Do not assume that the length of an answer should be proportional to the number of points assigned. Even for weighty problems, the correct solution can take just a few words. No problem on this exam asks for, or even really wants, a lengthy essay as a solution. If you believe a question is underspecified, make a reasonable assumption and document it. The exam is open book with respect to the course notes for the year 2002. The exam may refer to specific lectures, so it would be useful to have them handy before you begin to respond. You may not refer to any other sources related to the course between the time you start and finish the exam. You have three hours and 15 minutes to take this exam. Time begins when you start reading the exam questions. The first three hours must be a single, contiguous block. At some point, either immediately or later (but before the responses are due), you may use an additional 15 contiguous minutes to change or augment your answers. All Scheme code responses must use the dialects employed in this course. You may not evaluate any programs related to this exam on a computer. You must neither give assistance to any other course participant, nor receive assistance from anyone other than the course staff, on material under examination. Brown University has an Academic Code that governs this exam and all our other transactions. I expect you, as students and scholars, to abide by it faithfully and fully. Act honorably. Problem 1 2 3 4 5 6 7 8 9 Total Grade Thank you for taking cs173. May chance favor you. 1

Problem 1 [5 points] On 2002-11-04, we saw that in the simply-typed lambda calculus, all programs are guaranteed to halt. This makes the type checker for that language a program that can determine whether or not programs halt (in this case, it always responds in the affirmative). Does this contradict the Halting Problem? (Please state clearly whether or not it does; if you claim it doesn t, explain why not, and if you claim it does, reconcile. Be brief and very clear!) 2

Problem 2 [15 points] Consider the program ( 1 (first (cons true empty))) This program has a type error. Generate constraints for this program. Isolate the smallest set of these constraints that, solved together, identify the type error. Feel free to label the sub-expressions above with superscripts for use when writing and solving constraints. 3

Problem 3 [15 points] Languages like ML and Haskell support both let-based polymorphic type inference and type annotations on procedure and module boundaries. To model this, consider a new language construct with the following BNF description: {annotfun {<id> : <type>} : <type> <expr>} That is, instead of expecting inference to infer the type of the procedure, the programmer uses annotfun to explicitly declare its type. Describe the constraint generation rule(s) for annotfun expressions. (This should not be different from the rule for unannotated procedures.) Do these additional rules affect the unification-based constraint solver? Why or why not? 4

Problem 4 [10 points] Consider the type judgments discussed in the lecture notes for 2002-11-01. These rules are for an eager language. Consider the lazy version of the language instead. Indicate which of the judgments would change, and how, by writing the modified judgments below. Do not write judgments that do not change between the two versions of the language. Pay special attention to the typing rules for function definition function application For each one, if you believe the existing rule does not change, explain why not. (If you believe neither rule changes, you can answer both parts together.) 5

Problem 5 [15 points] We have discussed why, at least on paper, mark-and-sweep is an inferior garbage collection strategy for languages that permit implementations to move data in memory (such as ones that do not expose pointer operations). Yet even for such languages, most memory managers use a mark-and-sweep strategy for their large-object space. Give one reason why mark-and-sweep might be preferable on such objects. State two standard objections to mark-and-sweep, and explain why they don t apply in this context. For each reason, first state the objection, then explain why it doesn t apply. 6

Problem 6 [10 points] Suppose we have multiple kinds of data of different sizes. In the heap representations we studied in class, the type tag is always at the first address (i.e., at offset 0) that represents a value. Should it matter where the tag resides? For instance, why not put the tag at the last address rather than the first? (The more concrete you are, the more brief you can be.) 7

Problem 7 [15 points] In this course, we studied the use of hygienic macro systems in Scheme. Consider the macro for or, presented on 2002-11-22 (where it was called or%). Create a fragment of code that produces different results under hygienic and unhygienic expanders. Present the original program, the expanded code under each expander, and the result of evaluating each expanded program. 8

Problem 8 [15 points] Starting with the language FAE, which has only functions and addition, we add the new construct iota. iota reflects the amount of time that has elapsed since the beginning of program execution. Programmers can therefore employ iota to implement simple forms of profiling, pre-emptive multi-tasking, etc. Concretely, the initial value of iota is 0. Its value increases by one on every function application. References to iota evaluate to the current value of the counter. Thus, the program {+ iota {{fun {dummy} iota} 1729}} evaluates to 1 (the reference to iota on the left-hand-side evaluates to 0, while the one inside the application evaluates to 1). Extend the following interpreter with the iota construct. Clearly mark all additions and changes. Remember to update the abstract syntax. You may not use mutation anywhere in your interpreter! (define-datatype FAE FAE? [num (n number?)] [add (lhs FAE?) (rhs FAE?)] [id (name symbol?)] [fun (param symbol?) (body FAE?)] [app (fun-expr FAE?) (arg-expr FAE?)]) (define-datatype Env Env? [mtsub] [asub (name symbol?) (value FA-value?) (env Env?)]) (define-datatype FA-value FA-value? [numv (n number?)] [closurev (param symbol?) (body FAE?) (cache Env?)]) ;; parse : sexp FAE (define (parse sexp) (cond [(symbol? sexp) (id sexp)] [(number? sexp) (num sexp)] [(list? sexp) (case (first sexp) [( ) (add (parse (second sexp)) (parse (third sexp)))] [(with) (parse fun,(first (second sexp)),(third sexp),(second (second sexp)) )] [(fun) (fun (first (second sexp)) (parse (third sexp)))] [else (app (parse (first sexp)) (parse (second sexp)))])])) 9

;; numv-n : FAE [numv] number (define (numv-n value) (cases FA-value value [numv (n) n] [else (error numv-n "not a number")])) ;; numv+ : numv numv numv (define (numv n1 n2) (numv ( (numv-n n1) (numv-n n2)))) ;; lookup : symbol Env FA-value (define (lookup name env) (cases Env env [mtsub () (error lookup "no binding for identifier")] [asub (bound-name bound-value rest-env) (if (symbol? bound-name name) bound-value (lookup name rest-env))])) ;; interp : FAE Env FA-value (define (interp expr env) (cases FAE expr [num (n) (numv n)] [add (l r) (numv (interp l env) (interp r env))] [id (v) (lookup v env)] [fun (param body) (closurev param body env)] [app (fun-expr arg-expr) (local ([define fun-val (interp fun-expr env)] [define arg-val (interp arg-expr env)]) (cases FA-value fun-val [closurev (cl-param cl-body cl-cache) (interp cl-body (asub cl-param arg-val cl-cache))] [else (error interp "can only apply functions")]))])) 10

Problem 9 [2 points] Name any two kinds of camel. 11