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

Similar documents
CSE505, Fall 2012, Midterm Examination October 30, 2012

CSE 505, Fall 2007, Final Examination 10 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 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, Final Examination 11 December Please do not turn the page until everyone is ready.

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341, Spring 2013, Final Examination June 13, 2013

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CSE341, Fall 2011, Midterm Examination October 31, 2011

1 Introduction. 3 Syntax

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

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CSE-321 Programming Languages 2012 Midterm

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

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

CSE341 Spring 2017, Final Examination June 8, 2017

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 2016, Midterm Examination April 29, 2016

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE 505: Concepts of Programming Languages

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

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Midterm Examination April 28, 2017

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

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

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

Programming Languages

CSE341 Autumn 2017, Final Examination December 12, 2017

CSE 505: Concepts of Programming Languages

CSE331 Spring 2015, Final Examination June 8, 2015

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

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

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

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

Indicate the option which most accurately completes the sentence.

CSE341 Spring 2016, Final Examination June 6, 2016

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

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

CSE373 Fall 2013, Midterm Examination October 18, 2013

CSE-321 Programming Languages 2014 Final

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CSE-321 Programming Languages 2010 Midterm

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

CSE-321 Programming Languages 2011 Final

CIS 500 Software Foundations Midterm I

Lecture 9: Typed Lambda Calculus

Harvard School of Engineering and Applied Sciences Computer Science 152

Variables. Substitution

Programming Languages Fall 2014

The Substitution Model

CS4215 Programming Language Implementation. Martin Henz

1. Abstract syntax: deep structure and binding. 2. Static semantics: typing rules. 3. Dynamic semantics: execution rules.

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

Semantics with Applications 3. More on Operational Semantics

Type Safety. Java and ML are type safe, or strongly typed, languages. C and C++ are often described as weakly typed languages.

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

Gradual Typing for Functional Languages. Jeremy Siek and Walid Taha (presented by Lindsey Kuper)

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

CS152: Programming Languages. Lecture 5 Pseudo-Denotational Semantics. Dan Grossman Spring 2011

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

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Meeting14:Denotations

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

Typed Lambda Calculus and Exception Handling

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016

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.

CIS 500 Software Foundations Fall October 2

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

Lecture Notes on Data Representation

MPRI course 2-4 Functional programming languages Exercises

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

University of Illinois at Chicago Department of Computer Science. Final Examination. CS 151 Mathematical Foundations of Computer Science Fall 2012

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Meeting13:Denotations

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

Lecture 13: Subtyping

Abstract Interpretation

CSE 332, Spring 2010, Final Examination 8 June 2010

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

The Substitution Model. Nate Foster Spring 2018

Softwaretechnik. Lecture 03: Types and Type Soundness. Peter Thiemann. University of Freiburg, Germany SS 2008

CITS3211 FUNCTIONAL PROGRAMMING

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

Part VI. Imperative Functional Programming

Lecture Notes on Program Equivalence

Lecture 3: Recursion; Structural Induction

CSE351 Spring 2018, Midterm Exam April 27, 2018

CSE-321 Programming Languages 2010 Final

CSE373 Winter 2014, Midterm Examination January 29, 2014

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

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

Concepts of programming languages

Midterm II CS164, Spring 2006

CSE351 Winter 2016, Final Examination March 16, 2016

Lambda Calculus and Extensions as Foundation of Functional Programming

Transcription:

CSE 505, Fall 2008, Midterm Examination 29 October 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 1:20. You can rip apart the pages, but please write your name on each page. There are 100 points total, distributed unevenly among 4 questions (which 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: s ::= skip x := e s; s if e s s while e s e ::= c x e + e e e (c {..., 2, 1, 0, 1, 2,...}) (x {x 1, x 2,..., y 1, y 2,..., z 1, z 2,...,...}) H ; e c const H ; c c var H ; x H(x) add H ; e 1 c 1 H ; e 2 c 2 H ; e 1 + e 2 c 1 + c 2 mult H ; e 1 c 1 H ; e 2 c 2 H ; e 1 e 2 c 1 c 2 H 1 ; s 1 H 2 ; s 2 assign H ; e c H ; x := e H, x c ; skip seq1 H ; skip; s H ; s seq2 H ; s 1 H ; s 1 H ; s 1 ; s 2 H ; s 1; s 2 if1 H ; e c c > 0 H ; if e s 1 s 2 H ; s 1 if2 H ; e c c 0 H ; if e s 1 s 2 H ; s 2 while H ; while e s H ; if e (s; while e s) skip e ::= λx. e x e e c v ::= λx. e c τ ::= int τ τ e e (λ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 e[e /x] = e x[e/x] = e y x y[e/x] = y c[e/x] = c e 1 [e/x] = e 1 y x y F V (e) (λy. e 1 )[e/x] = λy. e 1 e 1 [e/x] = e 1 e 2 [e/x] = e 2 (e 1 e 2 )[e/x] = e 1 e 2 Γ e : τ Γ c : int Γ x : Γ(x) Γ, x : τ 1 e : τ 2 Γ λx. e : τ 1 τ 2 Γ e 1 : τ 2 τ 1 Γ e 2 : τ 2 Γ e 1 e 2 : τ 1 Preservation: If e : τ and e e, then e : τ. Progress: If e : τ, then e is a value or there exists an e such that e e. Substitution: If Γ, x:τ e : τ and Γ e : τ, then Γ e[e /x] : τ. 2

1. This problem (and the next one) involve a tiny language for, moving around the integer number line. Syntax: e ::= go c reverse e; e d ::= R L (c {..., 2, 1, 0, 1, 2,...}) Informal semantics: A position is a direction d and an intger c. The direction R is to the right, i.e., toward greater numbers, and L is to the left, i.e., toward lesser numbers. An expression changes the position as follows: go c has no effect on the position, if c is negative. go c moves the position distance c in the current direction and does not change the direction, if c is non-negative. For example, if the position is R; 3, then go 2 produces position R; 5. reverse changes the direction only. e 1 ; e 2 does e 1 and then e 2. (a) (12 points) Give a large-step operational semantics for our language. The judgment should have the form d; c; e d ; c where d and c comprise the starting position and d and c comprise the ending position. Do not use any other definitions, functions, or judgments. Hint: Sample solution uses 6 rules and uses math s +,, <, and. (b) (14 points) Prove this theorem: If e has no reverse expressions in it and R; c; e d ; c, then c c. You need a stronger induction hypothesis; be sure to state it clearly. (c) (4 points) Describe exactly where your proof in part (b) relies on the stronger induction hypothesis. Solution: (a) neg c < 0 d; c; go c d; c gor c 0 R; c; go c R; c + c gol c 0 L; c; go c L; c c revr R; c; reverse L; c revl L; c; reverse R; c seq d 0 ; c 0 ; e 1 d 1 ; c 1 d 1 ; c 1 ; e 2 d 2 ; c 2 d 0 ; c 0 ; (e 1 ; e 2 ) d 2 ; c 2 (b) Stronger induction hypothesis: If e has no reverse expressions in it and R; c; e d ; c, then c c and d = R. Proof is by induction on the derivation of R; c; e d ; c, proceeding by cases on the bottommost rule used in the derivation: neg Then d = R anc c = c because this rule does not change the position. gor Then d = R (clearly in the rule) and c c because c is c plus a nonnegative number. gol This rule cannot be used because the initial direction is R. revr This rule cannote be used because e has no reverse expressions, so e cannot be reverse. revl This rule cannot be used because e has no reverse expressions, so e cannot be reverse. It also cannot be used because the initial direction is R. seq By inversion of the rule, there exists e 1, e 2, d 1, and c 1 such that e is e 1 ; e 2, R; c; e 1 d 1 ; c 1, and d 1 ; c 1 ; e 2 d ; c. Applying induction to R; c; e 1 d 1 ; c 1 ensures d 1 is R anc c 1 c. Since d 1 is R, we can apply induction to d 1 ; c 1 ; e 2 d ; c to ensure d is R and c c 1. Since is transitive c c. With that and d is R, we are done. (c) In the seq case, without the stronger induction hypothesis we do not know d 1 is R, but we need this to apply induction to d 1 ; c 1 ; e 2 d ; c. 3

(This page intentionally blank) 4

2. In this problem, you will write a Caml interpreter for the language in the previous problem. (a) (3 points) Give two Caml type definitions to define expressions e and directions d. (For constants, use the Caml int type.) (b) (12 points) Write a function interp that takes two curried arguments: (1) a pair that is the starting position and (2) an expression. Return the pair that is the ending position. (c) (2 points) Give the Caml type of interp. (d) (2 points) Implement interp_from_origin, which takes just an expression and returns the result of running it from an initial position that is at the origin of the number line and facing right. Use partial application. (e) (1 points) Solution: Give the Caml type of interp_from_origin. (a) type exp = Go of int Reverse Seq of exp * exp type dir = R L (b) (* Note a solution with nested match expressions is also fine *) let rec interp (d,c) e = match d,e with R, Go c -> if c < 0 then (d,c) else (R,c+c ) L, Go c -> if c < 0 then (d,c) else (L,c-c ) R, Reverse -> (L,c) L, Reverse -> (R,c) _, Seq(e1,e2) -> interp (interp (d,c) e1) e2 (c) (dir*int) -> exp -> (dir*int) (d) let interp_from_origin = interp (R,0) (e) exp -> (dir*int) 5

3. This problem extends IMP statements with this strange new syntax and small-step evaluation rules: s ::=... s # s H ; skip # s H ; s H ; s 1 H ; s 1 H ; s 1 # s 2 H ; s 2 # s 1 (a) (6 points) Explain in 1 3 informal but precise English sentences the meaning of s 1 # s 2. (b) (3 points) Is IMP still deterministic? Explain briefly. (c) (3 points) Give an H, s 1, and s 2 such that H; s 1 terminates, H; s 2 terminates, H; s 1 ; s 2 terminates, but H; s 1 # s 2 does not terminate. (d) (3 points) Give an H, s 1, and s 2 such that H; s 1 terminates, H; s 2 terminates, H; s 1 # s 2 terminates, but H; s 1 ; s 2 does not terminate. Solution: (a) s 1 # s 2 executes s 1 and s 2 by alternating which substatement takes the next step, starting with s 1. In other words, it interleaves their execution with time slices of one execution step. After one statement reaches skip, the other statement finishes executing. (b) Yes, in fact it is still the case that for all H and s, either s is skip or there is exactly one derivation of an execution step. (c) One answer: Let H be, let s 1 be x := 1; while y skip, and let s 2 be y := 1. (d) One answer: Let H be, let s 1 be y := 0; y := 0; y := 1, and let s 2 be while y skip. (Interestingly, changing s 1 to y := 0; y := 1 is still correct, but changing s 1 to skip; y := 1 is incorrect, even though H(y) = 0. 6

4. In this problem we add options (like Caml s None and Some) to the simply-typed λ-calculus, using a get primitive instead of pattern-matching. Syntax: Operational Semantics: e ::=... None Some e get e v ::=... None Some v τ ::=... τ option some e e Some e Some e get-e e e get e get e get-some get (Some v) v get-none get None get None (a) (4 points) One of the evaluation rules is strange and probably not what you would implement in an actual language. Which rule? What does the rule mean? (b) (12 points) Give 3 appropriate new typing rules, one for each new form of expression. Your rules should be sound without being unnecessarily restrictive. (c) (4 points) State the new case of the Canonical Forms Lemma for values of option types. (You do not need to prove this easy lemma.) (d) (15 points) Extend the proof of the Progress Lemma to account for our additions. Include only the new cases. (Note you are only proving Progress, not Preservation or Substitution though those should also hold.) Hints: Solution: You need to use the new case of Canonical Forms. Be clear about where you do so. The strange evaluation rule will also be important. Be clear about where this is. (a) Rule get-none is strange. It causes any program that tries to evaluate get None not to terminate. A real language would do something like throw an exception. (b) Γ None : τ option Γ e : τ Γ Some e : τ option Γ e : τ option Γ get e : τ (c) If v : τ option, then v is None or there exists a v such that v is Some v. (d) Recall the proof is by induction on the structure (syntax height) of e. New cases: If e is None, then e is a value. If e is Some e for some e, then inverting the derivation of e : τ ensures e : τ for some τ. So by induction either e can take a step or it is a value. If e can take a step, then e can take a step with some. If e is a value, then e is a value. If e is get e for some e, then inverting the derivation of e : τ ensures e : τ option. So by induction either e can take a step or it is a value. If e can take a step, then e can take a step with get-e. If e is a value, then Canoncial Forms ensures it is None or Some v for some v. If it is None, we can take a step with get-none (which is why we have our strange rule; in practice we would probably be stuck). If it is Some v, we can take a step with get-some. 7

(This page intentionally blank) 8