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

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

CITS3211 FUNCTIONAL PROGRAMMING

Lecture 5: Lazy Evaluation and Infinite Data Structures

Exercise 1 ( = 24 points)

CSE 3302 Programming Languages Lecture 8: Functional Programming

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

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

Pure Lambda Calculus. Lecture 17

Exercise 1 (2+2+2 points)

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

1 Scope, Bound and Free Occurrences, Closed Terms

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

Course Notes Equational Programming: Lambda Calculus. Femke van Raamsdonk

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

Lambda Calculus and Extensions as Foundation of Functional Programming

Graphical Untyped Lambda Calculus Interactive Interpreter

CS 360: Programming Languages Lecture 12: More Haskell

Lambda Calculus and Type Inference

Lecture 4: Higher Order Functions

CS 440: Programming Languages and Translators, Spring 2019 Mon

Functional Languages and Higher-Order Functions

Chapter 5: The Untyped Lambda Calculus

Functional Programming

Exercise 1 ( = 18 points)

Programming Language Concepts: Lecture 14

CS 11 Haskell track: lecture 1

Shell CSCE 314 TAMU. Haskell Functions

Getting Started with Haskell (10 points)

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

What does my program mean?

CMSC 330: Organization of Programming Languages. Lambda Calculus

Exercise 1 ( = 22 points)

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

Concepts of programming languages

Lecture 19: Functions, Types and Data Structures in Haskell

The Untyped Lambda Calculus

CMSC 330: Organization of Programming Languages

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

λ calculus is inconsistent

CMSC 330: Organization of Programming Languages. Lambda Calculus

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

CSE 505: Concepts of Programming Languages

Fundamentals and lambda calculus

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

Functional Programming for Logicians - Lecture 1

Lambda Calculus and Type Inference

An introduction introduction to functional functional programming programming using usin Haskell

IA014: Advanced Functional Programming

Getting Started with Haskell (10 points)

301AA - Advanced Programming

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

The Untyped Lambda Calculus

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

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

CSCE 314 Programming Languages

Structural polymorphism in Generic Haskell

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

Introduction. chapter Functions

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

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

CMSC 330: Organization of Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Functional Programming

CSE-321 Programming Languages 2011 Final

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

International School on Rewriting (ISR 2015) Rewriting Techniques for Correctness of Program Transformations

Last class. CS Principles of Programming Languages. Introduction. Outline

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

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

Recursive Definitions, Fixed Points and the Combinator

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Elixir, functional programming and the Lambda calculus.

Logic - CM0845 Introduction to Haskell

Lambda Calculus-2. Church Rosser Property

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Introduction to Lambda Calculus. Lecture 5 CS 565 1/24/08

dynamically typed dynamically scoped

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Week 5 Tutorial Structural Induction

Functional Logic Programming: From Theory to Curry

Programming Languages Fall 2013

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

Formal Systems and their Applications

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

CS 457/557: Functional Languages

Type Systems Winter Semester 2006

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

Parallel Haskell on MultiCores and Clusters

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

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

Higher Order Functions in Haskell

CSCE 314 Programming Languages

INTRODUCTION TO HASKELL

CSC324 Principles of Programming Languages

301AA - Advanced Programming [AP-2017]

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

Shell CSCE 314 TAMU. Higher Order Functions

Transcription:

Topics Functional Programming Christian Sternagel Harald Zankl Evgeny Zuenko Department of Computer Science University of Innsbruck WS 2017/2018 abstract data types, algebraic data types, binary search trees, combinator parsing, efficiency, encoding data types as lambda-terms, evaluation strategies, formal verification, first steps, guarded recursion, Haskell introduction, higher-order functions, historical overview, implementing a type checker, induction, infinite data structures, input and output, lambda-calculus, lazy evaluation, list comprehensions, lists, modules, pattern matching, polymorphism, property-based testing, reasoning about functional programs, recursive functions, sets, strings, tail recursion, trees, tupling, type checking, type inference, types, types and type classes, unification, user-defined types Lecture 6 CS,HZ,EZ (DCS @ UIBK) lecture 6 2/26 Overview Recall λ-terms t ::= x (t t) (λx. t) conventions verbose in words x y (x y) x applied to y λx. x (λx. x) lambda x to x (identity function) λxy. x (λx. (λy. x)) lambda x y to x λx. x x (λx. (x x)) lambda x to x applied to x (λx. x) x ((λx. x) x) lambda x to x, applied to x CS,HZ,EZ (DCS @ UIBK) lecture 6 3/26 CS,HZ,EZ (DCS @ UIBK) lecture 6 4/26

Order of Evaluation Recall β-reduction term s (β-)reduces to term t in one step written: s β t iff there is subterm (λx. u) v of s, s.t., replacing (λx. u) v in s by u[x := v] results in t K = λxy. x I = λx. x Ω = (λx. x x) (λx. x x) consider d x = x + x the term d (d 2) may be evaluated as follows d (d 2) d (2 + 2) d 2 + d 2 (2 + 2) + d 2 d 2 + (2 + 2) d 4 4 + d 2 (2 + 2) + (2 + 2) d 2 + 4 4 + (2 + 2) (2 + 2) + 4 4 + 4 8 CS,HZ,EZ (DCS @ UIBK) lecture 6 5/26 CS,HZ,EZ (DCS @ UIBK) lecture 6 6/26 (Reduction) Strategies fix evaluation order call by value (idea: compute arguments before function calls) call by name (idea: compute arguments on demand only) call by value d (d 2) = d (2 + 2) = d 4 = 4 + 4 = 8 what is called evaluation strategy in programming, is typically called reduction strategy in λ-calculus call by name d (d 2) = d 2 + d 2 = (2 + 2) + d 2 = 4 + d 2 = 4 + (2 + 2) = 4 + 4 = 8 Applicative Order Reduction reduce rightmost innermost redex redex is innermost if it does not contain redexes itself consider t = (λx. (λy. y) x) z (λy. y) x is innermost redex t is redex, but not innermost CS,HZ,EZ (DCS @ UIBK) lecture 6 7/26 CS,HZ,EZ (DCS @ UIBK) lecture 6 8/26

Normal Order Reduction reduce leftmost outermost redex redex is outermost if it is not contained in another redex consider t = (λx. (λy. y) x) z t is outermost redex (λy. y) x is redex, but not outermost Exercises consider the λ-terms S = λxyz. x z (y z) K = λxy. x I = λx. x reduce S K I to NF using applicative order reduction reduce S K I to NF using normal order reduction CS,HZ,EZ (DCS @ UIBK) lecture 6 9/26 CS,HZ,EZ (DCS @ UIBK) lecture 6 10/26 Further Classification of λ-terms a term that is not an application is called value a term is called weak head normal form (WHNF) if it satisfies: whnf(x) = true whnf(λx. t) = true whnf((λx. t) u) = false whnf(t u) = whnf(t) Call by Value stop at values otherwise choose othermost redex whose right-hand side is value corresponds to strict (or eager) evaluation adopted by most programming languages term t value WHNF (λx. x) x x y x λx. (λy. y) x Call by Name stop at WHNFs otherwise same as normal order (that is, leftmost outermost redex) corresponds to lazy evaluation (without memoization) adopted for example by Haskell CS,HZ,EZ (DCS @ UIBK) lecture 6 11/26 CS,HZ,EZ (DCS @ UIBK) lecture 6 12/26

Idea hide implementation details just provide interface allows us to change implementation (e.g., make more efficient) without breaking client code Haskell Characteristics of Sets order of elements not important no duplicates {1, 2, 3, 5} = {5, 1, 3, 2} {1, 1, 2, 2} = {1, 2} consider module module M (T,...) where type T = C1... CN only name T is exported, but none of C1 to CN thus we are not able to directly construct values of type T if we want to export C1 to CN, we can use T(..) in export list CS,HZ,EZ (DCS @ UIBK) lecture 6 13/26 Operations on Sets description notation Haskell empty set empty :: Set a insertion {x} S insert :: a -> Set a -> Set a membership e S mem :: a -> Set a -> Bool union S T union :: Set a -> Set a -> Set a difference S \ T diff :: Set a -> Set a -> Set a CS,HZ,EZ (DCS @ UIBK) lecture 6 14/26 Sets as Lists module import data Set (Set, empty, insert, mem, union, diff) where qualified Data.List as List Set a = Set [a] empty :: Set a empty = Set [] insert :: Eq a => a -> Set a -> Set a insert x (Set xs) = Set $ List.nub $ x : xs mem :: Eq a => a -> Set a -> Bool x `mem` Set xs = x `elem` xs Note Imports import M imports all functions and types exported by module M we may restrict to f1,..., fn, writing import M (f1,..., fn) by import M hiding (f1,..., fn) we import everything except the functions f1 to fn qualified M allows us to access all functions exported by M using prefix M. import import qualified M as N, same as import qualified M but additionally rename M to N union, diff :: Eq a => Set a -> Set a -> Set a union (Set xs) (Set ys) = Set $ List.nub $ xs ++ ys diff (Set xs) (Set ys) = Set $ xs List.\\ ys CS,HZ,EZ (DCS @ UIBK) lecture 6 15/26 CS,HZ,EZ (DCS @ UIBK) lecture 6 16/26

New Types data with single constructor Set was used to hide implementation in this common special case use newtype Set a = Set a instead only difference: newtype has better performance than data Record Syntax for data type / new type T, instead of C t1... tn, we may use C {n1 :: t1,..., nn :: tn} as constructor provides selector functions n1 :: T -> t1,..., nn :: T -> tn data Equation a = E { lhs :: a, rhs :: a } ghci> let e1 = E "10" "5+5" ghci> let e2 = E { rhs = "5+5", lhs = "10" } ghci> lhs e1 "10" ghci> rhs e2 "5+5" CS,HZ,EZ (DCS @ UIBK) lecture 6 17/26 The Type use type BTree without prefix: import BTree (BTree(..)) import remaining functions from BTree with prefix import qualified BTree internal representation of set is binary tree (with selector rep) newtype Set a = Set { rep :: BTree a } Note newtype Set a = Set { rep :: BTree a } is almost the same as writing type Set a = BTree a additionally type system prevents us from accidentally (that is, without constructor Set) using BTrees as Sets no runtime penalty (in contrast to data Set a = Set { rep :: BTree }) reason: newtype restricted to single constructor (usually of same name as newly introduced type) data may have arbitrarily many constructors (e.g., Empty and Node) CS,HZ,EZ (DCS @ UIBK) lecture 6 18/26 Empty Set empty :: Set a empty = Set Empty Membership mem :: Ord a => a -> Set a -> Bool x `mem` s = x `memtree` rep s where memtree x Empty = False memtree x (Node y l r) = case compare x y of EQ -> True LT -> x `memtree` l GT -> x `memtree` r Insertion insert :: Ord a => a -> Set a -> Set a insert x s = Set $ inserttree x $ rep s inserttree :: Ord a => a -> BTree a -> BTree a inserttree x Empty = Node x Empty Empty inserttree x (Node y l r) = case compare x y of EQ -> Node y l r LT -> Node y (inserttree x l) r GT -> Node y l (inserttree x r) CS,HZ,EZ (DCS @ UIBK) lecture 6 19/26 CS,HZ,EZ (DCS @ UIBK) lecture 6 20/26

Union union :: Ord a => Set a -> Set a -> Set a union s t = Set $ rep s `uniontree` rep t Removing the Maximal Element splitmaxtree :: BTree a -> Maybe (a, BTree a) splitmaxtree Empty = Nothing splitmaxtree (Node x l Empty) = Just (x, l) splitmaxtree (Node x l r) = let Just (m, r') = splitmaxtree r in Just (m, Node x l r') uniontree :: Ord a => BTree a -> BTree a -> BTree a uniontree Empty s = s uniontree (Node x l r) s = inserttree x $ l `uniontree` r `uniontree` s The Maybe Type Prelude: data Maybe a = Just a Nothing used for type-based error handling if an error occurs, we return Nothing otherwise Just the result Safe Head safehead (x:_) = Just x safehead _ = Nothing CS,HZ,EZ (DCS @ UIBK) lecture 6 21/26 CS,HZ,EZ (DCS @ UIBK) lecture 6 22/26 Remove Given Element removetree :: Ord a => a -> BTree a -> BTree a removetree x Empty = Empty removetree x (Node y l r) = case compare x y of LT -> Node y (removetree x l) r GT -> Node y l (removetree x r) EQ -> case splitmaxtree l of Nothing -> r Just (m, l') -> Node m l' r For Binary Search Tree (BST) x smaller y: x can only occur in l x greater y: x can only occur in r x equals y: remove current node and combine l and r into new BST therefore, take maximum of l as new root guarantees that all other elements in l are smaller and that all elements in r are greater CS,HZ,EZ (DCS @ UIBK) lecture 6 23/26 Difference diff :: Ord a => Set a -> Set a -> Set a diff s t = Set $ rep s `difftree` rep t difftree :: Ord a => BTree a -> BTree a -> BTree a difftree t Empty = t difftree t (Node x l r) = removetree x t `difftree` l `difftree` r CS,HZ,EZ (DCS @ UIBK) lecture 6 24/26

Exercises (for November 24th) 1. Read Chapter 4 of Real World Haskell and Section 5 of the lecture notes on the lambda calculus. 2. Reduce add 2 3 to NF using applicative and normal order reduction. 3. Let type Strat = Term -> [Term] be the type of reduction strategies. Implement the strategy root :: Strat which applies a single β-step at the root (if possible). 4. Implement a strategy combinator nested :: Strat -> Strat that, given a strategy s, results in a new strategy which tries to apply s at all non-root positions. 5. Building on the previous functions, implement single-step call by name reduction cbn :: Strat. 6. Implement the function equals :: Ord a => Set a -> Set a -> Bool, checking whether two sets are equal. root x = [] no beta-step possible root ((λx. x) u) = [u] root reduction root (x (λx. t) u) = [] no redex at root position single beta-steps strictly below root position nested root (((λx. x) y) ((λz. z) w)) = [y ((λz. z) w),(λx. x) y w] single-step call by name reduction cbn (((λx. x) ((λy. y) z)) ((λw. w) v)) = [((λy. y) z) ((λw. w) v)] CS,HZ,EZ (DCS @ UIBK) lecture 6 25/26 CS,HZ,EZ (DCS @ UIBK) lecture 6 26/26