Exercise 1 (2+2+2 points)

Similar documents
Exercise 1 ( = 18 points)

Exercise 1 ( = 24 points)

Exercise 1 ( = 22 points)

Logic - CM0845 Introduction to Haskell

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

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

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

λ calculus is inconsistent

Functional Languages and Higher-Order Functions

Concepts of programming languages

Lecture 4: Higher Order Functions

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

CS 320: Concepts of Programming Languages

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

CSE-321 Programming Languages 2010 Midterm

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.

Theorem Proving Principles, Techniques, Applications Recursion

The University of Nottingham

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

Lecture 19: Functions, Types and Data Structures in Haskell

Lecture 5: Lazy Evaluation and Infinite Data Structures

CSE-321 Programming Languages 2012 Midterm

CIS 500 Software Foundations Midterm I

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

CSE 3302 Programming Languages Lecture 8: Functional Programming

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

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

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

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

INTRODUCTION TO FUNCTIONAL PROGRAMMING

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

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

Metaprogramming assignment 3

Inductive Definitions, continued

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

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

1 Introduction. 3 Syntax

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

CSE-321 Programming Languages 2010 Final

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

CSCE 314 Programming Languages

IA014: Advanced Functional Programming

Haskell 98 in short! CPSC 449 Principles of Programming Languages

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

Shell CSCE 314 TAMU. Higher Order Functions

CSCE 314 Programming Languages

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

CSE-321 Programming Languages 2011 Final

Type families and data kinds

CSE505, Fall 2012, Midterm Examination October 30, 2012

CS 320: Concepts of Programming Languages

Lists. Michael P. Fourman. February 2, 2010

Typed Lambda Calculus and Exception Handling

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

Lecture 14: Recursive Types

QIR Specification. Romain Vernoux. January 6, Notations 2

Tree Equality: The Problem

1.3. Conditional expressions To express case distinctions like

Introduction to Programming, Aug-Dec 2006

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

CS 320: Concepts of Programming Languages

Introduction to Functional Programming in Haskell 1 / 56

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

Programming Language Concepts, CS2104 Lecture 7

Lambda Calculus and Extensions as Foundation of Functional Programming

Inductive data types

Programming Languages Lecture 15: Recursive Types & Subtyping

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

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

Advanced features of Functional Programming (Haskell)

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

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

CSCI-GA Scripting Languages

Lambda Calculus and Type Inference

Type-indexed functions in Generic Haskell

Programming with Math and Logic

CSC173 Lambda Calculus Lambda Calculus Evaluation (10 min) Evaluate this expression (β-reduce with normal-order evaluation):

Mutable References. Chapter 1

Topic 7: Algebraic Data Types

Principles of Programming Languages 2017W, Functional Programming

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

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

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

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

Lambda Calculi With Polymorphism

Simple Unification-based Type Inference for GADTs

CS 440: Programming Languages and Translators, Spring 2019 Mon

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Lambda Calculus and Type Inference

Inductive Data Types

Structural polymorphism in Generic Haskell

Typed Racket: Racket with Static Types

Graphical Untyped Lambda Calculus Interactive Interpreter

Analysis of dependent types in Coq through the deletion of the largest node of a binary search tree

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

Standard ML. Data types. ML Datatypes.1

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

CS558 Programming Languages

GADTs. GADTs in Haskell

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

Transcription:

1 Exercise 1 (2+2+2 points) The following data structure represents binary trees only containing values in the inner nodes: data Tree a = Leaf Node (Tree a) a (Tree a) 1 Consider the tree t of integers on the right-hand side. The representation of t as an object of type Tree Int in Haskell would be: 2 Node (Node Leaf 2 Leaf) 1 Leaf Implement the following functions in Haskell. (a) Please implement the function swaptree which returns the tree where all left children have been swapped with the corresponding right children. When computing swaptree t one would obtain the tree on the right-hand side. Apart from the function declaration, also give the most general type declaration for swaptree. You may not use any higher-order functions. 1 swaptree :: Tree a -> Tree a swaptree Leaf = Leaf swaptree (Node l x r) = Node (swaptree r) x (swaptree l) 2

2 (b) The function foldtree of type (a -> b -> a -> a) -> a -> Tree b -> a works as follows: foldtree n l t replaces all occurrences of the constructor Node in the tree t by n and it replaces all occurrences of the constructor Leaf in t by l. Suppose there is the function add: add :: Int -> Int -> Int -> Int add x y z = x + y + z For the tree t from (a), foldtree add 0 t should compute: foldtree add 0 (Node (Node Leaf 2 Leaf) 1 Leaf) = add (add 0 2 0 ) 1 0 = 3 Here, Node is replaced by add and Leaf is replaced by 0. Now use the foldtree function to implement the swaptree function again. swaptree = foldtree (\x y z -> Node z y x) Leaf

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 (Node ) Node True Node False Node (Node ) Node Leaf Node Leaf Leaf Node

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

5 Exercise 3 (3+3 points) Let be a complete order and let f be a function which is continuous (and therefore also monotonic). Prove or disprove the following statements: (a) { f n ( ) n {0, 1, 2,...} } is a chain. We prove f n ( ) f n+1 ( ) for all n {0, 1, 2,...} by induction on n. n = 0: By definition we have f( ). n n + 1: The function f is continuous and therefore also monotonic. Thus, f n ( ) f n+1 ( ) implies f n+1 ( ) f n+2 ( ). (b) { f n ( ) n {0, 1, 2,...} } is a fixpoint of f. f( {f n ( ) n {0, 1, 2,...}}) f continuous = f({f n ( ) n {0, 1, 2,...}}) = {f n+1 ( ) n {0, 1, 2,...}} = {f n ( ) n {1, 2,...}} = ({f n ( ) n {1, 2,...}} { }) = ({f n ( ) n {1, 2,...}} {f 0 ( )}) = {f n ( ) n {0, 1, 2,...}}

6 Exercise 4 (3 points) We define the following algebraic data type for lists: data List a = Nil Cons a (List a) Write declarations in simple Haskell for the function maxlist:: List Int -> Int. Here, for empty lists the function should return bot. For non-empty lists, maxlist should return the maximum of that list. For example, maxlist (Cons 1 (Cons (-2) Nil)) should return 1. Your solution should use the functions defined in the transformation from the lecture such as sel n,i, isa constr, argof constr, and bot. You do not have to use the transformation rules from the lecture, though. Additionally, you may use the built-in function max:: Int -> Int -> Int for computing the maximum of two integers. maxlist= \xs -> if (isa Cons xs) then if (isa Nil (sel 2,2 (argof Cons xs))) then sel 2,1 (argof Cons xs) else max (sel 2,1 (argof Cons xs)) (maxlist (sel 2,2 (argof Cons xs))) else bot

7 Exercise 5 (2+4 points) Consider the following data structures for natural numbers and polymorphic lists: data Nats = Zero Succ Nats data List a = Nil Cons a (List a) Let δ be the set of rules from Definition 3.3.5, i.e., δ contains among others the following rules: fix λf. f (fix f) if True isa Nil Nil λx y. x True (a) Please translate the following Haskell-expression into an equivalent lambda term (e.g., using Lam). It suffices to give the result of the transformation. let length= \xs -> if (isa Nil xs) then Zero else Succ (length (sel 2,2 (argof Cons xs))) in length fix (λlength xs. if (isa Nil xs) Zero (Succ (length (sel 2,2 (argof Cons xs)))))

8 (b) Let fix t be the lambda term from (a). Please reduce (fix t)nil by WHNO-reduction with the δ -relation. You have to give all intermediate steps until one reaches weak head normal form. We have t = (λlength xs. if (isa Nil xs) Zero (Succ (length (sel 2,2 (argof Cons xs))))) δ δ δ fix t Nil (λf. f (fix f)) t Nil t (fix t) Nil (λxs. if (isa Nil xs) Zero (Succ (fix t (sel 2,2 (argof Cons xs))))) Nil if (isa Nil Nil) Zero (Succ (fix t (sel 2,2 (argof Cons Nil)))) if True Zero (Succ (fix t (sel 2,2 (argof Cons Nil)))) (λx y. x) Zero (Succ (fix t (sel 2,2 (argof Cons Nil)))) (λy. Zero) (Succ (fix t (sel 2,2 (argof Cons Nil)))) Zero

9 Exercise 6 (4 points) Use the type inference algorithm W to determine the most general type of the following λ-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. f (Succ Zero) The initial type assumption A 0 contains at least the following: A 0 (f) = a. a A 0 (Succ) = Nats Nats A 0 (Zero) = Nats W(A 0, λf. f (Succ Zero)) W(A 0 + {f :: b 0 }, f (Succ Zero)) W(A 0 + {f :: b 0 }, f) = (id, b 0 ) W(A 0 + {f :: b 0 }, Succ Zero) W(A 0 + {f :: b 0 }, Succ) = (id, (Nats Nats)) W(A 0 + {f :: b 0 }, Zero) = (id, Nats) building mgu of (Nats Nats) and (Nats b 1 ) = [b 1 /Nats] = ([b 1 /Nats], Nats) building mgu of b 0 and (Nats b 2 ) = [b 0 /(Nats b 2 )] = ([b 1 /Nats, b 0 /(Nats b 2 )], b 2 ) = ([b 1 /Nats, b 0 /(Nats b 2 )], ((Nats b 2 ) b 2 )) Resulting type: ((Nats b 2 ) b 2 )