The University of Nottingham

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

Recursive Types. Chapter 7

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

CMSC 330: Organization of Programming Languages

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

The University of Nottingham

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.

CMSC 330: Organization of Programming Languages. Lambda Calculus Encodings

Exercise 1 ( = 18 points)

CMSC 330: Organization of Programming Languages

T ::=Nat T T. zero : Nat. This rule can be instantiated for any type T, so it is actually a family of rules, one for each type.

Exercise 1 ( = 24 points)

Lambda Calculus and Extensions as Foundation of Functional Programming

Design Principles of Programming Languages. Recursive Types. Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term, 2014

Exercise 1 (2+2+2 points)

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

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

Functional Programming

Design Principles of Programming Languages. Recursive Types. Zhenjiang Hu, Haiyan Zhao, Yingfei Xiong Peking University, Spring Term

Concepts of programming languages

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

1 Introduction. 3 Syntax

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

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

Graphical Untyped Lambda Calculus Interactive Interpreter

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Sometimes it s good to be lazy

Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

Functional Programming

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

Lecture 14: Recursive Types

CSE-321 Programming Languages 2011 Final

Problem Set 4: Streams and Lazy Evaluation

CS450 - Structure of Higher Level Languages

Fundamental Concepts. Chapter 1

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

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

Theorem Proving Principles, Techniques, Applications Recursion

CSE 3302 Programming Languages Lecture 8: Functional Programming

Inductive data types (I)

CIS 500: Software Foundations

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.

1 Scope, Bound and Free Occurrences, Closed Terms

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

Types and Programming Languages. Lecture 8. Recursive type

CSE-321 Programming Languages 2010 Midterm

Exercise 1 ( = 22 points)

Lecture 5: Lazy Evaluation and Infinite Data Structures

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Getting Started with Haskell (10 points)

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

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

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CIS 500 Software Foundations Midterm I

CIS 500: Software Foundations

Programming with Math and Logic

Lecture 5: The Untyped λ-calculus

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

QIR Specification. Romain Vernoux. January 6, Notations 2

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

Lecture 9: Typed Lambda Calculus

Pure Lambda Calculus. Lecture 17

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

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

MLW. Henk Barendregt and Freek Wiedijk assisted by Andrew Polonsky. March 26, Radboud University Nijmegen

CMSC 330: Organization of Programming Languages

Introduction to the Lambda Calculus

λ calculus is inconsistent

CIS 194: Homework 6. Due Wednesday, 4 March. Fibonacci numbers. It s all about being lazy.

Functional Programming Languages (FPL)

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

Coq Summer School, Session 2 : Basic programming with numbers and lists. Pierre Letouzey

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

Organization of Programming Languages CS3200/5200N. Lecture 11

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Induction in Coq. Nate Foster Spring 2018

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

ITERATORS AND STREAMS 9

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

CMSC 330: Organization of Programming Languages

Functional Programming. Overview. Topics. Recall λ-terms. Examples

Logic - CM0845 Introduction to Haskell

Calculus of Inductive Constructions

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

Structure and Interpretation of Computer Programs

Handout 10: Imperative programs and the Lambda Calculus

Lambda Calculus. Lecture 4 CS /26/10

Introduction to the Lambda Calculus. Chris Lomont

Programming Languages Lecture 14: Sum, Product, Recursive Types

Type Systems Winter Semester 2006

CIS 500: Software Foundations

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Structure and Interpretation of Computer Programs

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

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

COMP 382: Reasoning about algorithms

Transcription:

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER 2013-2014 G54FOP FOUNDATIONS OF PROGRAMMING Time allowed 2 hours Candidates may complete the front cover of their answer book and sign their desk card but must NOT write anything else until the start of the examination period is announced Answer all 4 questions Marks available for sections of questions are shown in square brackets. No calculators are permitted in this examination. Dictionaries are not allowed with one exception. Those whose first language is not English may use a standard translation dictionary to translate between that language and English provided that neither language is the subject of this examination. Subject specific translation dictionaries are not permitted. No electronic devices capable of storing and retrieving text, including electronic dictionaries, may be used. DO NOT turn your examination paper over until instructed to do so Turn Over

2 Question 1: [25 pts] Representation of lists in the untyped λ-calculus Lists are constructed by using two combinators, nil for the empty list and cons for the construction of a new list from a head and a tail. At first we represent a list by repeated pairing: nil = false = λx.λy.y cons = λh.λt.(h, t) = λh.λt.λx.x h t Define combinators (pure λ-terms) head and tail for the head and tail of a list, respectively. They must satisfy these reduction relations (it doesn t matter what they do on empty lists): head (cons h t) h tail (cons h t) t Define a combinator isnil that checks if a list is empty. It must satisfy these reduction relations: isnil nil true isnil (cons h t) false Implement a recursor for lists: for every pair of terms c and g, define a combinator (lrec c g) satisfying these reduction relations: (lrec c g) nil c (lrec c g) (cons h t) g h t ((lrec c g) t) In your solution, you can use the fixpoint combinator: [7 pts] Y = λf.(λx.f (x x)) (λx.f (x x)). Now write an alternative implementation of lists as iterators. Define the new list constructors nil IT and cons IT so that they have the following reduction behaviour (for all terms c and g): nil IT c g c (cons IT h t) c g g h (t c g) Continued on next page

3 Question 2: [25 pts] Simply typed λ-calculus and system T [10 pts] Give a short precise definition of the following properties that a type system may or may not satisfy: Confluence Weak Normalization Strong Normalization Progress Subject Reduction (Preservation) [6 pts] The following are incomplete terms in the simply typed λ-calculus with a base type o. Rewrite the terms, replacing each question mark with the type of the corresponding abstracted variable; also state what type each resulting term has: λx :?. λy :?. λz :?. x (z y) y λx :?. λy :?. y (x y) λx :?. λy :?. λz :?. z (y z (x z)) The aim of this part is to program the Fibonacci sequence in system T. The common recursive implementation uses pairs, but system T doesn t have Cartesian products. Instead we use higher-order recursion to define an auxiliary function that takes the initial values as arguments: fib : Nat Nat fib 0 = 0 fib 1 = 1 fib (S (S n)) = (fib n) + (fib (S n)) fib HO : Nat (Nat Nat Nat) fib HO 0 a b = a fib HO (S n) a b = fib HO n b (a + b) then fib = λn.fib HO n 0 1 Give the definition of fib HO in system T. Turn Over

4 Question 3: [25 pts] Inductive Types and System F We define a type of games with two players. When the game finishes, each player will receive a certain payoff. On each player s turn, they can choose between two new positions, the left or the right move. We implement game positions by the inductive type with the following introduction rules: x, y : Nat (end x y) : Game r, l : Game (play r l) : Game Write the elimination and reduction rules for the type Game. Define the type Game in system F and implement the recursor (from the elimination rule). [Remember the method to define a recursor from an iterator: define an auxilliary function that gives as result a pair whose first element is a copy of the input.] A strategy is a function that tells a player how to choose between two game positions: Strategy = Game Game Bool. (If the result is true the first position is chosen, if it is false the second position is chosen.) Write (using the recursor from the elimination rule) a function that, given strategies for the two players, computes the outcome of a game: outcome : Game Strategy Strategy Nat Nat. Continued on next page

5 Question 4: [25 pts] Coinductive Types Consider the two coinductive types of streams and of infinite trees on a base type A, defined by: S A = νx.a X T A = νx.a X X. For each of the two types write down its formal rules (elimination, introduction, reduction). [For the rest of the Question, you can use the functions head, tail and for streams; and label, lchild, rchild and node for trees.] Using the rules from part, implement the interleave function on streams: interleave : S A S A S A That takes two streams a 0 a 1 a 2, b 0 b 1 b 2 and returns the stream a 0 b 0 a 1 b 1 a 2 b 2. Also write two functions that select the elements on even and odd positions: evens : S A S A odds : S A S A Write two functions that convert a tree to a stream and vice-versa [the label is the head of the stream, the left subtree contains the odd elements, the right subtree contains the (nonzero) even elements]: stream tree : S A T A tree stream : T A S A [Hint: For tree stream you should extend the function to operate on the type Tree TA of finite trees with infinite trees as leaves. You can use informal pattern-matching notation to define a coalgebra on it.] End