Introduction to Functional Programming in Haskell 1 / 56

Similar documents
COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Programming with Math and Logic

A general introduction to Functional Programming using Haskell

Functional Programming. Big Picture. Design of Programming Languages

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

An introduction introduction to functional functional programming programming using usin Haskell

CS 11 Haskell track: lecture 1

INTRODUCTION TO FUNCTIONAL PROGRAMMING

CSCC24 Functional Programming Scheme Part 2

Lecture 19: Functions, Types and Data Structures in Haskell

LECTURE 16. Functional Programming

Denotational Semantics. Domain Theory

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

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

Introduction. chapter Functions

Logic - CM0845 Introduction to Haskell

Functional Languages. Hwansoo Han

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

Functional Programming

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

A Small Interpreted Language

Imperative languages

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Outline. What is semantics? Denotational semantics. Semantics of naming. What is semantics? 2 / 21

CSC324 Principles of Programming Languages

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

CS 242. Fundamentals. Reading: See last slide

CSCE 314 Programming Languages

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

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

Programming Languages Fall Prof. Liang Huang

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

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

Functional Programming. Pure Functional Languages

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

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

Functional Programming. Pure Functional Languages

Concepts of Programming Languages

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

Higher Order Functions in Haskell

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

CS 320: Concepts of Programming Languages

Functional Programming and Haskell

Functional Programming

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

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

CSC324 Principles of Programming Languages

Functional Programming. Pure Functional Programming

CS 381: Programming Language Fundamentals

Lecture 4: Higher Order Functions

INTRODUCTION TO HASKELL

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Type-indexed functions in Generic Haskell

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

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

Chapter 15. Functional Programming Languages

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

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Graphical Untyped Lambda Calculus Interactive Interpreter

CS 457/557: Functional Languages

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

Introduction to Functional Programming

CSCE 314 Programming Languages

Functional Programming

CPS 506 Comparative Programming Languages. Programming Language Paradigm

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

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

CSE 3302 Programming Languages Lecture 8: Functional Programming

CSCE 314 Programming Languages

Haskell through HUGS THE BASICS

An introduction to functional programming. July 23, 2010

Functional Programming

Programming Languages

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

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

Applicative, traversable, foldable

Background Type Classes (1B) Young Won Lim 6/28/18

Syntax and Grammars 1 / 21

Functional Programming in C++

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

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Chapter 11 :: Functional Languages

Functional Programming - 2. Higher Order Functions

CSCI-GA Scripting Languages

The Algebra of Programming in Haskell

CS 440: Programming Languages and Translators, Spring 2019 Mon

CS 360: Programming Languages Lecture 12: More Haskell

Haskell An Introduction

CS 314 Principles of Programming Languages

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Exercise 1 ( = 24 points)

Structural polymorphism in Generic Haskell

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

FP Foundations, Scheme

Lambda Calculus see notes on Lambda Calculus

Exercise 1 (2+2+2 points)

Programming Languages Fall 2013

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

Scheme as implemented by Racket

Transcription:

Introduction to Functional Programming in Haskell 1 / 56

Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order functions Lazy evaluation How to functional program Functional programming workflow Data types Type-directed programming Haskell style Refactoring and reuse Refactoring Type classes Type inference 2 / 56

Outline Why learn functional programming? The essence of functional programming How to functional program Refactoring and reuse Type inference Why learn functional programming? 3 / 56

Why learn (pure) functional programming? 1. This course: strong correspondence of core concepts to PL theory abstract syntax can be represented by algebraic data types denotational semantics can be represented by functions 2. It will make you a better (imperative) programmer forces you to think recursively and compositionally forces you to minimize use of state... essential skills for solving big problems 3. It is the future! more scalable and parallelizable functional features have been added to most mainstream languages Why learn functional programming? 4 / 56

Quotes from pragmatists Programming is not about data. It s about transforming data... That s why I think functional programming is a natural successor to object-oriented programming. In OOP, we re constantly concerned about the state of our data. In functional programming, our focus is on the transformation of data. And transformation is where the value is added. Dave Thomas, The Pragmatic Programmer Why learn functional programming? 5 / 56

Quotes from pragmatists Without understanding functional programming, you can t invent MapReduce, the algorithm that makes Google so massively scalable. Joel Spolsky, Joel on Software Why learn functional programming? 6 / 56

Quotes from pragmatists So that s the big deal about functional languages; and it is one big fricking deal. There is a freight train barreling down the tracks towards us, with multi-core emblazoned on it; and you d better be ready by the time it gets here. Robert C. Martin, a.k.a. Uncle Bob Why learn functional programming? 7 / 56

Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order functions Lazy evaluation How to functional program Refactoring and reuse Type inference The essence of functional programming 8 / 56

What is a (pure) function? A function is pure if: it always returns the same output for the same inputs it doesn t do anything else no side effects In Haskell: whenever we say function we mean a pure function! The essence of functional programming 9 / 56

What are and aren t functions? Always functions: mathematical functions f (x) = x 2 + 2x + 3 encryption and compression algorithms Usually not functions: C, Python, JavaScript,... functions (procedures) Java, C#, Ruby,... methods Haskell only allows you to write (pure) functions! The essence of functional programming 10 / 56

Why procedures/methods aren t functions output depends on environment may perform arbitrary side effects The essence of functional programming 11 / 56

Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order functions Lazy evaluation How to functional program Refactoring and reuse Type inference The essence of functional programming 12 / 56

Getting into the Haskell mindset Haskell sum :: [Int] -> Int sum [] = 0 sum (x:xs) = x + sum xs In Haskell, = means is not change to! Java int sum(list<int> xs) { int s = 0; for (int x : xs) { s = s + x; } return s; } The essence of functional programming 13 / 56

Getting into the Haskell mindset Quicksort in Haskell qsort :: Ord a => [a] -> [a] qsort [] = [] qsort (x:xs) = qsort (filter (<= x) xs) ++ [x] ++ qsort (filter (> x) xs) Quicksort in C void qsort(int low, int high) { int i = low, j = high; int pivot = numbers[low + (high-low)/2]; while (i <= j) { while (numbers[i] < pivot) { i++; } while (numbers[j] > pivot) { j--; } if (i <= j) { swap(i, j); i++; j--; } } if (low < j) qsort(low, j); if (i < high) qsort(i, high); } void swap(int i, int j) { int temp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = temp; } The essence of functional programming 14 / 56

Referential transparency An expression can be replaced by its value without changing the overall program behavior a.k.a. referent length [1,2,3] + 4 3 + 4 what if length was a Java method? Corollary: an expression can be replaced by any expression with the same value without changing program behavior Supports equational reasoning The essence of functional programming 15 / 56

Equational reasoning Computation is just substitution! sum [2,3,4] sum :: [Int] -> Int sum [] = 0 sum (x:xs) = x + sum xs equations sum (2:(3:(4:[]))) 2 + sum (3:(4:[])) 2 + 3 + sum (4:[]) 2 + 3 + 4 + sum [] 2 + 3 + 4 + 0 9 The essence of functional programming 16 / 56

Describing computations Function definition: a list of equations that relate inputs to output matched top-to-bottom applied left-to-right Example: reversing a list imperative view: how do I rearrange the elements in the list? functional view: how is a list related to its reversal? reverse :: [a] -> [a] reverse [] = [] reverse (x:xs) = reverse xs ++ [x] The essence of functional programming 17 / 56

Exercise succ :: Int -> Int succ x = x + 1 double :: Int -> Int double x = x + x 1. Evaluate: double (succ (double 3)) A : 12 B : 14 C : 16 2. Prove, up to associativity of (+), using equational reasoning: double (succ x) = succ (succ (double x)) The essence of functional programming 18 / 56

Exercise foo :: Int -> Int -> Int foo 0 x = x + 1 foo x 0 = x + 2 foo x y = x + y 1. Evaluate: foo (foo 0 0) 0 A : 0 B : 2 C : 3 2. Evaluate: foo 0 (foo 0 0) A : 0 B : 2 C : 3 The essence of functional programming 19 / 56

Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order functions Lazy evaluation How to functional program Refactoring and reuse Type inference The essence of functional programming 20 / 56

First-order functions Examples cos :: Float -> Float even :: Int -> Bool length :: [a] -> Int The essence of functional programming 21 / 56

Higher-order functions Examples map :: (a -> b) -> [a] -> [b] filter :: (a -> Bool) -> [a] -> [a] (.) :: (b -> c) -> (a -> b) -> a -> c The essence of functional programming 22 / 56

Higher-order functions as control structures map: loop for doing something to each element in a list map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x : map f xs map f [2,3,4,5] = [f 2, f 3, f 4, f 5] map even [2,3,4,5] = [even 2, even 3, even 4, even 5] = [True,False,True,False] fold: loop for aggregating elements in a list foldr :: (a->b->b) -> b -> [a] -> b foldr f y [] = y foldr f y (x:xs) = f x (foldr f y xs) foldr f y [2,3,4] = f 2 (f 3 (f 4 y)) foldr (+) 0 [2,3,4] = (+) 2 ((+) 3 ((+) 4 0)) = 2 + (3 + (4 + 0)) = 9 The essence of functional programming 23 / 56

Function composition Can create new functions by composing existing functions apply the second function, then apply the first Function composition (.) :: (b -> c) -> (a -> b) -> a -> c f. g = \x -> f (g x) (f. g) x = f (g x) Types of existing functions not :: Bool -> Bool succ :: Int -> Int even :: Int -> Bool head :: [a] -> a tail :: [a] -> [a] Definitions of new functions plus2 = succ. succ odd = not. even second = head. tail drop2 = tail. tail The essence of functional programming 24 / 56

Currying / partial application In Haskell, functions that take multiple arguments are implicitly higher order plus :: Int -> Int -> Int Haskell Curry Curried plus 2 3 plus :: Int -> Int -> Int increment :: Int -> Int increment = plus 1 Uncurried plus (2,3) plus :: (Int,Int) -> Int a pair of ints The essence of functional programming 25 / 56

Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order functions Lazy evaluation How to functional program Refactoring and reuse Type inference The essence of functional programming 26 / 56

Lazy evaluation In Haskell, expressions are reduced: only when needed at most once Supports: infinite data structures separation of concerns nats :: [Int] nats = 1 : map (+1) nats fact :: Int -> Int fact n = product (take n nats) min3 :: [Int] -> [Int] min3 = take 3. sort What is the running time of this function? The essence of functional programming 27 / 56

Outline Why learn functional programming? The essence of functional programming How to functional program Functional programming workflow Data types Type-directed programming Haskell style Refactoring and reuse Type inference How to functional program 28 / 56

FP workflow (simple) obsessive compulsive refactoring disorder How to functional program 29 / 56

FP workflow (detailed) Norman Ramsey, On Teaching How to Design Programs, ICFP 14 How to functional program 30 / 56

Outline Why learn functional programming? The essence of functional programming How to functional program Functional programming workflow Data types Type-directed programming Haskell style Refactoring and reuse Type inference How to functional program 31 / 56

Algebraic data types Data type definition introduces new type of value enumerates ways to construct values of this type Definitions consists of... a type name a list of data constructors with argument types Some example data types data Bool = True False data Nat = Zero Succ Nat data Tree = Node Int Tree Tree Leaf Int Definition is inductive the arguments may recursively include the type being defined the constructors are the only way to build values of this type How to functional program 32 / 56

Anatomy of a data type definition type name data Expr = Lit Int Plus Expr Expr cases data constructor types of arguments Example: 2 + 3 + 4 Plus (Lit 2) (Plus (Lit 3) (Lit 4)) How to functional program 33 / 56

FP data types vs. OO classes Haskell data Tree = Node Int Tree Tree Leaf Java abstract class Tree {... } class Node extends Tree { int label; Tree left, right;... } class Leaf extends Tree {... } separation of type- and value-level set of cases closed set of operations open merger of type- and value-level set of cases open set of operations closed Extensibility of cases vs. operations = the expression problem How to functional program 34 / 56

Type parameters type parameter (Like generics in Java) data List a = Nil Cons a (List a) reference to type parameter recursive reference to type Specialized lists type IntList = List Int type CharList = List Char type RaggedMatrix a = List (List a) How to functional program 35 / 56

Outline Why learn functional programming? The essence of functional programming How to functional program Functional programming workflow Data types Type-directed programming Haskell style Refactoring and reuse Type inference How to functional program 36 / 56

Tools for defining functions Recursion and other functions sum :: [Int] -> Int sum xs = if null xs then 0 else head xs + sum (tail xs) (1) case analysis Pattern matching sum :: [Int] -> Int sum [] = 0 sum (x:xs) = x + sum xs (2) decomposition Higher-order functions sum :: [Int] -> Int sum = foldr (+) 0 no recursion or variables needed! How to functional program 37 / 56

What is type-directed programming? Use the type of a function to help write its body How to functional program 38 / 56

Type-directed programming Basic goal: transform values of argument types into result type If argument type is... atomic type (e.g. Int, Char) apply functions to it algebraic data type use pattern matching case analysis decompose into parts function type apply it to something If result type is... atomic type output of another function algebraic data type build with data constructor function type function composition or partial application build with lambda abstraction How to functional program 39 / 56

Outline Why learn functional programming? The essence of functional programming How to functional program Functional programming workflow Data types Type-directed programming Haskell style Refactoring and reuse Type inference How to functional program 40 / 56

Good Haskell style Why it matters: layout is significant! eliminate misconceptions we care about elegance Easy stuff: use spaces! (tabs cause layout errors) align patterns and guards See style guides on course web page How to functional program 41 / 56

Formatting function applications Function application: is just a space associates to the left binds most strongly f(x) f x (f x) y f x y (f x) + (g y) f x + g y Use parentheses only to override this behavior: f (g x) f (x + y) How to functional program 42 / 56

Outline Why learn functional programming? The essence of functional programming How to functional program Refactoring and reuse Refactoring Type classes Type inference Refactoring and reuse 43 / 56

Refactoring in the FP workflow Motivations: separate concerns promote reuse promote understandability gain insights obsessive compulsive refactoring disorder Refactoring and reuse 44 / 56

Refactoring relations Semantics-preserving laws prove with equational reasoning and/or induction Eta reduction: \x -> f x f Map map fusion: map f. map g map (f. g) Fold map fusion: foldr f b. map g foldr (f. g) b Algebra of computer programs John Backus, Can Programming be Liberated from the von Neumann Style?, ACM Turing Award Lecture, 1978 Refactoring and reuse 45 / 56

Strategy: systematic generalization commas :: [String] -> [String] commas [] = [] commas [x] = [x] commas (x:xs) = x : ", " : commas xs commas :: [String] -> [String] commas = intersperse ", " Introduce parameters for constants seps :: String -> [String] -> [String] seps _ [] = [] seps _ [x] = [x] seps s (x:xs) = x : s : seps s xs Broaden the types intersperse :: a -> [a] -> [a] intersperse _ [] = [] intersperse _ [x] = [x] intersperse s (x:xs) = x : s : intersperse s xs Refactoring and reuse 46 / 56

Strategy: abstract repeated templates abstract (v): extract and make reusable (as a function) showresult :: Maybe Float -> String showresult Nothing = "ERROR" showresult (Just v) = show v movecommand :: Maybe Dir -> Command movecommand Nothing = Stay movecommand (Just d) = Move d safeadd :: Int -> Maybe Int -> Int safeadd x Nothing = x safeadd x (Just y) = x + y Repeated structure: pattern match default value if Nothing apply function to contents if Just Refactoring and reuse 47 / 56

Strategy: abstract repeated templates Describe repeated structure in function maybe :: b -> (a -> b) -> Maybe a -> b maybe b _ Nothing = b maybe _ f (Just a) = f a Reuse in implementations showresult = maybe "ERROR" show movecommand = maybe Stay Move safeadd x = maybe x (x+) Refactoring and reuse 48 / 56

Refactoring data types data Expr = Var Name Add Expr Expr Sub Expr Expr Mul Expr Expr vars :: Expr -> [Name] vars (Var x) = [x] vars (Add l r) = vars l ++ vars r vars (Sub l r) = vars l ++ vars r vars (Mul l r) = vars l ++ vars r eval :: Env -> Expr -> Int eval m (Var x) = get x m eval m (Add l r) = eval m l + eval m r eval m (Sub l r) = eval m l - eval m r eval m (Mul l r) = eval m l * eval m r Refactoring and reuse 49 / 56

Refactoring data types Factor out shared structure data Expr = Var Name BinOp Op Expr Expr data Op = Add Sub Mul vars :: Expr -> [Name] vars (Var x) = [x] vars (BinOp _ l r) = vars l ++ vars r eval :: Env -> Expr -> Int eval m (Var x) = get x m eval m (BinOp o l r) = op o (eval m l) (eval m r) where op Add = (+) op Sub = (-) op Mul = (*) Refactoring and reuse 50 / 56

Outline Why learn functional programming? The essence of functional programming How to functional program Refactoring and reuse Refactoring Type classes Type inference Refactoring and reuse 51 / 56

What is a type class? 1. an interface that is supported by many different types 2. a set of types that have a common behavior class Eq a where (==) :: a -> a -> Bool class Show a where show :: a -> String types whose values can be compared for equality types whose values can be shown as strings class Num a where (+) :: a -> a -> a (*) :: a -> a -> a negate :: a -> a... types whose values can be manipulated like numbers Refactoring and reuse 52 / 56

Type constraints class Eq a where (==) :: a -> a -> Bool List elements can be of any type length :: [a] -> Int length [] = 0 length (_:xs) = 1 + length xs List elements must support equality! elem :: Eq a => a -> [a] -> Bool elem _ [] = False elem y (x:xs) = x == y elem y xs use method add type class constraint Refactoring and reuse 53 / 56

Outline Why learn functional programming? The essence of functional programming How to functional program Refactoring and reuse Type inference Type inference 54 / 56

Type inference How to perform type inference If a literal, data constructor, or named function: write down the type you re done! Otherwise: 1. identify the top-level application e 1 e 2 2. recursively infer their types e 1 : T 1 and e 2 : T 2 3. T 1 should be a function type T 1 = T arg T res 4. unify T arg =? T 2, yielding type variable assignment σ 5. return e 1 e 2 : σt res (T res with type variables substituted) If any of these steps fails, it is a type error! Example: map even Type inference 55 / 56

Exercises Given data Maybe a = Nothing Just a gt :: Int -> Int -> Bool not :: Bool -> Bool map :: (a -> b) -> [a] -> [b] even :: Int -> Bool (.) :: (b -> c) -> (a -> b) -> a -> c 1. Just 2. not even 3 3. not (even 3) 4. not. even 5. even. not 6. map (Just. even) Type inference 56 / 56