Exercise 1 ( = 22 points)

Similar documents
Exercise 1 ( = 18 points)

Exercise 1 ( = 24 points)

Exercise 1 (2+2+2 points)

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

CS 320: Concepts of Programming Languages

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

λ calculus is inconsistent

Lecture 19: Functions, Types and Data Structures in Haskell

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Functional Languages and Higher-Order Functions

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

Lecture 5: Lazy Evaluation and Infinite Data Structures

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

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Typed Racket: Racket with Static Types

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

Inductive Definitions, continued

Lecture 4: Higher Order Functions

Metaprogramming assignment 3

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

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

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

Logic - CM0845 Introduction to Haskell

Dependencies by case or by function?

CSE-321 Programming Languages 2010 Midterm

Concepts of programming languages

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.

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

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

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

CS 320: Concepts of Programming Languages

Shell CSCE 314 TAMU. Haskell Functions

Graphical Untyped Lambda Calculus Interactive Interpreter

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

CS 320 Midterm Exam. Fall 2018

Lambda Calculus and Type Inference

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Type-indexed functions in Generic Haskell

CSE-321 Programming Languages 2011 Final

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

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

CSCE 314 Programming Languages

Fall Lecture 3 September 4. Stephen Brookes

Lecture 2: List algorithms using recursion and list comprehensions

An introduction introduction to functional functional programming programming using usin Haskell

CIS 500 Software Foundations Midterm I

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

Theorem Proving Principles, Techniques, Applications Recursion

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

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

CMSC 330: Organization of Programming Languages

Lambda Calculus and Extensions as Foundation of Functional Programming

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

Programming Language Concepts: Lecture 14

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

The Typed Racket Guide

02157 Functional Programming Tagged values and Higher-order list functions

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CMSC 330: Organization of Programming Languages

Lambda Calculus and Type Inference

Lambda Calculus as a Programming Language

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

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

Type families and data kinds

CSE 3302 Programming Languages Lecture 8: Functional Programming

Introduction to Functional Programming in Haskell 1 / 56

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

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda 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

Type Checking and Type Inference

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Functional Programming and Haskell

Fall Recursion and induction. Stephen Brookes. Lecture 4

Programming Language Concepts: Lecture 19

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

Lists. Michael P. Fourman. February 2, 2010

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

02157 Functional Programming. Michael R. Ha. Tagged values and Higher-order list functions. Michael R. Hansen

Introduction to Programming, Aug-Dec 2006

Program Calculus Calculational Programming

Lecture 14: Recursive Types

The University of Nottingham

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

CS 440: Programming Languages and Translators, Spring 2019 Mon

Harvard School of Engineering and Applied Sciences Computer Science 152

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

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Lambda Calculi With Polymorphism

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Pattern Matching and Abstract Data Types

Polymorphism and Type Inference

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

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

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CS115 - Module 9 - filter, map, and friends

Lecture #23: Conversion and Type Inference

Imperative languages

Transcription:

1 Exercise 1 (4 + 3 + 4 + 5 + 6 = 22 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. foldduo f g h (C x xs) foldduo f g h (D x xs) foldduo f g h E = f x (foldduo f g h xs) = g x (foldduo f g h xs) = h

2 (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. cd = foldduo (+) (\x y -> y + 1) 0

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 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. C True C (C ) C (D ) D One D (C ) D (D ) C False C E D E C E D

4 (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 -- without do-notation printlength :: IO () printlength = getline >>= \s -> putstr s >> putstr (show (length s)) -- alternative: with do-notation printlength2 :: IO () printlength2 = do s <- getline putstr s putstr (show (length s))

5 (e) The digit sum of a natural number is the sum of all digits of its decimal representation. For example, the digit sum of the number 6042 is 6 + 0 + 4 + 2 = 12. Write a Haskell function digitsum :: Int -> Int that takes a natural number and returns its digit sum. Your function may behave arbitrarily on negative numbers. It can be helpful to use the pre-defined functions div, mod :: Int -> Int -> Int to compute result and remainder of division, respectively. For example, div 7 3 is 2 and mod 7 3 is 1. digitsum :: Int -> Int digitsum 0 = 0 digitsum (n+1) = mod (n+1) 10 + digitsum (div (n+1) 10) Now implement a functiondigitsumlist :: Int -> Int -> [Int] wheredigitsumlist n b returns a list of all those numbers x where 0 x b and where the digit sum of x is n. Perform your implementation only with the help of a list comprehension, i.e., you should use exactly one declaration of the following form: digitsumlist... = [...... ] Of course, here you can (and should) make use of the function digitsum to compute the digit sum of a number. digitsumlist :: Int -> Int -> [Int] digitsumlist n b = [ x x <- [0..b], digitsum x == n ]

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. f square :: (Int -> Int) -> (Int -> Int) f square square 0 = 0 f square square (x+1) = 1 + 2*x + square x (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. (f square n ( ))(x) = { x 2, if 0 x < n, otherwise

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} We need to show two statements: a) The set of all constant functions from D 1 to D 2 has a smallest element. Obviously, the constant function f with f(x) = for all x D 1 satisfies this requirement. b) For every chain S on the set of all constant functions from D 1 to D 2 there is a least upper bound S which is an element of the set of all constant functions from D 1 to D 2. Let S be a chain of constant functions from D 1 to D 2. By the above lemma, we have ( S)(x) = {f(x) f S}. It remains to show that the function S : D 1 D 2 actually is a constant function. For all x, y D 1, we have: ( S)(x) = {f(x) f S} = {f(y) f S} since the elements of S are constant functions = ( S)(y) Therefore, also ( S)(x) is a constant function.

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 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 (fix (λf x. if (even x) Nil (Cons x (f x)) ) )

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) 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). (fix (λg x. Cons (plus x 3) Nil)) 2 δ ((λf. f (fix f)) (λg x. Cons (plus x 3) Nil)) 2 β ((λg x. Cons (plus x 3) Nil) (fix (λg x. Cons (plus x 3) Nil))) 2 β ((λx. Cons (plus x 3) Nil) 2 β Cons (plus 2 3) Nil

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. λ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 W(A 0, λf. (Succ (f x)) ) W(A 0 + {f :: b 1 }, (Succ (f x)) ) W(A 0 + {f :: b 1 }, Succ) = (id, (Nats Nats) ) W(A 0 + {f :: b 1 }, (f x) ) W(A 0 + {f :: b 1 }, f) = (id, b 1 ) W(A 0 + {f :: b 1 }, x) = (id, b 2 ) mgu(b 1, (b 2 b 3 ) ) = [b 1 /(b 2 b 3 )] = ([b 1 /(b 2 b 3 )], b 3 ) mgu((nats Nats), (b 3 b 4 ) ) = [b 3 /Nats, b 4 /Nats] = ([b 1 /(b 2 Nats), b 3 /Nats, b 4 /Nats], Nats) = ([b 1 /(b 2 Nats), b 3 /Nats, b 4 /Nats], ((b 2 Nats) Nats) ) Resulting type: ((b 2 Nats) Nats)