Exercise 1 ( = 24 points)

Similar documents
Exercise 1 ( = 18 points)

Exercise 1 ( = 22 points)

Exercise 1 (2+2+2 points)

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

CSCE 314 Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

Functional Languages and Higher-Order Functions

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

CS 320: Concepts of Programming Languages

Lecture 19: Functions, Types and Data Structures in Haskell

Lecture 5: Lazy Evaluation and Infinite Data Structures

Lecture 4: Higher Order Functions

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

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

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

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

Haskell Overview II (2A) Young Won Lim 8/9/16

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

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

The University of Nottingham

CSE-321 Programming Languages 2010 Midterm

CS 320: Concepts of Programming Languages

Haskell Overview II (2A) Young Won Lim 8/23/16

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

λ calculus is inconsistent

Typed Scheme: Scheme with Static Types

Theorem Proving Principles, Techniques, Applications Recursion

If a program is well-typed, then a type can be inferred. For example, consider the program

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

Graphical Untyped Lambda Calculus Interactive Interpreter

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

CS 320 Homework One Due 2/5 11:59pm

CS 209 Functional Programming

Lambda Calculus and Type Inference

CS 440: Programming Languages and Translators, Spring 2019 Mon

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

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Concepts of programming languages

Type-indexed functions in Generic Haskell

CSE 3302 Programming Languages Lecture 8: Functional Programming

IA014: Advanced Functional Programming

Haskell 98 in short! CPSC 449 Principles of Programming Languages

An introduction introduction to functional functional programming programming using usin Haskell

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

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

Programming Languages Fall 2013

Dependencies by case or by function?

Principles of Programming Languages

Haskell through HUGS THE BASICS

CIS 500 Software Foundations Midterm I

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Recursion. Lars-Henrik Eriksson. Functional Programming 1. Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström

Type Systems. Parametric Polymorphism. 1. Recall Let-Polymorphism. 1. Recall Let-Polymorphism. Lecture 9 Dec. 15th, 2004 Sebastian Maneth

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

Introduction to Functional Programming in Haskell 1 / 56

Haskell Overview II (2A) Young Won Lim 9/26/16

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

The Polymorphic Blame Calculus and Parametricity

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

Logic - CM0845 Introduction to Haskell

Introduction to Programming, Aug-Dec 2006

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.

Inductive Definitions, continued

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

A general introduction to Functional Programming using Haskell

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

Type families and data kinds

Lambda Calculus and Extensions as Foundation of Functional Programming

Recursion and Structural Induction

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.

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Shell CSCE 314 TAMU. Haskell Functions

CSE-321 Programming Languages 2011 Final

Introduction to Functional Programming using Haskell

1.3. Conditional expressions To express case distinctions like

Advanced features of Functional Programming (Haskell)

CS 320 Midterm Exam. Fall 2018

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

CMSC 330: Organization of Programming Languages

Streams and Evalutation Strategies

Metaprogramming assignment 3

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Lambda Calculus as a Programming Language

CSC/MAT-220: Lab 6. Due: 11/26/2018

INTRODUCTION TO FUNCTIONAL PROGRAMMING

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

Generalized Iteration and Coiteration for Higher-Order Nested Datatypes

More Lambda Calculus and Intro to Type Systems

CSCE 314 Programming Languages

CSE505, Fall 2012, Midterm Examination October 30, 2012

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

CMSC 330: Organization of Programming Languages

Lists. Michael P. Fourman. February 2, 2010

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

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

Programming in Omega Part 1. Tim Sheard Portland State University

Lambda Calculus as a Programming Language

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

Transcription:

1 Exercise 1 (4 + 5 + 4 + 6 + 5 = 24 points) The following data structure represents polymorphic binary trees that contain values only in special Value nodes that have a single successor: data Tree a = Leaf Node (Tree a) (Tree a) Value a (Tree a) Consider the tree t of characters on the right-hand side. The representation of t as an object of type Tree Char in Haskell would be: (Node (Value a (Value b Leaf)) (Node (Node Leaf Leaf) (Value c Leaf))) Implement the following functions in Haskell. a b c (a) The function foldtree of type (a -> b -> b) -> (b -> b -> b) -> b -> Tree a -> b works as follows: foldtree f g h x replaces all occurrences of the constructor Value in the tree x by f, it replaces all occurrences of the constructor Node in x by g, and it replaces all occurrences of the constructor Leaf in x by h. So for the tree t above, should compute foldtree (:) (++) [] t ((++) ((:) a ((:) b [])) ((++) ((++) [] []) ((:) c []))), which in the end results in "abc" (i.e., in the list [ a, b, c ]). Here, Value is replaced by (:), Node is replaced by (++), and Leaf is replaced by []. foldtree f g h (Value n x) = f n (foldtree f g h x) foldtree f g h (Node x y) = g (foldtree f g h x) (foldtree f g h y) foldtree h Leaf = h

2 (b) Use the foldtree function from (a) to implement the average function which has the type Tree Int -> Int and returns the average of the values that are stored in the tree. This should be accomplished as follows: Use foldtree with suitable functions as arguments in order to compute the sum of the values stored in the trees. Use foldtree with suitable functions as arguments in order to compute the number of Value-objects in the tree. Perform integer division with the pre-defined function div :: Int -> Int -> Int on these values to obtain the result. Here your function is required to work correctly only on those trees that contain the constructor Value at least once. average t = div (foldtree (+) (+) 0 t) (foldtree (\x y -> y+1) (+) 0 t)

3 (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 Sketch a graphical representation of the first three levels of the domain for the data type Tree Bool. Node (Value ) Node (Node ) Value (Node ) Value (Value ) Node Leaf Value Leaf Value True Value False Node Leaf Node (Node ) Node (Value ) Value Leaf Node

4 (d) We call a list ys of integers an n-times even product of a list xs if ys has length n and if all elements of ys are even numbers that occur in xs. The goal of this exercise is to write a function evenproducts :: [Int] -> Int -> [[Int]] that takes a list of integers xs and a natural number n and returns a list that contains all n-times even products of xs. For example, evenproducts [4,5,6] 2 = [[4,4], [4,6], [6,4], [6,6]]. The following declarations are already given: evenproducts :: [Int] -> Int -> [[Int]] evenproducts xs 0 = [] evenproducts xs 1 = map (\z -> [z]) (filter even xs) Please give the declaration of evenproducts for the missing case of numbers that are at least 2. Perform your implementation only with the help of a list comprehension, i.e., you should use exactly one declaration of the following form: evenproducts xs (n+2) = [...... ] evenproducts xs (n+2) = [ y:ys y <- xs, even y, ys <- evenproducts xs (n+1)]

5 (e) We define the level n of a tree as the list of those values that are at distance n from the root of the tree. Here, the root node has distance 0 from the root, and a non-root node has distance n + 1 from the root if its parent node has distance n from the root. Write a Haskell function level :: Tree a -> Int -> [a] which, given a tree t and a natural number n, computes the list of all values in t that occur there at level n (with repetition, i.e., a value should appear in the result list as many times as it appears on level n). As an example, consider again the tree t from the beginning of the exercise. Here we have level t 2 = [ b, c ] and level t 7 = []. level :: Tree a -> Int -> [a] level t 0 = case t of Value x u -> [x] -> [] level t (n+1) = case t of Leaf -> [] Value x u -> level u n Node u v -> (level u n) ++ (level v n)

6 Exercise 2 (4 + 5 = 9 points) Consider the following Haskell declarations for the fib function, which for a natural number x computes the value fibonacci(x): fib :: Int -> Int fib 0 = 0 fib 1 = 1 fib (x+2) = fib (x+1) + fib x (a) Please give the Haskell declarations for the higher-order function f fib corresponding to fib, i.e., the higher-order function f fib such that the least fixpoint of f fib is fib. In addition to the function declaration(s), please also give the type declaration of f fib. Since you may use full Haskell for f fib, you do not need to translate fib into simple Haskell. f fib :: (Int -> Int) -> (Int -> Int) f fib fib 0 = 0 f fib fib 1 = 1 f fib fib (x+2) = fib (x+1) + fib x (b) We add the Haskell declaration bot = bot. For each n N please determine which function is computed by f fib n bot. Here f fib n bot represents the n-fold application of f fib to bot, i.e., it is short for f fib (f fib... (f fib bot)...). }{{} n times Let f n : Z Z be the function that is computed by f fib n bot. Give f n in closed form, i.e., using a non-recursive definition. In this definition, you may use the function fibonacci : N N where fibonacci(x) computes the x-th Fibonacci number. Here it suffices to give the result of your calculations. You do not need to present any intermediate steps. { (f fib n fibonacci(x), if n > 0 and 0 x n ( ))(x) =, otherwise

7 Exercise 3 (3 + 3 = 6 points) Let D 1, D 2, D 3 be domains with corresponding complete partial orders D1, D2, D3. As we know from the lecture, then also (D2 D 3 ) is a complete partial order on (D 2 D 3 ). Now let f : D 1 D 2 and g : D 1 D 3 be functions. We then define the function h : D 1 (D 2 D 3 ) via h(x) = (f(x), g(x)). (a) Prove or disprove: If f and g are strict functions, then also h is a strict function. The statement does not hold. Consider the following counterexample: D 1 = D 2 = D 3 = B and f = g = B B. Obviously f and g are strict functions, i.e., f( B ) = g( B ) = B. However, we have h( B ) = ( B, B ) (B B ). (b) Prove or disprove: If f and g are monotonic functions, then also h is a monotonic function. Let x D1 y. Then we have: h(x) = (f(x), g(x)) f and g are monotonic, def. of (D2 D 3 ) (f(y), g(y)) (D2 D 3 ) = h(y) Hence, also h is monotonic.

8 Exercise 4 (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 odd or (+) are translated into constants of the lambda calculus. It suffices to give the result of the transformation. let f = \x -> if (odd x) then (\y -> x) else f ((+) x 3) in f fix (λf x. if (odd x) (λy.x) (f ((+) x 3)) )

9 (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) times 3 2 6 Now let the lambda term t be defined as follows: t = (λx. (fix λg. x)) (λz. (times 3 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). (λx. (fix λg. x)) (λz. (times 3 2)) β fix (λg. λz. (times 3 2)) δ (λf. f (fix f)) (λg. λz. (times 3 2)) β (λg. λz. (times 3 2)) (fix (λg. λz. (times 3 2))) β λz. (times 3 2)

10 Exercise 5 (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. ( (Cons λx. x) y ) The initial type assumption A 0 contains at least the following: A 0 (Cons) = a. (a (List a List a)) A 0 (x) = a. a A 0 (y) = a. a W(A 0, ( (Cons λx. x) y ) ) W(A 0, (Cons λx. x) ) W(A 0, Cons) = (id, (b 1 (List b 1 List b 1 )) ) W(A 0, λx. x ) W(A 0 + {x :: b 2 }, x ) = (id, b 2 ) = (id, (b 2 b 2 )) mgu( (b 1 (List b 1 List b 1 )), ((b 2 b 2 ) b 3 ) ) = [ b 1 /(b 2 b 2 ), b 3 /(List (b 2 b 2 ) List (b 2 b 2 )) ] = ( [ b 1 /(b 2 b 2 ), b 3 /(List (b 2 b 2 ) List (b 2 b 2 )) ], (List (b 2 b 2 ) List (b 2 b 2 )) ) W(A 0, y) = (id, b 4 ) mgu( (List (b 2 b 2 ) List (b 2 b 2 )), (b 4 b 5 ) ) = [ b 4 /List (b 2 b 2 ), b 5 /List (b 2 b 2 ) ] = ( [ b 1 /(b 2 b 2 ), b 3 /(List (b 2 b 2 ) List (b 2 b 2 )), b 4 /List (b 2 b 2 ), b 5 /List (b 2 b 2 ) ], List (b 2 b 2 ) ) Resulting type: List (b 2 b 2 )