On every sheet please give your first name, last name, and matriculation number.

Similar documents
POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

Deterministic Discrete Modeling

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Data Structures and Algorithms Written Examination

COMPUTER SCIENCE TRIPOS

CSI 333 Lecture 1 Number Systems

Regular Expressions and Automata using Haskell

Computability Theory

The Needle Programming Language

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Practice with Proofs

Fun with Phantom Types

CS 103X: Discrete Structures Homework Assignment 3 Solutions

How To Understand The Theory Of Computer Science

C H A P T E R Regular Expressions regular expression

TOPIC 4: DERIVATIVES

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Termination Checking: Comparing Structural Recursion and Sized Types by Examples

Chapter 4 Lecture Notes

Automata and Computability. Solutions to Exercises

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Activity 1: Using base ten blocks to model operations on decimals

Chapter 15 Functional Programming Languages

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

CHAPTER 3. Methods of Proofs. 1. Logical Arguments and Formal Proofs

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

Special Directions for this Test

The Technology Behind a Graphical User Interface for an Equational Reasoning Assistant

GAP CLOSING. 2D Measurement. Intermediate / Senior Student Book

Perfion Output Using Special Barcode fonts

arrays C Programming Language - Arrays

Section IV.1: Recursive Algorithms and Recursion Trees

Creating, Solving, and Graphing Systems of Linear Equations and Linear Inequalities

Lecture 11: Tail Recursion; Continuations

How To Program In Scheme (Prolog)

Discrete Mathematics and Probability Theory Fall 2009 Satish Rao, David Tse Note 2

Kapitel 1 Multiplication of Long Integers (Faster than Long Multiplication)

1998. (R. Bird and P. Wadler, Introduction to Functional Programming, Prentice

1. Let P be the space of all polynomials (of one real variable and with real coefficients) with the norm

INTRODUCTION TO FUNCTIONAL PROGRAMMING Take-Home Exam

The University of the State of New York REGENTS HIGH SCHOOL EXAMINATION MATHEMATICS B. Tuesday, August 16, :30 to 11:30 a.m.

CSIS 202: Introduction to Computer Science Spring term Midterm Exam

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Math 319 Problem Set #3 Solution 21 February 2002

CS170 Lab 11 Abstract Data Types & Objects

The Prime Numbers. Definition. A prime number is a positive integer with exactly two positive divisors.

J a v a Quiz (Unit 3, Test 0 Practice)

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

SECTION 10-2 Mathematical Induction

CPSC 121: Models of Computation Assignment #4, due Wednesday, July 22nd, 2009 at 14:00

COMPSCI 105 S2 C - Assignment 2 Due date: Friday, 23 rd October 7pm

36 CHAPTER 1. LIMITS AND CONTINUITY. Figure 1.17: At which points is f not continuous?

Machine Learning and Data Mining. Regression Problem. (adapted from) Prof. Alexander Ihler

MATHEMATICS. Y5 Multiplication and Division 5330 Square numbers, prime numbers, factors and multiples. Equipment. MathSphere

Aspect-Oriented Programming with Type Classes

Section 4.2: The Division Algorithm and Greatest Common Divisors

Solving Quadratic Equations

CORRELATED TO THE SOUTH CAROLINA COLLEGE AND CAREER-READY FOUNDATIONS IN ALGEBRA

Software Systems Engineering

This asserts two sets are equal iff they have the same elements, that is, a set is determined by its elements.

Project 2: Bejeweled

1. Admission requirements for the master program

Course outline. Code: ICT311 Title: Software Development 2

Lecture 1: Schur s Unitary Triangularization Theorem

Lecture Data Types and Types of a Language

Programming Languages

Sudoku puzzles and how to solve them

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Grade 6 Math Circles. Binary and Beyond

1 Formulating The Low Degree Testing Problem

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

Programming and Reasoning with Side-Effects in IDRIS

Lecture 22: C Programming 4 Embedded Systems

Lecture 13 - Basic Number Theory.

Object-Oriented Programming in Java

HOMEWORK 5 SOLUTIONS. n!f n (1) lim. ln x n! + xn x. 1 = G n 1 (x). (2) k + 1 n. (n 1)!

MATH 425, PRACTICE FINAL EXAM SOLUTIONS.

Idris, a General Purpose Dependently Typed Programming Language: Design and Implementation

Nonlinear Algebraic Equations. Lectures INF2320 p. 1/88

Notes on Complexity Theory Last updated: August, Lecture 1

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Chapter 6: Programming Languages

Chapter 7: Functional Programming Languages

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

minimal polyonomial Example

Comp 255Q - 1M: Computer Organization Lab #3 - Machine Language Programs for the PDP-8

Database Design. Database Design I: The Entity-Relationship Model. Entity Type (con t) Chapter 4. Entity: an object that is involved in the enterprise

Listen and Learn PRESENTED BY MATHEMAGICIAN Mathematics, Grade 7

2 When is a 2-Digit Number the Sum of the Squares of its Digits?

Transcription:

RHEINISCH- WESTFÄLISCHE TECHNISCHE HOCHSCHULE AACHEN LEHR- UND FORSCHUNGSGEBIET INFORMATIK 2 RWTH Aachen D-52056 Aachen GERMANY http://verify.rwth-aachen.de/ LuFG Informatik II Functional Programming Exam, February 24, 2010 Prof. Dr. Jürgen Giesl Carsten Fuhs First name: Last name: Matr. number: Course of study (please mark exactly one): Master of Informatik Bachelor of Informatik for Master s studies On every sheet please give your first name, last name, and matriculation number. You must solve the exam without consulting any extra documents (e.g., course notes). Make sure your answers are readable. Do not use red pens or pencils. Please answer the exercises on the exercise sheets. If needed, also use the back sides of the exercise sheets. Answers on extra sheets can only be accepted if they are clearly marked with your name, your matriculation number, and the exercise number. Cross out text that should not be considered in the evaluation. Students that try to cheat do not pass the exam. At the end of the exam, please return all sheets together with the exercise sheets. Total number of points Number of points obtained Exercise 1 16 Exercise 2 9 Exercise 3 6 Exercise 4 6 Exercise 5 9 Exercise 6 10 Total 56 Grade -

2 Exercise 1 (4 + 3 + 4 + 5 = 16 points) The following data structure represents polymorphic lists that can contain values of two types in arbitrary order: data DuoList a b = C a (DuoList a b) D b (DuoList a b) E Consider the following list zs of integers and characters: [ 4, a, b, 6 ] The representation of zs as an object of type DuoList Int Char in Haskell would be: Implement the following functions in Haskell. (a) The function foldduo of type C 4 (D a (D b (C 6 E))) (a -> c -> c) -> (b -> c -> c) -> c -> DuoList a b -> c works as follows: foldduo f g h xs replaces all occurrences of the constructor C in the list xs by f, it replaces all occurrences of the constructor D in xs by g, and it replaces all occurrences of the constructor E in xs by h. So for the list zs above, should compute foldduo (*) (\x y -> y) 3 zs (*) 4 ((\x y -> y) a ((\x y -> y) b ((*) 6 3))), which in the end results in 72. Here, C is replaced by (*), D is replaced by (\x y -> y), and E is replaced by 3.

3 (b) Use the foldduo function from (a) to implement the cd function which has the type DuoList Int a -> Int and returns the sum of the entries under the data constructor C and of the number of elements built with the data constructor D. In our example above, the call cd zs should have the result 12. The reason is that zs contains the entries 4 and 6 under the constructor C and it contains two elements a and b built with the data constructor D.

4 (c) Consider the following data type declaration for natural numbers: data Nats = Zero Succ Nats A graphical representation of the first four levels of the domain fornats could look like this: Succ (Succ Zero) Succ (Succ (Succ )) Succ Zero Succ (Succ ) Zero Succ We define the following data type Single, which has only one data constructor One: data Single = One Sketch a graphical representation of the first three levels of the domain for the data type DuoList Bool Single.

5 (d) Write a Haskell function printlength that first reads a line from the user, then prints this string on the console and in the end also prints the length of this string on the console. Also give the type declaration for your function. You may use the do-notation, but you are not obliged to use it. Some of the following pre-defined functions can be helpful: getline :: IO String reads a line from the user length :: String -> Int has the length of a string as its result show :: Int -> String converts a number to a string putstr :: String -> IO () writes a string to the console An example run should look as given below. Here the string foo was read from the user. Main> printlength foo foo3

6 Exercise 2 (4 + 5 = 9 points) Consider the following Haskell declarations for the square function: square :: Int -> Int square 0 = 0 square (x+1) = 1 + 2*x + square x (a) Please give the Haskell declarations for the higher-order function f square corresponding to square, i.e., the higher-order function f square such that the least fixpoint of f square is square. In addition to the function declaration(s), please also give the type declaration of f square. Since you may use full Haskell for f square, you do not need to translate square into simple Haskell. (b) We add the Haskell declaration bot = bot. For each n N please determine which function is computed by f square n bot. Here f square n bot represents the n-fold application of f square to bot, i.e., it is short for f square (f square... (f square bot)...). } {{ } n times Let f n : Z Z be the function that is computed by f square n bot. Give f n in closed form, i.e., using a non-recursive definition.

7 Exercise 3 (6 points) Let D 1, D 2 be domains, let D2 be a complete partial order on D 2. As we know from the lecture, then also D1 D 2 is a complete partial order on the set of all functions from D 1 to D 2. Prove that D1 D 2 is also a complete partial order on the set of all constant functions from D 1 to D 2. A function f : D 1 D 2 is called constant iff f(x) = f(y) holds for all x, y D 1. Hint: The following lemma may be helpful: If S is a chain of functions from D 1 to D 2, then S is the function with: ( S)(x) = {f(x) f S}

8 Exercise 4 (6 points) We define the following data structures for natural numbers and polymorphic lists: data Nats = Zero Succ Nats data List a = Nil Cons a (List a) Consider the following expression in complex Haskell: let length Nil = Zero length (Cons x xs) = Succ (length xs) in length Please give an equivalent expression let length =... in length in simple Haskell. Your solution should use the functions defined in the transformation from the lecture such as sel n,i, isa constr, and argof constr. However, you do not have to use the transformation rules from the lecture.

9 Exercise 5 (4 + 5 = 9 points) Consider the following data structure for polymorphic lists: data List a = Nil Cons a (List a) (a) Please translate the following Haskell-expression into an equivalent lambda term (e.g., using Lam). Recall that pre-defined functions like even are translated into constants of the lambda calculus. It suffices to give the result of the transformation. let f = \x -> if (even x) then Nil else Cons x (f x) in f

10 (b) Let δ be the set of rules for evaluating the lambda terms resulting from Haskell, i.e., δ contains at least the following rules: fix λf. f (fix f) plus 2 3 5 Now let the lambda term t be defined as follows: t = (fix (λg x. Cons (plus x 3) Nil)) 2 Please reduce the lambda term t by WHNO-reduction with the βδ -relation. You have to give all intermediate steps until you reach weak head normal form (and no further steps).

11 Exercise 6 (10 points) Use the type inference algorithm W to determine the most general type of the following lambda term under the initial type assumption A 0. Show the results of all sub-computations and unifications, too. If the term is not well typed, show how and why the W-algorithm detects this. λf. (Succ (f x)) The initial type assumption A 0 contains at least the following: A 0 (Succ) = (Nats Nats) A 0 (f) = a. a A 0 (x) = a. a