CSE-321 Programming Languages 2010 Midterm

Similar documents
CSE-321 Programming Languages 2012 Midterm

CSE-321 Programming Languages 2014 Final

CSE-321 Programming Languages 2011 Final

CSE-321 Programming Languages 2010 Final

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

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

Type Systems Winter Semester 2006

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

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

Concepts of programming languages

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

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

Chapter 5: The Untyped Lambda Calculus

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

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

CMSC 330: Organization of Programming Languages

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

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

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

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

Mutable References. Chapter 1

Lecture Notes on Data Representation

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

1 Introduction. 3 Syntax

Introduction to the Lambda Calculus

CSE 341 Section 5. Winter 2018

Exercise 1 ( = 24 points)

Programming Languages

CSE 505: Concepts of Programming Languages

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

CSE505, Fall 2012, Midterm Examination October 30, 2012

Type Inference; Parametric Polymorphism; Records and Subtyping Section and Practice Problems Mar 20-23, 2018

Lecture 9: Typed Lambda Calculus

More Lambda Calculus and Intro to Type Systems

Exercise 1 (2+2+2 points)

Exercise 1 ( = 18 points)

Lambda Calculus as a Programming Language

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

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

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

CMSC 330: Organization of Programming Languages

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

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Explicit Recursion in Generic Haskell

Pure Lambda Calculus. Lecture 17

Functional Programming

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Graphical Untyped Lambda Calculus Interactive Interpreter

CMSC 330: Organization of Programming Languages

Lambda Calculus as a Programming Language

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

Exercise 1 ( = 22 points)

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

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

Data types for mcrl2

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

dynamically typed dynamically scoped

4.5 Pure Linear Functional Programming

Programming Languages Lecture 15: Recursive Types & Subtyping

CpSc 421 Final Solutions

λ calculus is inconsistent

More Lambda Calculus and Intro to Type Systems

Costly software bugs that could have been averted with type checking

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Inductive datatypes in HOL. lessons learned in Formal-Logic Engineering

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

Theorem Proving Principles, Techniques, Applications Recursion

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

Introduction to the λ-calculus

Lecture 5: The Untyped λ-calculus

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

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

Lists. Michael P. Fourman. February 2, 2010

Lecture Notes on Induction and Recursion

1 Scope, Bound and Free Occurrences, Closed Terms

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

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

The Untyped Lambda Calculus

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

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor.

CSE341 Autumn 2017, Midterm Examination October 30, 2017

Generalized Iteration and Coiteration for Higher-Order Nested Datatypes

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

CITS3211 FUNCTIONAL PROGRAMMING

Programming Languages Fall 2014

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

Denotational Semantics. Domain Theory

Calculus INT. Value Types A, B ::= types of the low-level language Interactive Types X, Y ::= [A] A X X Y A X

1.3. Conditional expressions To express case distinctions like

(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

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

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

Programming Languages

CSE 215: Foundations of Computer Science Recitation Exercises Set #4 Stony Brook University. Name: ID#: Section #: Score: / 4

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011

Transcription:

Name: Hemos ID: CSE-321 Programming Languages 2010 Midterm Score Prob 1 Prob 2 Prob 3 Prob 4 Total Max 15 30 35 20 100 1

1 SML Programming [15 pts] Question 1. [5 pts] Give a tail recursive implementation of preorder for preorder traversals of binary trees. Fill in the blank: datatype a tree = Leaf of a Node of a tree * a * a tree (* preorder : a tree -> a list *) fun preorder t = let fun preorder (Leaf x) pre = pre @ [x] preorder (Node (left, x, right)) pre = in preorder right (preorder left (pre @ [x])) preorder t [] end Question 2. [7 pts] DICT is a signature for dictionaries: signature DICT = sig type key type a dict val empty : unit -> a dict val lookup : a dict -> key -> a option val delete : a dict -> key -> a dict val insert : a dict -> key * a -> a dict end key denotes the type of keys in dictionaries. a dict denotes the type of dictionaries for a type values. empty () returns an empty dictionary. lookup d k searches the key k in the dictionary d. If the key is found, it returns the associated item. Otherwise, it returns NONE. delete d k deletes the key k and its associated item in the dictionary d and returns the resultant dictionary d. If the key does not exist in the dictionary d, it returns the given dictionary d without any modification. insert d (k, v) inserts the new key k and its associated item v in the dictionary d. If the key k already exists in the dictionary d, it just updates its associated item with the given item v. 2

Implement the functor DictFn which takes a KEY structure and generates a corresponding DICT structure that uses a functional representation of dictionaries. Fill in the blank: signature KEY = sig type t (* eq k k : true k is equal to k *) (* false otherwise *) val eq : t * t -> bool end functor DictFn (Key : KEY) :> DICT where type key = Key.t = struct type key = Key.t type a dict = key -> a option fun empty () = fn => NONE fun lookup d k = d k fun delete d k = fn k => if Key.eq (k, k ) then NONE else d k fun insert d (k, v) = end fn k => if Key.eq (k, k ) then SOME v else d k 3

Question 3. [3 pts] Give an implementation of IntDict whose key type is int. Fill in the blank. You may use the functor DictFn that you write in Question 2. structure IntKey :> KEY where type t = int = struct type t = int fun eq (k, k ) = k = k end structure IntDict = DictFn (IntKey) 4

2 Inductive definitions [30 pts] Consider the following system from the Course Notes where s mparen means that s is a string of matched parentheses. ɛ mparen Meps s mparen (s) mparen Mpar s 1 mparen s 2 mparen s 1 s 2 mparen Mseq In order to show that if s mparen holds, s is indeed a string of matched parentheses, we introduce a new judgment k s where k is a non-negative integer: k s k left parentheses concatenated with s form a string of matched parentheses (( ( s is a string of matched parentheses }{{} k The idea is that we scan a given string from left to right and keep counting the number of left parentheses that have not yet been matched with corresponding right parentheses. Thus we begin with k = 0, increment k each time a left parenthesis is encountered, and decrement k each time a right parenthesis is encountered: 0 ɛ Peps k + 1 s k (s Pleft k 1 s k > 0 k )s Pright The second premise k > 0 in the rule Pright ensures that in any prefix of a given string, the number of right parentheses may not exceed the number of left parentheses. Now a judgment 0 s expresses that s is a string of matched parentheses. Your task is to prove Theorem 2.1. If you need a lemma to complete the proof, state the lemma, prove it, and use it to complete the proof of Theorem 2.1. For individual steps in the proof, please use the following format: conclusion Theorem 2.1. If s mparen, then 0 s. justification 5

6

7

8

3 λ-calculus [35 pts] Question 1. [5 pts] Show the reduction sequence under the call-by-name strategy. Underline the redex at each step. (λx. λy. y x) ((λx. x) (λy. y)) (λz. z) (λy. y ((λx. x) (λy. y))) (λz. z) (λz. z) ((λx. x) (λy. y)) (λx. x) (λy. y) (λy. y) Question 2. [3 pts] Complete the definition of FV(e) that finds the set of free variables in e. FV(x) = {x} FV(λx. e) = FV(e) {x} FV(e 1 e 2 ) = FV(e 1 ) FV(e 2 ) Question 3. [2 pts] Fill in the blank with the set of free variables of the given expression. FV(λx. x) = {} FV(x y) = {x, y} FV(λx. x y) = {y} FV(λx. λy. x y) = {} FV((λx. x y) (λy. x y)) = {y, x} 9

Question 4. [5 pts] This question assumes types var and exp that we have seen in Assignment 4: type var = string datatype exp = Var of var Lam of var * exp App of exp * exp Suppose that we have two functions notfv and varswap: notfv : var -> exp -> bool notfv x e returns true if x is a free variable of e and false otherwise. varswap : var * var -> exp -> exp varswap (x, y) e returns [x y]e. Below is a function aequal of type ( exp * exp ) -> bool such that aequal (e 1, e 2 ) returns true if e 1 and e 2 are α-equivalent and false otherwise. fun aequal (Var x, Var y) = x = y aequal (App (e1, e2), App (e1, e2 )) = aequal (e1, e1 ) andalso aequal (e2, e2 ) aequal (Lam (x, e), Lam (y, e )) = if x = y then aequal (e, e ) else if notfv x e then aequal (e, varswap (y, x) e ) else false aequal = false We write e α e if e can be rewritten as e by renaming bound variables in e and vice versa. Give exactly four inference rules corresponding to the above definition of aequal. Use the notation x / FV(e) for notfv x e and [x y]e for varswap (x, y) e. x α x e 1 α e 1 e 2 α e 2 e 1 e 2 α e 1 e 2 e α e λx. e α λx. e x y x FV(e ) e α [y x]e λx. e α λy. e 10

Question 5. [8 pts] A Church numeral encodes a natural number n as a λ-abstraction ˆn which takes a function f and returns f n = f f f (n times): ˆn = λf. f n = λf. λx. f f f f x In this question, you will define three functions: sub for the subtraction operation, mod for the modulo operation, and, as an extra credit problem, div for the division operation. Your answers may use the following pre-defined constructs: zero, one, succ, if/then/else, pair/fst/snd, pred, eq, and fix. zero and one encode the natural numbers zero and one, respectively. zero = ˆ0 = λf. λx. x one = ˆ1 = λf. λx. f x succ finds the successor of a given natural number. succ = λ n. λf. λx. n f (f x) if e then e 1 else e 2 is a conditional construct. if e then e 1 else e 2 = e e 1 e 2 pair creates a pair of two expressions, and fst and snd are projection operators. pair = λx. λy. λb. b x y fst = λp. p (λt. λf. t) snd = λp. p (λt. λf. f) pred computes the predecessor of a given natural number where the predecessor of 0 is 0. eq tests two natural numbers for equality. fix is the fixed point combinator. pred = λ n. fst ( n next (pair zero zero)) eq = λx. λy. and (iszero (x pred y)) (iszero (y pred x)) fix = λf. (λf. F λx. (f f x)) (λf. F λx. (f f x)) These constructs use the following auxiliary constructs, which you do not need: tt = λt. λf. t ff = λt. λf. f and = λx. λy. x y ff iszero = λx. x (λy. ff) tt next = λp. pair (snd p) (succ (snd p)) 11

Define a subtraction function sub such that sub ˆm ˆn evaluates to m n if m > n and ˆ0 otherwise. sub = λ ˆm. λˆn. (ˆn pred) ˆm Define a modulo function mod such that mod ˆm ˆn evaluates to ˆr if r is the remainder of division of m by n. mod never takes ˆ0 as the second argument. Hence the result of evaluating mod ˆm ˆ0 is unspecified. You may use the subtraction function sub that you define above. mod = fix ( λf. λ ˆm. λˆn. if eq ˆm ˆn then zero else if eq (sub ˆm ˆn) zero then ˆm else (f (sub ˆm ˆn) ˆn) ) Extra credit question. [10 pts] Define a division function div such that div ˆm ˆn evaluates to ˆq if q is the quotient of m divided by n. div never takes ˆ0 as the second argument. Hence the result of evaluating div ˆm ˆ0 is unspecified. In this question, you are not allowed to use the fixed point combinator (and its definition), but you may use the subtraction function sub that you define above. div = λ ˆm. λˆn. snd ( ˆm (λp. if eq (fst p) ˆn then pair zero (succ (snd p)) else if eq (sub (fst p) ˆn) zero then p else pair (sub (fst p) ˆn) (succ (snd p))) (pair ˆm zero)) 12

Question 6. [3 pts] Give an expression whose reduction does not terminate. (λx. x x) (λx. x x) Question 7. [5 pts] Following is the definition of de Bruijn expressions: de Bruijn expression M ::= n λ. M M M de Bruijn index n ::= 0 1 2 Suppose that you are given the definition of τi n (N) for shifting by n (i.e., incrementing by n) all de Bruijn indexes in N corresponding to free variables, where a de Bruijn index m in N such that m < i does not count as a free variable. Complete the definition of σ n (M, N) for substituting N for every occurrence of n in M where N may include free variables. σ n (M 1 M 2, N) = σ n (M 1, N) σ n (M 2, N) σ n (λ. M, N) = λ. σ n+1 (M, N) σ n (m, N) = m if m < n σ n (n, N) = τ n 0 (N) σ n (m, N) = m 1 if m > n Question 8. [4 pts] Show the reduction of the given expression where the redex is underlined. λ. λ. (λ. (λ. 3 2 1 0) (2 1 0)) (λ. 0) λ. λ. (λ. 2 1 (λ. 0) 0) (1 0 (λ. 0)) (λ. (λ. 1) 0) (λ. 2 1 0) (λ. λ. 3 2 0) (λ. 2 1 0) 13

4 Simply-typed λ-calculus [20 pts] Question 1. [2 pts] Consider the following simply-typed λ-calculus: type A ::= bool A A expression e ::= x λx:a. e e e true false if e then e else e Write the typing rules for x, λx:a. e, and e e: Γ x : A Γ λx:a. e : A B Γ e e : B Question 2. [3 pts] Consider the extension of the simply-typed λ-calculus with product types: type A ::= A A expression e ::= (e, e) fst e snd e Write the reduction rules for these constructs under lazy reduction strategy: 14

Question 3. [5 pts] Consider the extension of the simply-typed λ-calculus with sum types: Write the typing rules: type A ::= A+A expression e ::= inl A e inr A e case e of inl x. e inr x. e Γ inl A e : B+A Γ inr A e : A+B Γ case e of inl x 1. e 1 inr x 2. e 2 : C Question 4. [5 pts] Consider the extension of the simply-typed λ-calculus with fixed point constructs expression e ::= fix x:a. e Write the typing rule for fix x:a. e and its reduction rule. Γ fix x:a. e : A 15

Question 5. [5 pts] Consider the following SML program: fun even 0 = true even 1 = false even n = odd (n - 1) and odd 0 = false odd 1 = true odd n = even (n - 1) The function even calls the function odd, and the function odd calls the function even. We refer to these functions as mutually recursive functions. Write an expression of type (int bool) (int bool) that encodes both even and odd in the simply-typed λ-calculus: type A ::= int bool A A A A expression e ::= x λx:a. e e e (e, e) fst e snd e () true false if e then e else e fix x:a. e = 0 1 We assume that the infix operations and = are given as primitive, which correspond to the integer substitution and equality test, respectively. 16