CSE 505, Fall 2009, Midterm Examination 5 November Please do not turn the page until everyone is ready.

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

CSE 505, Fall 2005, Midterm Examination 8 November Please do not turn the page until everyone is ready.

CSE505, Fall 2012, Final Examination December 10, 2012

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

(2) (15pts) Using Prolog, implement a type-checker for the following small subset of System F:

Programming Languages

Review. Principles of Programming Languages. Equality. The Diamond Property. The Church-Rosser Theorem. Corollaries. CSE 230: Winter 2007

CSCI 490 problem set 6

Math 115 Practice for Exam 2

Theories of Programming Languages Assignment 5

An Introduction to Logical Relations Proving Program Properties Using Logical Relations

Lecture Notes on Inductive Definitions

Math 106: Calculus I, Spring 2018: Midterm Exam II Monday, April Give your name, TA and section number:

Element x is R-minimal in X if y X. R(y, x).

NICTA Advanced Course. Theorem Proving Principles, Techniques, Applications

COMP6463: λ-calculus

Lecture Notes on Inductive Definitions

Typed Arithmetic Expressions

Math 116 Second Midterm November 13, 2017

CMSC 336: Type Systems for Programming Languages Lecture 10: Polymorphism Acar & Ahmed 19 February 2008

Midterm 1. Your Exam Room: Name of Person Sitting on Your Left: Name of Person Sitting on Your Right: Name of Person Sitting in Front of You:

High-Level Small-Step Operational Semantics for Transactions (Technical Companion)

Signature. Printed Name. Math 312 Hour Exam 1 Jerry L. Kazdan March 5, :00 1:20

Math 104 Section 2 Midterm 2 November 1, 2013

Supplementary Notes on Inductive Definitions

CMSC 631 Program Analysis and Understanding Fall Type Systems

Classical Program Logics: Hoare Logic, Weakest Liberal Preconditions

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

Extending the Lambda Calculus: An Eager Functional Language

Midterm 2. Your Exam Room: Name of Person Sitting on Your Left: Name of Person Sitting on Your Right: Name of Person Sitting in Front of You:

GOTO the past / programs choose their own adventure. CSE 505: Programming Languages

Static Program Analysis

Math 115 Second Midterm November 12, 2013

Formal Methods Lecture 6. (B. Pierce's slides for the book Types and Programming Languages )

CS 6110 Lecture 28 Subtype Polymorphism 3 April 2013 Lecturer: Andrew Myers

Beyond First-Order Logic

COMPSCI 611 Advanced Algorithms Second Midterm Exam Fall 2017

Reasoning About Imperative Programs. COS 441 Slides 10b

Normalization by Evaluation

CS 6110 Lecture 21 The Fixed-Point Theorem 8 March 2013 Lecturer: Andrew Myers. 1 Complete partial orders (CPOs) 2 Least fixed points of functions

1 Introduction. 2 Recap The Typed λ-calculus λ. 3 Simple Data Structures

Math 1: Calculus with Algebra Midterm 2 Thursday, October 29. Circle your section number: 1 Freund 2 DeFord

MA651 Topology. Lecture 9. Compactness 2.

Math 115 Second Midterm November 12, 2018

Simply Typed Lambda Calculus

COMPSCI 611 Advanced Algorithms Second Midterm Exam Fall 2017

MATH 341, Section 001 FALL 2014 Introduction to the Language and Practice of Mathematics

MTH 310, Section 001 Abstract Algebra I and Number Theory. Sample Midterm 1

Mathematical Foundations of Programming. Nicolai Kraus. Draft of February 15, 2018

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

MATH 251 MATH 251: Multivariate Calculus MATH 251 FALL 2005 EXAM-I FALL 2005 EXAM-I EXAMINATION COVER PAGE Professor Moseley

Math 51 Midterm 1 July 6, 2016

Introduction to lambda calculus Part 2

CS 6110 Lecture 35 Solving Domain Equations 19 April 2013 Lecturer: Andrew Myers

Lecture Notes on The Curry-Howard Isomorphism

CSE 1400 Applied Discrete Mathematics Proofs

Typing λ-terms. Types. Typed λ-terms. Base Types. The Typing Relation. Advanced Formal Methods. Lecture 3: Simply Typed Lambda calculus

Test 3, Linear Algebra

Math 115 Second Midterm November 12, 2018

Math 116 First Midterm October 17, 2014

Week 4. (1) 0 f ij u ij.

Handout: Proof of the completeness theorem

Formal Methods Lecture 6. (B. Pierce's slides for the book Types and Programming Languages )

CS611 Lecture 25 Solving Domain Equations 22 October 2007 Lecturer: Andrew Myers

Math 131 Exam 1 October 4, :00-9:00 p.m.

Please give details of your answer. A direct answer without explanation is not counted.

10. The GNFA method is used to show that

CS422 - Programming Language Design

Lectures Notes on Progress

Midterm Exam Types and Programming Languages Frank Pfenning. October 18, 2018

Typing-by-encoding. A reductionistic approach to building type systems. François Pottier.

Math 13, Spring 2013, Lecture B: Midterm

Proof Techniques (Review of Math 271)

Coinductive big-step operational semantics

Introduction to lambda calculus Part 6

For all For every For each For any There exists at least one There exists There is Some

MATH 1553, SPRING 2018 SAMPLE MIDTERM 1: THROUGH SECTION 1.5

Hoare Examples & Proof Theory. COS 441 Slides 11

MIDTERM EXAM SOLUTIONS

CIS 500 Software Foundations Midterm II Answer key November 17, 2004

Quiz 1 Date: Monday, October 17, 2016

EDA045F: Program Analysis LECTURE 10: TYPES 1. Christoph Reichenbach

Type Systems Winter Semester 2006

Exam 3, Math Fall 2016 October 19, 2016

MAT 3271: Selected solutions to problem set 7

Functions If (x, y 1 ), (x, y 2 ) S, then y 1 = y 2

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

MATH 13 SAMPLE FINAL EXAM SOLUTIONS

COMPUTER SCIENCE TRIPOS

If you have completed your extra credit opportunity, please place it on your inbox.

Induction on Failing Derivations

Denotational Semantics of Programs. : SimpleExp N.

Type Systems. Lecture 2 Oct. 27th, 2004 Sebastian Maneth.

Math 115 Practice for Exam 3

1 Invariant subspaces

CIS 500 Software Foundations. Midterm II. March 28, 2012

Safety Analysis versus Type Inference

Lesson 24: True and False Number Sentences

Consequence Relations and Natural Deduction

0.1 Random useful facts. 0.2 Language Definition

Transcription:

CSE 505, Fall 2009, Midterm Examination 5 November 2009 Please do not turn the page until everyone is ready Rules: The exam is closed-book, closed-note, except for one side of one 85x11in piece of paper Please stop promptly at 11:50 You can rip apart the pages, but please write your name on each page There are 100 points total, distributed unevenly among 5 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 In this problem, we change IMP by adding one more constant, : c {, 2, 1, 0, 1, 2, } { } Because is a constant, it can also be an expression, the result of evaluating an expression, or the contents of a heap variable However, is not a legal argument to any math operators like (blue) plus except = and Informally, if an expression has any subexpression that evaluates to, then the expression evaluates to (a) (7 points) Add four inference rules to the H ; e c judgment to account for (b) (17 points) Considering all the inference rules now in the language, prove that if e contains a and H ; e c, then c is Hint: Use induction The new rules from part (a) are not the difficult cases (c) (6 points) Our IMP statement semantics can now get stuck In English, explain exactly how this could occur Propose a small change to the statement semantics to avoid this Give any new inference rule(s) and explain in English how you changed the meaning of the language (a) sq1 H ; e 1 H ; e 1 + e 2 sq2 H ; e 2 H ; e 1 + e 2 sq3 H ; e 1 H ; e 1 e 2 sq4 H ; e 2 H ; e 1 e 2 (b) Proof by induction on the derivation of H ; e c proceeding by the rules instantiated at the bottom of the derivation: const Then e = c, so if e contains, then e and c are var Holds vacuously because e is some x and therefore does not contain add Because the result is c 1 + c 2, neither c 1 nor c 2 is Therefore, since H ; e 1 c 1 and H ; e 2 c 2, by induction neither e 1 nor e 2 contains So e 1 + e 2 does not contain and the theorem holds vacuously mult Because the result is c 1 c 2, neither c 1 nor c 2 is Therefore, since H ; e 1 c 1 and H ; e 2 c 2, by induction neither e 1 nor e 2 contains So e 1 e 2 does not contain and the theorem holds vacuously sq1 Holds trivially because c is sq2 Holds trivially because c is sq3 Holds trivially because c is sq4 Holds trivially because c is Note: The proof can also be done by structural induction on e, but then you need to argue for each form of e what rules could apply (c) A statement can get stuck if the expression in an if-statement evaluates to because it is neither the case that > 0 nor 0 There are of course many ways to fix this We could add the rule below, which has the effect of treating as false, just like a non-positive number if3 H ; e H ; if e s 1 s 2 H ; s 2 3

(This page intentionally blank) 4

2 In this problem, we use the following large-step semantics for IMP statements: The judgment is H ; s H meaning s under heap H produces heap H The inference rules are: skip H; skip H assign H ; e c H; x := e H, x c seq H; s 1 H 1 H 1 ; s 2 H 2 H; (s 1 ; s 2 ) H 2 if1 H ; e c H; s 1 H 1 c > 0 H; if e s 1 s 2 H 1 if2 H ; e c H; s 2 H 2 c 0 H; if e s 1 s 2 H 2 while H; if e (s; while e s) skip H H; while e s H The sequence operator is associative That is, s 1 ; (s 2 ; s 3 ) and (s 1 ; s 2 ); s 3 are equivalent (a) (5 points) State this associativity fact formally as a theorem in terms of the large-step semantics for statements (b) (16 points) Prove the theorem you stated in part (a) Hint: Do not use induction (a) (For all H, H, s 1, s 2, and s 3 ), H; (s 1 ; (s 2 ; s 3 )) H if and only if H; ((s 1 ; s 2 ); s 3 ) H (b) We prove the two directions of the if and only if separately First assume H; (s 1 ; (s 2 ; s 3 )) H Inverting the derivation ensures we have a derivation that looks like this for some H 1 and H 2 : H ; s 1 H 1 H 1 ; s 2 H 2 H 2 ; s 3 H H 1 ; (s 2 ; s 3 ) H H; (s 1 ; (s 2 ; s 3 )) H So we know H; s 1 H 1, H 1 ; s 2 H 2, and H 2 ; s 3 H, from which we can derive: H; s 1 H 1 H 1 ; s 2 H 2 H; (s 1 ; s 2 ) H 2 H 2 ; s 3 H H; ((s 1 ; s 2 ); s 3 ) H Now assume H; ((s 1 ; s 2 ); s 3 ) H Inverting the derivation ensures we have a derivation that looks like this for some H 1 and H 2 : H; s 1 H 1 H 1 ; s 2 H 2 H; (s 1 ; s 2 ) H 2 H 2 ; s 3 H H; ((s 1 ; s 2 ); s 3 ) H So we know H; s 1 H 1, H 1 ; s 2 H 2, and H 2 ; s 3 H, from which we can derive: H ; s 1 H 1 H 1 ; s 2 H 2 H 2 ; s 3 H H 1 ; (s 2 ; s 3 ) H H; (s 1 ; (s 2 ; s 3 )) H 5

(This page intentionally blank) 6

3 (14 points) Describe what, if anything, each of the following Caml programs would print: (a) let f x y = x y in let z = f print_string "hi " in f print_string "hi" (b) let f x = (fun y -> print_string x) in let g = f "elves " in let x = "trees " in g "cookies " (c) let rec f n x = if n>=0 then (let _ = print_string x in f (n-1) x) else () in f 3 "hi " (d) let rec f n x = if n>=0 then (let _ = print_string x in f (n-1) x) else () in f 3 (e) let rec f x = f x in print_string (f "hi ") (a) hi hi (b) elves (c) hi hi hi hi (d) prints nothing (evaluates to a function that prints when called) (e) prints nothing (goes into an infinite loop) 7

4 In this problem, we use the untyped lambda calculus with small-step call-by-value left-to-right evaluation Recall this encoding of pairs: mkpair λx λy λz z x y fst λp p(λx λy x) snd λp p(λx λy y) (a) (9 points) For any values v 1 and v 2, fst ( mkpair v 1 v 2 ) produces a value in 6 steps Writing only lambda terms (ie, no abbreviations), show these steps Show just the result of each step, not the derivation that produces it (λp p (λx λy x)) ((λx λy λz z x y) v 1 v 2 ) (b) (6 points) Again using no abbreviations, extend the encoding to include a swap function Given an encoding of the pair (v 1, v 2 ), swap should return an encoding of the pair (v 2, v 1 ) (a) (λp p (λx λy x)) ((λx λy λz z x y) v 1 v 2 ) (λp p (λx λy x)) ((λy λz z v 1 y) v 2 ) (λp p (λx λy x)) (λz z v 1 v 2 ) (λz z v 1 v 2 ) (λx λy x) (λx λy x) v 1 v 2 (λy v 1 ) v 2 v 1 (b) There are an infinite number of correct solutions Here are four: λp (λx λy λz z x y)(p λx λy y)(p λx λy x) λp λz z (p λx λy y)(p λx λy x) λp (λx λy λz z y x)(p λx λy x)(p λx λy y) λp p λx λy λz z y x 8

5 In this problem, we consider the simply-typed lambda-calculus (using small-step call-by-value left-toright evaluation) We suppose the integer constants c (of type int) include only positive integers (1, 2, 3, ), ie, we remove negative numbers We add a subtraction operator (e ::= e e) and these rules: (a) (4 points) (b) (6 points) c 3 is math s subtraction of c 2 from c 1 Γ e 1 : int Γ e 2 : int c 1 c 2 c 3 Γ e 1 e 2 : int Our operational semantics needs two additional rules Give them Our language is not type-safe Demonstrate this (c) (10 points) Consider the Preservation Lemma, the Progress Lemma, and the Substitution Lemma Which of these lemmas are true in our language? Explain your answers briefly, but proofs are not required (a) e 1 e 1 e 1 e 2 e 1 e 2 e 2 e 2 v e 2 v e 2 (b) Consider an expression like 3-4 It type-checks under the emtpy context ( ) with type int, but it cannot take a step because the result of the mathematical subtraction is -1, which is not in our language, so no rule applies (c) The Progress Lemma does not hold; see part (b) The Preservation Lemma does hold: all three new operational rules produce an expression of type int assuming the expression before the step type-checks with type int The Substitution Lemma does hold (assuming (e 1 e 2 )[e/x] = (e 1 [e/x]) (e 2 [e/x])); the case of the proof for subtractions expressions follows from induction just like the case for application 9