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

Similar documents
CIS 500 Software Foundations Midterm I

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.

CIS 500 Software Foundations Fall September 25

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

Type Systems Winter Semester 2006

Chapter 5: The Untyped Lambda Calculus

CIS 500 Software Foundations Fall October 2

Costly software bugs that could have been averted with type checking

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

Concepts of programming languages

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

Programming Languages Fall 2014

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

CIS 500 Software Foundations Midterm II. Review questions

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

CSE-321 Programming Languages 2010 Midterm

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

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

CSE-321 Programming Languages 2012 Midterm

The Untyped Lambda Calculus

More Lambda Calculus and Intro to Type Systems

Graphical Untyped Lambda Calculus Interactive Interpreter

Programming Languages Fall 2013

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

Functional Languages and Higher-Order Functions

More Lambda Calculus and Intro to Type Systems

CMSC 336: Type Systems for Programming Languages Lecture 4: Programming in the Lambda Calculus Acar & Ahmed 22 January 2008.

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Lecture #3: Lambda Calculus. Last modified: Sat Mar 25 04:05: CS198: Extra Lecture #3 1

CSC173 Lambda Calculus Lambda Calculus Evaluation (10 min) Evaluate this expression (β-reduce with normal-order evaluation):

Lecture 9: Typed Lambda Calculus

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

(Advanced) topics in Programming Languages

Exercise 1 (2+2+2 points)

Lesson 4 Typed Arithmetic Typed Lambda Calculus

CSE 505: Concepts of Programming Languages

Types and Programming Languages. Lecture 5. Extensions of simple types

Lecture 14: Recursive Types

The University of Nottingham

CIS 500 Software Foundations Midterm II Answer key November 16, 2005

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

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Denotational Semantics. Domain Theory

Formal Systems and their Applications

Types and Programming Languages. Lecture 8. Recursive type

CMSC 330: Organization of Programming Languages

CSE505, Fall 2012, Midterm Examination October 30, 2012

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

Computer Science 203 Programming Languages Fall Lecture 10. Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus

Lecture Notes on Data Representation

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

CMSC 330: Organization of Programming Languages

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

Exercise 1 ( = 18 points)

Recursion. Lecture 6: More Lambda Calculus Programming. Fixed Points. Recursion

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

CIS 500 Software Foundations Fall December 4

Exercise 1 ( = 24 points)

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

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

CSE-321 Programming Languages 2011 Final

Untyped Lambda Calculus

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

1 Introduction. 3 Syntax

Lambda Calculi With Polymorphism

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

CSE-321 Programming Languages 2010 Final

CS 314 Principles of Programming Languages. Lecture 21

Lambda Calculus as a Programming Language

Untyped Lambda Calculus

Introduction to the Lambda Calculus

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Exercise 1 ( = 22 points)

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

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

上海交通大学试卷 ( A 卷 ) ( 2015 至 2016 学年第 2 学期 )

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Programming Languages Lecture 14: Sum, Product, Recursive Types

Chapter 5: The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

Programming Languages

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

Fundamental Concepts. Chapter 1

Lambda Calculus as a Programming Language

The Untyped Lambda Calculus

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

CIS 500 Software Foundations. Final Exam. May 3, Answer key

LambdaLab: An Interactive λ-calculus Reducer for Learning

Lambda calculus. Chapter 2

Introduction to Lambda Calculus. Lecture 7 CS /08/09

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

The Lambda Calculus. notes by Don Blaheta. October 12, A little bondage is always a good thing. sk

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

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

Transcription:

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

Inductive Definitions Review: Recall that the function size, which calculates the total number of nodes in the abstract syntax tree of a term in the language of arithmetic and boolean expressions, can be written either in standard recursive function notation size(true) = 1 size(false) = 1 size(0) = 1 size(succ t 1 ) = size(t 1 ) + 1 size(pred t 1 ) = size(t 1 ) + 1 size(iszero t 1 ) = size(t 1 ) + 1 size(if t 1 then t 2 else t 3 ) = size(t 1 ) + size(t 2 ) + size(t 3 ) + 1 or, equivalently, as the least relation closed under the following inference rules: (true, 1) size (false, 1) size (0, 1) size (t 1, n) size ((succ t 1 ), (n + 1)) size (t 1, n) size ((pred t 1 ), (n + 1))) size (t 1, n) size ((iszero t 1 ), (n + 1)) size (t 1, n 1 ) size (t 2, n 2 ) size (t 3, n 3 ) size ((if t 1 then t 2 else t 3 ), (n 1 + n 2 + n 3 + 1)) size 1

1. (7 points) Suppose that we define another relation, weird, as the least relation closed under the following rules: (true, 1) weird (W1) (0, 1) weird (W2) (0, 8) weird (W3) (t 1, n) weird ((succ t 1 ), (n + 1)) weird (t 1, n) weird ((succ (succ t 1 )), (n + 2)) weird ((succ (succ t 1 )), n) weird ((pred t 1 ), n) weird (iszero (iszero t 1 )), n) weird ((iszero t 1 ), (n + 1)) weird (t 1, n 1 ) weird (t 2, n 2 ) weird (t 3, n 3 ) weird ((if t 1 then t 2 else t 3 ), (n 1 + n 2 + n 3 + 1)) weird (W4) (W5) (W6) (W7) (W8) Which of the following pairs are related by weird? Write Yes (if related) or No (if not) next to each pair. (a) (true, 1) Answer: Yes (b) ((if true then 0 else 0), 18) Answer: Yes (c) ((if true then 0 else 0), 11) Answer: Yes (d) ((pred 0), 3) Answer: Yes (e) ((succ (succ true)), 3) Answer: Yes (f) ((iszero 0), 3) Answer: No (g) ((pred false), 3) Answer: No Grading scheme: One point for each item. 2

2. (5 points) Here are the same rules again: (true, 1) weird (W1) (0, 1) weird (W2) (0, 8) weird (W3) (t 1, n) weird ((succ t 1 ), (n + 1)) weird (t 1, n) weird ((succ (succ t 1 )), (n + 2)) weird ((succ (succ t 1 )), n) weird ((pred t 1 ), n) weird (iszero (iszero t 1 )), n) weird ((iszero t 1 ), (n + 1)) weird (t 1, n 1 ) weird (t 2, n 2 ) weird (t 3, n 3 ) weird ((if t 1 then t 2 else t 3 ), (n 1 + n 2 + n 3 + 1)) weird (W4) (W5) (W6) (W7) (W8) Which of these rules can be dropped without changing the relation that they define? (I.e., what is the smallest subset of the above rules such that the least relation closed under this subset is the same as the least relation closed under all the rules?) Write the name(s) of the unnecessary rule(s) here: Answer: W5 and W7 gradingschemetwo points off for each incorrect rule; two points off for each missing rule. 3

Typed Arithmetic Expressions The full definition of the language of typed arithmetic and boolean expressions is reproduced, for your reference, at the end of the exam. Some properties enjoyed by this language are listed at the bottom of this page. 3. (5 points) Suppose we add a new rule if true then t 2 else t 3 t 3 (E-FUNNY1) to the ones given at the end of the exam. Do these properties of the original system continue to hold in the presence of this rule? For each property that becomes false when the proposed rule is added to the system, state the name of the property and give a brief counter-example demonstrating that it does not hold in the presence of the new rule. Answer: Determinism: if true then 0 else succ(0) can now evaluate in one step to either 0 or succ 0. Uniqueness: Same counter-example. Grading scheme: One point off for an almost correct but slightly confused counter-example. Two points off for completely mangled or incomprehensible counter-example. Two points off for missing a property that becomes false. Two points off for each property that is incorrectly identified as becoming false. -3 for correct answer but no counter-examples No credit for no answer. Properties: Determinism (of one-step evaluation): if t t and t t, then t = t. Uniqueness (of normal forms): If t u and t u, where u and u are both normal forms, then u = u. Termination (of evaluation): For every term t there is some normal form t such that t t. Progress: If t : T, then either t is a value or else there is some t with t t. Preservation: If t : T and t t, then t : T. 4

4. (5 points) Suppose instead that we add this rule: t 2 t 2 if t 1 then t 2 else t 3 if t 1 then t 2 else t 3 (E-FUNNY2) Answer in the same format as problem 3: For each property that becomes false when the proposed rule is added, write its name and give a brief counter-example. The properties are listed again at the bottom of this page for easy reference. (N.b.: In this problem, we are considering the effect of adding the rule E-FUNNY2 to the original language of typed arithmetic expressions, not including the rule E-FUNNY1 proposed in problem 3.) Answer: Determinism: if false then (pred 0) else (succ 0) can now evaluate in one step to either succ 0 or if false then 0 else (succ 0). (There were several other correct sorts of counter-examples for this one.) Grading scheme: -3 for not identifying determinism as becoming false; -2 for not providing a counter-example (or for a mangled counter-example); -1 for a somewhat-right counter-example. -2 each for incorrectly identifying other properties as becoming false. Properties: Determinism (of one-step evaluation): if t t and t t, then t = t. Uniqueness (of normal forms): If t u and t u, where u and u are both normal forms, then u = u. Termination (of evaluation): For every term t there is some normal form t such that t t. Progress: If t : T, then either t is a value or else there is some t with t t. Preservation: If t : T and t t, then t : T. 5

5. (5 points) Suppose instead that we add this rule to the original languge of typed arithmetic expressions: pred false pred (pred false) (E-FUNNY3) Do the properties of the original system continue to hold in the presence of this rule? Answer in the same format as the previous two problems. Answer: Termination: pred false diverges. Grading scheme: -2 for each property wrong (to a maximum of five); -2 for giving the correct property that changed but not providing a counter-example. Properties: Determinism (of one-step evaluation): if t t and t t, then t = t. Uniqueness (of normal forms): If t u and t u, where u and u are both normal forms, then u = u. Termination (of evaluation): For every term t there is some normal form t such that t t. Progress: If t : T, then either t is a value or else there is some t with t t. Preservation: If t : T and t t, then t : T. 6

6. (5 points) Suppose instead that we add this rule to the original languge of typed arithmetic expressions: 0 : Bool (T-FUNNY4) Do the properties of the original system continue to hold in the presence of this rule? Answer in the same format as the previous three problems. Answer: Progress: if 0 then true else true has type Bool, is a normal form, and is not a value. Grading scheme: -3 for saying Preservation fails and Progress is preserved; -2 for for claiming other properies become false; -4 for claiming that no properties become false. Properties: Determinism (of one-step evaluation): if t t and t t, then t = t. Uniqueness (of normal forms): If t u and t u, where u and u are both normal forms, then u = u. Termination (of evaluation): For every term t there is some normal form t such that t t. Progress: If t : T, then either t is a value or else there is some t with t t. Preservation: If t : T and t t, then t : T. 7

7. (5 points) Suppose instead that we add this rule to the original languge of typed arithmetic expressions: pred 0 : Bool (T-FUNNY5) Do the properties of the original system continue to hold in the presence of this rule? Answer in the same format as the previous three problems. Answer: Preservation: pred 0 has type Bool and evaluates in one step to 0, which does not have type Bool. Properties: Determinism (of one-step evaluation): if t t and t t, then t = t. Uniqueness (of normal forms): If t u and t u, where u and u are both normal forms, then u = u. Termination (of evaluation): For every term t there is some normal form t such that t t. Progress: If t : T, then either t is a value or else there is some t with t t. Preservation: If t : T and t t, then t : T. 8

Untyped lambda-calculus 8. (12 points) Write down the normal forms of the following λ-terms, or none if a term has no normal form: (a) (λs. λz. s (s z)) (λx. x) Answer: λz. (λx. x) ((λx. x) z) (b) (λx. x) (λx. x) (λx. x) (λx. x x) Answer: λx. x x (c) (λt. λf. t) (λt. λf. f) (λt. λf. t) (λt. λf. f) Answer: λf. f (d) (λx. x x) (λx. x x) (λx. x) Answer: None (e) λx. (λx. x x) (λx. x x) Answer: λx. (λx. x x) (λx. x x) (f) (λf. (λx. f (λy. x x y)) (λx. f (λy. x x y))) (λs. λz. z) Answer: λz. z Grading scheme: Binary. 2 points each. 9

9. (6 points) Here are the definitions of the Church numerals and the basic operations over them from Chapter 5 of TAPL: c 0 = λs. λz. z; c 1 = λs. λz. s z; c 2 = λs. λz. s (s z); = λs. λz. s (s (s z)); c 3 scc = λn. λs. λz. s (n s z); plus = λm. λn. λs. λz. m s (n s z); iszro = λm. m (λx. fls) tru; tru = λt. λf. t fls = λt. λf. f and = λb. λc. b c fls; not = λb. b fls tru pair = λf.λs.λb. b f s; fst = λp. p tru; snd = λp. p fls; zz = pair c 0 c 0 ; ss = λp. pair (snd p) (plus c 1 prd = λm. fst (m ss zz); (snd p)); Use these combinators to fill in the blank in the following definition to yield a function less that, when applied to two church numerals, c m and c n, returns tru if m < n and otherwise returns fls. For example, less c 2 c 4 should evaluate to tru, while less c 3 c 1 and less c 3 c 3 should both evaluate to fls. Your answer should use only the combinators defined above (plus applications and variables). Do not write any explicit λ-abstractions. Answer: less = λm. λn. (not (iszro (m prd n))) Grading scheme: One point off for missing parenthesis or wrong order of evaluation; One point off for swapping m and n; Two points off for neglecting the case of m = n; Three points off for violating the rules (No lambda abstractions). 10

10. (6 points) Recall the Church encoding of lists from the solution to homework 4. nil = λc. λn. n; cons = λh. λt. λc. λn. c h (t c n); head = λl. l (λh.λt.h) fls; tail = λl. fst (l (λx. λp. pair (snd p) (cons x (snd p))) (pair nil nil)); isnil = λl. l (λh.λt.fls) tru; Fill in the blanks in the following definition to yield a lambda term map that takes a term l representing a list and a function f, applies f to each element of l, and yields a list of the results (just like the List.map function in OCaml). For example, map scc (cons c 2 (cons c 0 (cons c 1 nil))) should be equivalent to (cons c 3 (cons c 1 (cons c 2 nil))). Your answer should consist entirely of variables and applications no lambda-abstractions and no uses of any of the combinators defined above. Answer: map = λl. λf. λc. λn. l (λh. λt. c (f h) t) n Grading scheme: One point off for missing parenthesis or wrong order of evaluation; One point off for each slight mistake if the answer is almost right; Partial credits are generously given if the answer is structurally similar to the correct answer. 11

Nameless representation of terms 11. (2 points) Suppose we have defined the naming context Γ = a, b, c, d. Then one possible named representation of the debruijn term λ. 1 0 (λ. 1) would be λx. d x (λy. x). Write down a possible named representation for each of the following debruijn terms. (a) λ. λ. 1 3 0 Answer: λx. λy. x c y (b) λ. 3 (λ. 2 1 1) 1 Answer: Grading scheme: Binary λx. b (λy. d x x) d 12. (3 points) Write down (in debruijn notation) the normal form of the following debruijn term. (λ. λ. 1 (λ. 1)) (λ. 0) Answer: λ. (λ. 0) (λ.1) Grading scheme: Binary 12

Behavioral Equivalence 13. (14 points) Recall the definitions of observational and behavioral equivalence from the lecture notes: Two terms s and t are observationally equivalent iff either both are normalizable (i.e., they reach a normal form after a finite number of evaluation steps) or both are divergent. Terms s and t are behaviorally equivalent iff, for every finite sequence of values v 1, v 2,..., v n, the applications s v 1 v 2... v n and are observationally equivalent. t v 1 v 2... v n Recall, also, the following definitions of lambda-terms from the text: c 0 = λs. λz. z; c 1 = λs. λz. s z; c 2 = λs. λz. s (s z); c 3 = λs. λz. s (s (s z)); plus = λm. λn. λs. λz. m s (n s z); fix = λf. (λx. f (λy. x x y)) (λx. f (λy. x x y)); For each of the following pairs of terms, write Yes if the terms are behaviorally equivalent and no if they are not. (a) (b) tru λx. λy. (λz. z) x Answer: Yes λx. λy. x y λx. x Answer: Yes (c) plus c 2 c 1 c 3 Answer: Yes (d) λx. λy. x y λx. λy. x (λz. z) y Answer: No (e) (λx. x x) (λx. x x) (λx. x x x) (λx. x x x) Answer: Yes (f) (λx. x x) (λx. x x) λx. (λx. x x) (λx. x x) Answer: No (g) λf. (λx. f (x x)) (λx. f (x x)) fix Answer: No Grading scheme: Binary; each item worth two points. 13

For reference: Boolean and arithmetic expressions Syntax t ::= terms true constant true false constant false if t then t else t conditional 0 constant zero succ t successor pred t predecessor iszero t zero test v ::= true false nv values true value false value numeric value nv ::= numeric values 0 zero value succ nv successor value T ::= Bool Nat types type of booleans type of numbers Evaluation if true then t 2 else t 3 t 2 if false then t 2 else t 3 t 3 t 1 t 1 if t 1 then t 2 else t 3 if t 1 then t 2 else t 3 t 1 t 1 succ t 1 succ t 1 pred 0 0 pred (succ nv 1 ) nv 1 t 1 t 1 pred t 1 pred t 1 iszero 0 true iszero (succ nv 1 ) false t 1 t 1 iszero t 1 iszero t 1 (E-IFTRUE) (E-IFFALSE) (E-IF) (E-SUCC) (E-PREDZERO) (E-PREDSUCC) (E-PRED) (E-ISZEROZERO) (E-ISZEROSUCC) (E-ISZERO) continued on next page... 14

Typing true : Bool false : Bool t 1 : Bool t 2 : T t 3 : T if t 1 then t 2 else t 3 : T (T-TRUE) (T-FALSE) (T-IF) 0 : Nat (T-ZERO) t 1 : Nat succ t 1 : Nat t 1 : Nat pred t 1 : Nat t 1 : Nat iszero t 1 : Bool (T-SUCC) (T-PRED) (T-ISZERO) 15