CSE505, Fall 2012, Midterm Examination October 30, 2012

Similar documents
CSE 505, Fall 2008, Midterm Examination 29 October 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 2006, Final Examination 11 December Please do not turn the page until everyone is ready.

CSE341, Fall 2011, Midterm Examination October 31, 2011

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

CSE341, Spring 2013, Final Examination June 13, 2013

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

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE 332, Spring 2010, Midterm Examination 30 April 2010

CSE341 Autumn 2017, Midterm Examination October 30, 2017

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

CSE341 Autumn 2017, Final Examination December 12, 2017

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

CSE 505: Concepts of Programming Languages

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

CSE373 Fall 2013, Midterm Examination October 18, 2013

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.

CSE331 Winter 2014, Midterm Examination February 12, 2014

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 Spring 2017, Final Examination June 8, 2017

CSE 505: Concepts of Programming Languages

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

Programming Languages

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

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

CIS 500 Software Foundations Midterm I

Harvard School of Engineering and Applied Sciences Computer Science 152

1 Introduction. 3 Syntax

CSE-321 Programming Languages 2012 Midterm

Programming Languages Lecture 14: Sum, Product, Recursive Types

CSE341 Spring 2016, Final Examination June 6, 2016

CSE331 Winter 2014, Midterm Examination February 12, 2014

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

CSE341 Spring 2017, Midterm Examination April 28, 2017

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

CMSC 330: Organization of Programming Languages. Operational Semantics

CMSC 330: Organization of Programming Languages

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

λ calculus is inconsistent

CS152: Programming Languages. Lecture 2 Syntax. Dan Grossman Spring 2011

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

Lecture 9: Typed Lambda Calculus

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

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

CSE-321 Programming Languages 2011 Final

CSE-321 Programming Languages 2010 Midterm

Lecture 3: Recursion; Structural Induction

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

Mutable References. Chapter 1

The Substitution Model

Programming Languages Fall 2014

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

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

Variables. Substitution

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

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

MPRI course 2-4 Functional programming languages Exercises

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

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.

CSE331 Spring 2015, Final Examination June 8, 2015

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Typed Lambda Calculus and Exception Handling

Theorem Proving Principles, Techniques, Applications Recursion

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

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

CS421 Spring 2014 Midterm 1

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

Lambda Calculus and Type Inference

CSE 341 : Programming Languages Midterm, Spring 2015

CS 3512, Spring Instructor: Doug Dunham. Textbook: James L. Hein, Discrete Structures, Logic, and Computability, 3rd Ed. Jones and Barlett, 2010

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

CSE373 Winter 2014, Midterm Examination January 29, 2014

Notes for Recitation 8

Formal Systems and their Applications

CMSC 330: Organization of Programming Languages. Lambda Calculus

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

Discrete Mathematics and Probability Theory Fall 2016 Seshia and Walrand Midterm 1

Induction and Semantics in Dafny

CITS3211 FUNCTIONAL PROGRAMMING

Fall Recursion and induction. Stephen Brookes. Lecture 4

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

(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

Lecture 13 CIS 341: COMPILERS

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

Part VI. Imperative Functional Programming

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

Propositional Logic Formal Syntax and Semantics. Computability and Logic

Lecture Notes on Program Equivalence

CSE-321 Programming Languages 2014 Final

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

The Substitution Model. Nate Foster Spring 2018

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011

Lecture 13: Subtyping

Transcription:

CSE505, Fall 2012, Midterm Examination October 30, 2012 Rules: The exam is closed-book, closed-notes, except for one side of one 8.5x11in piece of paper. Please stop promptly at Noon. You can rip apart the pages if you like. There are 100 points total, distributed unevenly among 5 questions (most of 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. (26 points) This problem considers a language that is like the language for IMP expressions (not statements), but where we have pixels instead of integers. A pixel value contains three numbers between 0 and 255 (the first for red, the second for green, the third for blue, but that is not relevant much). Here is the syntax and an English description of the semantics: e ::= p x e + e lighten e darken e p ::= c, c, c H ::= H, x p (c {0, 1,..., 255}) (x {x 1, x 2,..., y 1, y 2,..., z 1, z 2,...,...}) Heaps and variables work as usual, with values being pixels. As indicated in the syntax, all parts of pixel values must always be between 0 and 255 inclusive. Addition adds each component of its pixel arguments separately to produce a new pixel, with sums greater than 255 rounded down to 255. A lighten expression produces a pixel with each component being one more than it was in the argument, again with a max of 255 (i.e., 255 stays 255). A darken expression produces a pixel with each component being one less than it was in the argument, with a min of 0 (i.e., 0 stays 0). (Notice higher values are lighter.) (a) Give a large-step operational semantics for this language, with a judgment of the form H ; e p. Use 5 rules. This is a good hint: there are other approaches that need many more rules. Use from mathematics ( blue math in terms of lecture) min(x, y) and max(x, y) for computing the minimum and maximum of two numbers. Also use addition and subtraction. (b) Using your answer to part (a), prove this: H ; lighten e p if and only if H ; e + 1, 1, 1 p. (c) Define inference rules for a predicate noblack(e) that holds if none of the pixel constants in e are the constant 0, 0, 0 and e contains no darken expressions. (d) Disprove this: If H ; e p and noblack(e), then p 0, 0, 0. 3

(More room for answering problem 1) 4

2. (19 points) In this problem we implement the language from Problem 1 in OCaml and write another related OCaml function. Take these type definitions as given: type pixel = int * int * int type exp = Pixel of pixel Var of string Add of exp * exp Lighten of exp Darken of exp type heap = string -> pixel (a) Write an OCaml function interp of type heap -> exp -> pixel that implements the semantics defined in Problem 1. Note that in addition to + and -, OCaml has functions max and min of type int -> int -> int. (b) We call the first component of a pixel the red component. For example, in 4, 7, 9, the red component is 4. Write an OCaml function no_red_component of type exp -> exp such that: For any heap h that contains only pixels with red components of zero, interp h (no_red_component e) never has any value returned from a call to interp (including recursive calls) contain a red component other than 0. Iff interp h e returns (c1,c2,c3), then interp h (no_red_component e) returns (0,c2,c3) (i.e., the other components are the same). Hint: Your function does not call interp. Rather, it translates its argument into a similar program that, when run, never produces a non-zero red component (assuming the heap has only pixels with red components of 0). 5

3. (20 points) Take the IMP language we studied in class and remove if-statements and while-statements and the corresponding small-step rules. (We remove these only to make this problem shorter; it would still work with them.) Prove this theorem for the resulting language: If s does not contain the variable x and H ; s H ; s, then H, x c ; s H ; s for some H such that H (y) = H (y) for all y x. Assume this lemma is already proven, but clearly indicate where you use it: If e does not contain the variable x and H ; e c, then H, x c ; e c. We expect a formal proof, but you do not need to define formally, does not contain the variable x. Hint: You need induction, but you do not need a stronger induction hypothesis. 6

4. (15 points) (10 for (a) and 5 for (b)) For this problem, you will provide an encoding for ML-style options (the type with constructors None and Some) in the call-by-value lambda-calculus. (a) Define these three lambda terms: The None constructor is an option. The Some constructor takes one argument and returns an option containing the argument. The use-it constructor takes three (curried) arguments. If the first argument is None then it returns the second argument. Else it returns the result of calling the third argument with the value contained in the first argument. (b) Using your encoding and our left-to-right call-by-value small-step semantics, write out the sequence of lambda expressions by which use-it ( Some v 1 ) v 2 λx. x becomes v 1. Note: Part (b) is helpful for checking part (a), but it is worth only 5 points, so you may wish to spend time on other problems instead. 7

5. (20 points) (a) We know the Preservation Theorem is true for the simply-typed lambda calculus. Is it still true if we add the rule e 42 to the operational semantics? If so, briefly explain why. If not, provide a counterexample. (b) We know the Progress Theorem is true for the simply-typed lambda calculus. Is it still true if we add the rule e 42 to the operational semantics? If so, briefly explain why. If not, provide a counterexample. (c) Suppose there is a different type system for lambda calculus with constants for which the Preservation Theorem is false. Will it still be false after adding the rule e 42 to the operational semantics? Briefly explain your answer. (d) Suppose there is a different type system for lambda calculus with constants for which the Progress Theorem is false. Will it still be false after adding the rule e 42 to the operational semantics? Briefly explain your answer. 8