Functional Programming Concepts for Data Processing

Size: px
Start display at page:

Download "Functional Programming Concepts for Data Processing"

Transcription

1 Functional Programming Concepts for Data Processing Chris Lindholm UCAR SEA / 70

2 About Me I work at CU LASP I work primarily with Scala I teach Haskell at work Goal: leverage FP to improve scientific software 2 / 70

3 The Plan Convince you Functional Programming is worth learning about Introduce Functional Programming Refactor some code to be more Functional 3 / 70

4 Why Functional Programming? 4 / 70

5 Why Functional Programming? FP helps us write good software 5 / 70

6 Side E ects 6 / 70

7 Side E ects Side effects are difficult to reason about 7 / 70

8 Side E ects Side effects are difficult to reason about Side effects are difficult to test 8 / 70

9 Side E ects Side effects are difficult to reason about Side effects are difficult to test Side effects are difficult to scale 9 / 70

10 Side E ects Side effects are difficult to reason about Side effects are difficult to test Side effects are difficult to scale Side effects are unnecessary 10 / 70

11 Why Functional Programming? FP helps us write good software FP is naturally suited for expressing data-oriented workflows 11 / 70

12 Data Processing is Function-y Functions are math's way of processing data. 12 / 70

13 Data Processing is Function-y Functions are math's way of processing data. We can build pipelines with function composition and application. 13 / 70

14 Data Processing is Function-y Functions are math's way of processing data. We can build pipelines with function composition and application. a :: A d :: D -- D -- \ f :: A -> B g :: B -> C E / h :: C -> D -> E -- A -> B -> C 14 / 70

15 Data Processing is Function-y Functions are math's way of processing data. We can build pipelines with function composition and application. a :: A d :: D -- D -- \ f :: A -> B g :: B -> C E / h :: C -> D -> E -- A -> B -> C (g. f) :: A -> C -- (.) :: (b -> c) -> (a -> b) -> (a -> c) (h. g. f) :: A -> D -> E (h. g. f) a d :: E 15 / 70

16 Why Functional Programming? FP helps us write good software FP is naturally suited for expressing data-oriented workflows Learning FP makes you a better programmer 16 / 70

17 Introduction to Functional Programming 17 / 70

18 Imperative Programming Express computations using statements that perform side effects. 18 / 70

19 Imperative Programming Express computations using statements that perform side effects. // Declare a variable to mutate later. int var; // Change control flow depending on p. if (p) { var = 0; // If p is true, execute a statement that mutates var. } else { var = 1; // If p is false, execute a statement that mutates var. } 19 / 70

20 Imperative Programming Express computations using statements that perform side effects. // Declare a variable to mutate later. int var; // Change control flow depending on p. if (p) { var = 0; // If p is true, execute a statement that mutates var. } else { var = 1; // If p is false, execute a statement that mutates var. } Functional Programming Express computations using expressions that evaluate to values. 20 / 70

21 Imperative Programming Express computations using statements that perform side effects. // Declare a variable to mutate later. int var; // Change control flow depending on p. if (p) { var = 0; // If p is true, execute a statement that mutates var. } else { var = 1; // If p is false, execute a statement that mutates var. } Functional Programming Express computations using expressions that evaluate to values. -- Declare var to be 0 if p is true, 1 otherwise. var = if p then 0 else 1 21 / 70

22 Lambda Calculus The theory of computation underlying functional programming. 22 / 70

23 Lambda Calculus The theory of computation underlying functional programming. Lambda calculus has only Variables Functions Expressions but can express anything that is computable. 23 / 70

24 (λpq. pqp) (λxy. x) (λab. b) AND TRUE FALSE T RUE F ALSE = F ALSE (λpq. pqp)(λxy. x)(λab. b) β λab. b 24 / 70

25 (λpq. pqp)(λxy. x)(λab. b) β (λ[p := (λxy. x)]q. pqp)(λab. b) β (λq. (λxy. x)q(λxy. x))(λab. b) β λ[q := (λab. b)]. (λxy. x)q(λxy. x) β (λxy. x)(λab. b)(λxy. x) β (λ[x := (λab. b)]y. x)(λxy. x) β (λy. (λab. b))(λxy. x) β (λ[y := (λxy. x)]. (λab. b)) β λab. b 25 / 70

26 Pure Functions Pure functions take inputs, produce outputs using only those inputs, and do nothing else. 26 / 70

27 Pure Functions Pure functions take inputs, produce outputs using only those inputs, and do nothing else. def pure(x): return x / 70

28 Pure Functions Pure functions take inputs, produce outputs using only those inputs, and do nothing else. def pure(x): return x + 1 def maybe_pure(x): return f(x) / 70

29 First-class Functions First-class functions are values rather than syntactic constructs. 29 / 70

30 First-class Functions First-class functions are values rather than syntactic constructs. f = lambda x: x / 70

31 First-class Functions First-class functions are values rather than syntactic constructs. f = lambda x: x + 1 (+) :: Int -> Int -> Int addone :: Int -> Int addone = (+) 1 31 / 70

32 Higher-order Functions Higher-order functions operate on other functions. 32 / 70

33 Higher-order Functions Higher-order functions operate on other functions. (.) :: (b -> c) -> (a -> b) -> (a -> c) 33 / 70

34 Referential Transparency A referentially transparent expression is one that can be replaced with the result of its evaluation and not change the meaning of the program. 34 / 70

35 Referential Transparency A referentially transparent expression is one that can be replaced with the result of its evaluation and not change the meaning of the program. Expressions that are not referentially transparent have side effects. 35 / 70

36 Are these the same? def program1(): # expr is the expression we're testing expr = 1 return (expr, expr) def program2(): return (1, 1) 36 / 70

37 Are these the same? def program1(): # expr is the expression we're testing expr = 1 return (expr, expr) def program2(): return (1, 1) >>> program1() (1,1) >>> program2() (1,1) 37 / 70

38 Are these the same? def program1(): expr = datetime.now() return (expr, expr) def program2(): return (datetime.now(), datetime.now()) 38 / 70

39 Are these the same? def program1(): expr = datetime.now() return (expr, expr) def program2(): return (datetime.now(), datetime.now()) >>> program1() (datetime.datetime(2018, 4, 1, 17, 16, 17, ), datetime.datetime(2018, 4, 1, 17, 16, 17, )) >>> program2() (datetime.datetime(2018, 4, 1, 17, 16, 19, ), datetime.datetime(2018, 4, 1, 17, 16, 19, )) 39 / 70

40 Are these the same? def my_length(x): res = 0 for i in range(0, len(x)): res += 1 return res def program1(): expr = my_length([1,2,3]) return (expr, expr) def program2(): return (my_length([1,2,3]), my_length([1,2,3])) 40 / 70

41 Are these the same? def my_length(x): res = 0 for i in range(0, len(x)): res += 1 return res def program1(): expr = my_length([1,2,3]) return (expr, expr) def program2(): return (my_length([1,2,3]), my_length([1,2,3])) >>> program1() (3, 3) >>> program2() (3, 3) 41 / 70

42 Equational Reasoning Reasoning about code through substitution rather than execution. 42 / 70

43 Equational Reasoning Reasoning about code through substitution rather than execution. -- Apply a function to every element of a list. map :: (a -> b) -> [a] -> [b] map _ [] = [] map f x:xs = f x : map f xs 43 / 70

44 Equational Reasoning Reasoning about code through substitution rather than execution. -- Apply a function to every element of a list. map :: (a -> b) -> [a] -> [b] map _ [] = [] map f x:xs = f x : map f xs map (+ 1) [1, 2, 3] 44 / 70

45 Equational Reasoning Reasoning about code through substitution rather than execution. -- Apply a function to every element of a list. map :: (a -> b) -> [a] -> [b] map _ [] = [] map f x:xs = f x : map f xs map (+ 1) [1, 2, 3] map (+ 1) [1, 2, 3] = (1 + 1) : map (+ 1) [2, 3] = (1 + 1) : (2 + 1) : map (+ 1) [3] = (1 + 1) : (2 + 1) : (3 + 1) : map (+ 1) [] = (1 + 1) : (2 + 1) : (3 + 1) : [] = 2 : 3 : 4 : [] = [2, 3, 4] 45 / 70

46 Recap 46 / 70

47 Recap Functional programs are expressions. 47 / 70

48 Recap Functional programs are expressions. We run them by evaluating the expressions. 48 / 70

49 Recap Functional programs are expressions. We run them by evaluating the expressions. We try to keep functions and expressions free from side effects. 49 / 70

50 Recap Functional programs are expressions. We run them by evaluating the expressions. We try to keep functions and expressions free from side effects. We manipulate functions like any other values. 50 / 70

51 Recap Functional programs are expressions. We run them by evaluating the expressions. We try to keep functions and expressions free from side effects. We manipulate functions like any other values. These things enable us to apply mathematical reasoning to our programs. 51 / 70

52 Recap Functional programs are expressions. We run them by evaluating the expressions. We try to keep functions and expressions free from side effects. We manipulate functions like any other values. These things enable us to apply mathematical reasoning to our programs. There's a lot more to functional programming. 52 / 70

53 Refactoring to be More Functional 53 / 70

54 Refactoring to be More Functional def process(): # Read input data. data = read_data("input") # Run our processing algorithm. data = [f(x) for x in data] data = [g(x) for x in data] # Write our output. write_data(data, "output") 54 / 70

55 Refactoring to be More Functional def process(): # Read input data. data = read_data("input") # Run our processing algorithm. data = [f(x) for x in data] data = [g(x) for x in data] # Write our output. write_data(data, "output") Testing this is hard. 55 / 70

56 Refactoring to be More Functional def process(input_file, output_file): # Read input data. data = read_data(input_file) # Run our processing algorithm. data = [f(x) for x in data] data = [g(x) for x in data] # Write our output. write_data(data, output_file) Make testing easier by taking arguments. 56 / 70

57 Refactoring to be More Functional def process(input_file, output_file): # Read input data. data = read_data(input_file) # Run our processing algorithm. data = [g(x) for x in data] data = [f(x) for x in data] # Write our output. write_data(data, output_file) 57 / 70

58 Refactoring to be More Functional def process(input_file, output_file): # Read input data. data = read_data(input_file) # Run our processing algorithm. output_f = [f(x) for x in data] output_g = [g(x) for x in output_f] # Write our output. write_data(output_g, output_file) Keep data immutable. 58 / 70

59 Refactoring to be More Functional def process(input_file, output_file): # Read input data. data = read_data(input_file) # Run our processing algorithm. output_f = process_f(data) output_g = process_g(output_f) # Write our output. write_data(output_g, output_file) def process_f(data): return [f(x) for x in data] def process_g(data): return [g(x) for x in data] Separate pure algorithms from side-effecting infrastructure. 59 / 70

60 def process(): # Read input data. data = read_data("input") # Run our processing algorithm. data = [f(x) for x in data] data = [g(x) for x in data] # Write our output. write_data(data, "output") def process(input_file, output_file): # Read input data. data = read_data(input_file) # Run our processing algorithm. output_f = process_f(data) output_g = process_g(output_f) # Write our output. write_data(output_g, output_file) def process_f(data): return [f(x) for x in data] def process_g(data): return [g(x) for x in data] 60 / 70

61 def process(): # Read input data. data = read_data("input") # Run our processing algorithm. data = [f(x) for x in data] data = [g(x) for x in data] # Write our output. write_data(data, "output") def process(input_file, output_file): # Read input data. data = read_data(input_file) # Run our processing algorithm. output_f = process_f(data) output_g = process_g(output_f) # Write our output. write_data(output_g, output_file) def process_f(data): return [f(x) for x in data] def process_g(data): return [g(x) for x in data] 61 / 70

62 def process(): # Read input data. data = read_data("input") # Run our processing algorithm. data = [f(x) for x in data] data = [g(x) for x in data] # Write our output. write_data(data, "output") def process(input_file, output_file): # Read input data. data = read_data(input_file) # Run our processing algorithm. output_f = process_f(data) output_g = process_g(output_f) # Write our output. write_data(output_g, output_file) def process_f(data): return [f(x) for x in data] def process_g(data): return [g(x) for x in data] 62 / 70

63 Refactoring to be More Functional def process(input_file, output_file): # Read input data. data = read_data(input_file) # Run our processing algorithm. output_f = map(f, data) output_g = map(g, output_f) # Write our output. write_data(output_g, output_file) #def process_f(data): # return [f(x) for x in data] #def process_g(data): # return [g(x) for x in data] Build larger programs from smaller ones. 63 / 70

64 Refactoring to be More Functional def process(input_file, output_file): # Read input data. data = read_data(input_file) # Run our processing algorithm. output = map(compose(g, f), data) # Write our output. write_data(output, output_file) def compose(g, f): return lambda x: g(f(x)) Steal from math. -- Functor composition law map g. map f = map (g. f) 64 / 70

65 Refactoring Example in Haskell main :: IO () main = let inputfile = "input" outputfile = "output" in pipeline inputfile outputfile pipeline :: FilePath -> FilePath -> IO () pipeline inputfile outputfile = readdata inputfile <&> map (g. f) >>= writedata outputfile -- (<&>) :: Functor f => f a -> (a -> b) -> f b -- (>>=) :: Monad m => m a -> (a -> m b) -> m b 65 / 70

66 Guidelines for Writing Functional Code 66 / 70

67 Guidelines for Writing Functional Code Write functions that take inputs and return outputs 67 / 70

68 Guidelines for Writing Functional Code Write functions that take inputs and return outputs Keep data immutable 68 / 70

69 Guidelines for Writing Functional Code Write functions that take inputs and return outputs Keep data immutable Reach for side effects last 69 / 70

70 Guidelines for Writing Functional Code Write functions that take inputs and return outputs Keep data immutable Reach for side effects last Limit side effects to the outer layers of your program 70 / 70

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

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm. Quick announcement Midterm date is Wednesday Oct 24, 11-12pm. The lambda calculus = ID (λ ID. ) ( ) The lambda calculus (Racket) = ID (lambda (ID) ) ( )

More information

JVM ByteCode Interpreter

JVM ByteCode Interpreter JVM ByteCode Interpreter written in Haskell (In under 1000 Lines of Code) By Louis Jenkins Presentation Schedule ( 15 Minutes) Discuss and Run the Virtual Machine first

More information

Imperative languages

Imperative languages Imperative languages Von Neumann model: store with addressable locations machine code: effect achieved by changing contents of store locations instructions executed in sequence, flow of control altered

More information

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

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona 1/37 CSc 372 Comparative Programming Languages 2 : Functional Programming Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/37 Programming Paradigms

More information

Introduction to Functional Programming in Haskell 1 / 56

Introduction to Functional Programming in Haskell 1 / 56 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

More information

More Lambda Calculus and Intro to Type Systems

More Lambda Calculus and Intro to Type Systems More Lambda Calculus and Intro to Type Systems Plan Heavy Class Participation Thus, wake up! Lambda Calculus How is it related to real life? Encodings Fixed points Type Systems Overview Static, Dyamic

More information

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

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 18 Thursday, March 29, 2018 In abstract algebra, algebraic structures are defined by a set of elements and operations

More information

Introduction to Functional Programming

Introduction to Functional Programming A Level Computer Science Introduction to Functional Programming William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London Aims and Claims Flavour of Functional

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ August 17, 2007 Lambda Calculus and Type

More information

More Lambda Calculus and Intro to Type Systems

More Lambda Calculus and Intro to Type Systems #1 More Lambda Calculus and Intro to Type Systems #2 Plan Heavy Class Participation Thus, wake up! (not actually kidding) Lambda Calculus How is it related to real life? Encodings Fixed points Type Systems

More information

Functional Programming

Functional Programming Functional Programming COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Original version by Prof. Simon Parsons Functional vs. Imperative Imperative programming

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Lambda Calculus CMSC 330 Summer 2017 1 100 years ago Albert Einstein proposed special theory of relativity in 1905 In the paper On the Electrodynamics of

More information

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus CMSC 330: Organization of Programming Languages Lambda Calculus 1 100 years ago Albert Einstein proposed special theory of relativity in 1905 In the paper On the Electrodynamics of Moving Bodies 2 Prioritätsstreit,

More information

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems Homework Lecture 7: Parsers & Lambda Calculus CSC 131 Spring, 2019 Kim Bruce First line: - module Hmwk3 where - Next line should be name as comment - Name of program file should be Hmwk3.hs Problems How

More information

Programming with Math and Logic

Programming with Math and Logic .. Programming with Math and Logic an invitation to functional programming Ed Morehouse Wesleyan University The Plan why fp? terms types interfaces The What and Why of Functional Programming Computing

More information

CSCE 314 Programming Languages Functors, Applicatives, and Monads

CSCE 314 Programming Languages Functors, Applicatives, and Monads CSCE 314 Programming Languages Functors, Applicatives, and Monads Dr. Hyunyoung Lee 1 Motivation Generic Functions A common programming pattern can be abstracted out as a definition. For example: inc ::

More information

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242 Spring 2013 Foundations Yu Zhang Acknowledgement: modified from Stanford CS242 https://courseware.stanford.edu/pg/courses/317431/ Course web site: http://staff.ustc.edu.cn/~yuzhang/fpl Reading Concepts

More information

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

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming language Functional programming language : a program

More information

Denotational Semantics. Domain Theory

Denotational Semantics. Domain Theory Denotational Semantics and Domain Theory 1 / 51 Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning

More information

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus CMSC 330: Organization of Programming Languages Lambda Calculus 1 Turing Completeness Turing machines are the most powerful description of computation possible They define the Turing-computable functions

More information

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

Haskell Overview II (2A) Young Won Lim 8/9/16 (2A) Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

An introduction introduction to functional functional programming programming using usin Haskell

An introduction introduction to functional functional programming programming using usin Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dkau Haskell The most popular p purely functional, lazy programming g language Functional programming language : a program

More information

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations Outline FP Foundations, Scheme In Text: Chapter 15 Mathematical foundations Functional programming λ-calculus LISP Scheme 2 Imperative Languages We have been discussing imperative languages C/C++, Java,

More information

Overview. Elements of Programming Languages. Evaluation order. Evaluation order

Overview. Elements of Programming Languages. Evaluation order. Evaluation order Overview Elements of Programming Languages Lecture 15: Evaluation strategies and laziness James Cheney University of Edinburgh November 18, 216 Final few lectures: cross-cutting language design issues

More information

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

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules Haskell: From Basic to Advanced Part 2 Type Classes, Laziness, IO, Modules Qualified types In the types schemes we have seen, the type variables were universally quantified, e.g. ++ :: [a] -> [a] -> [a]

More information

Functional Programming

Functional Programming Functional Programming Björn B. Brandenburg The University of North Carolina at Chapel Hill Based in part on slides and notes by S. Olivier, A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts. Brief

More information

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

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Tuesday, February 19, 2013 The lambda calculus (or λ-calculus) was introduced by Alonzo Church and Stephen Cole Kleene in

More information

Elixir, functional programming and the Lambda calculus.

Elixir, functional programming and the Lambda calculus. Elixir, functional programming and the Lambda calculus. Programming II - Elixir Version Johan Montelius Spring Term 2018 Introduction In this tutorial you re going to explore lambda calculus and how it

More information

Introductory Example

Introductory Example CSc 520 Principles of Programming Languages 21: Lambda Calculus Introduction Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg

More information

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml CMSC 330: Organization of Programming Languages Functional Programming with OCaml 1 What is a functional language? A functional language: defines computations as mathematical functions discourages use

More information

Introduction to Functional Programming and Haskell. Aden Seaman

Introduction to Functional Programming and Haskell. Aden Seaman Introduction to Functional Programming and Haskell Aden Seaman Functional Programming Functional Programming First Class Functions Expressions (No Assignment) (Ideally) No Side Effects Different Approach

More information

Exercise 1 ( = 24 points)

Exercise 1 ( = 24 points) 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 =

More information

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

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc. CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:

More information

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

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete Dr A Sahu Dept of Computer Science & Engineering IIT Guwahati Why study? Lambda calculus Syntax Evaluation Relationship to programming languages Church Rosser theorem Completeness of Lambda Calculus: Turing

More information

A Brief Introduction to Scheme (I)

A Brief Introduction to Scheme (I) A Brief Introduction to Scheme (I) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Scheme Scheme I p.1/44 Scheme: Feature Set A

More information

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

Background Type Classes (1B) Young Won Lim 6/28/18 Background Type Classes (1B) Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2

More information

Refactoring to Functional. Hadi Hariri

Refactoring to Functional. Hadi Hariri Refactoring to Functional Hadi Hariri Functional Programming In computer science, functional programming is a programming paradigm, a style of building the structure and elements of computer programs,

More information

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

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011 CS152: Programming Languages Lecture 11 STLC Extensions and Related Topics Dan Grossman Spring 2011 Review e ::= λx. e x e e c v ::= λx. e c τ ::= int τ τ Γ ::= Γ, x : τ (λx. e) v e[v/x] e 1 e 1 e 1 e

More information

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

More information

Functional Languages. Hwansoo Han

Functional Languages. Hwansoo Han Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Haskell 101 Dr. Hyunyoung Lee 1 Contents 1. Historical Background of Haskell 2. Lazy, Pure, and Functional Language 3. Using ghc and ghci 4. Functions 5. Haskell Scripts

More information

functional programming in Python, part 2

functional programming in Python, part 2 Programming Languages Week 2 functional programming in Python, part 2 College of Information Science and Engineering Ritsumeikan University review of part 1 eliminating assignment makes programs easier

More information

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017 Introduction to Concepts in Functional Programming CS16: Introduction to Data Structures & Algorithms Spring 2017 Outline Functions State Functions as building blocks Higher order functions Map Reduce

More information

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell Ori Bar El Maxim Finkel 01/11/17 1 History Haskell is a lazy, committee designed, pure functional programming language that

More information

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

Haskell Overview II (2A) Young Won Lim 8/23/16 (2A) Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

Exercise 1 ( = 18 points)

Exercise 1 ( = 18 points) 1 Exercise 1 (4 + 5 + 4 + 5 = 18 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

More information

CS457/557 Functional Languages

CS457/557 Functional Languages CS457/557 Functional Languages Spring 2018 Lecture 1: Course Introduction Andrew Tolmach Portland State University (with thanks to Mark P. Jones) 1 Goals of this course Introduce the beautiful ideas of

More information

Side Effects (3A) Young Won Lim 1/13/18

Side Effects (3A) Young Won Lim 1/13/18 Side Effects (3A) Copyright (c) 2016-2018 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later

More information

COMP3151/9151 Foundations of Concurrency Lecture 8

COMP3151/9151 Foundations of Concurrency Lecture 8 1 COMP3151/9151 Foundations of Concurrency Lecture 8 Liam O Connor CSE, UNSW (and data61) 8 Sept 2017 2 Shared Data Consider the Readers and Writers problem from Lecture 6: Problem We have a large data

More information

Types And Categories. Anat Gilboa

Types And Categories. Anat Gilboa Types And Categories Anat Gilboa (@anat_gilboa) 1 Background 2 The Plan 3 The Plan https://github.com/hmemcpy/milewski-ctfp-pdf 4 The Plan 5 https://chrisdone.com/posts/monads-are-burritos 6 What to expect

More information

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

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur Functional Programming and λ Calculus Amey Karkare Dept of CSE, IIT Kanpur 0 Software Development Challenges Growing size and complexity of modern computer programs Complicated architectures Massively

More information

Fundamentals and lambda calculus

Fundamentals and lambda calculus Fundamentals and lambda calculus Again: JavaScript functions JavaScript functions are first-class Syntax is a bit ugly/terse when you want to use functions as values; recall block scoping: (function ()

More information

Fall Lecture 3 September 4. Stephen Brookes

Fall Lecture 3 September 4. Stephen Brookes 15-150 Fall 2018 Lecture 3 September 4 Stephen Brookes Today A brief remark about equality types Using patterns Specifying what a function does equality in ML e1 = e2 Only for expressions whose type is

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques () Lecture 13 February 12, 2018 Mutable State & Abstract Stack Machine Chapters 14 &15 Homework 4 Announcements due on February 20. Out this morning Midterm results

More information

Organization of Programming Languages CS3200/5200N. Lecture 11

Organization of Programming Languages CS3200/5200N. Lecture 11 Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages

More information

Writing code that I'm not smart enough to write. A funny thing happened at Lambda Jam

Writing code that I'm not smart enough to write. A funny thing happened at Lambda Jam Writing code that I'm not smart enough to write A funny thing happened at Lambda Jam Background "Let s make a lambda calculator" Rúnar Bjarnason Task: write an interpreter for the lambda calculus Lambda

More information

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

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics 1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Basics 2 Contents 1. Jump into Haskell: Using ghc and ghci (more detail) 2. Historical Background of Haskell 3. Lazy, Pure, and Functional

More information

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

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy 06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic

More information

Lecture #3: Lambda Calculus. Last modified: Sat Mar 25 04:05: CS198: Extra Lecture #3 1

Lecture #3: Lambda Calculus. Last modified: Sat Mar 25 04:05: CS198: Extra Lecture #3 1 Lecture #3: Lambda Calculus Last modified: Sat Mar 25 04:05:39 2017 CS198: Extra Lecture #3 1 Simplifying Python Python is full of features. Most are there to make programming concise and clear. Some are

More information

CS 242. Fundamentals. Reading: See last slide

CS 242. Fundamentals. Reading: See last slide CS 242 Fundamentals Reading: See last slide Syntax and Semantics of Programs Syntax The symbols used to write a program Semantics The actions that occur when a program is executed Programming language

More information

Functional Programming and Haskell

Functional Programming and Haskell Functional Programming and Haskell Tim Dawborn University of Sydney, Australia School of Information Technologies Tim Dawborn Functional Programming and Haskell 1/22 What are Programming Paradigms? A programming

More information

More Lambda Calculus and Intro to Type Systems

More Lambda Calculus and Intro to Type Systems More Lambda Calculus and Intro to Type Systems #1 One Slide Summary The lambda calculus is a model of computation or a programming language that is as expressive as a Turing machine. The lambda calculus

More information

Functional Programming in C++

Functional Programming in C++ Functional Programming in C++ An Overview Programming in a functional style Why functional programming? What is functional programming? Characteristics of functional programming What's missing? Functional

More information

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

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides) Fundamentals and lambda calculus Deian Stefan (adopted from my & Edward Yang s CSE242 slides) Logistics Assignments: Programming assignment 1 is out Homework 1 will be released tomorrow night Podcasting:

More information

3. Functional Programming. Oscar Nierstrasz

3. Functional Programming. Oscar Nierstrasz 3. Functional Programming Oscar Nierstrasz Roadmap > Functional vs. Imperative Programming > Pattern Matching > Referential Transparency > Lazy Evaluation > Recursion > Higher Order and Curried Functions

More information

The story so far. Elements of Programming Languages. While-programs. Mutable vs. immutable

The story so far. Elements of Programming Languages. While-programs. Mutable vs. immutable The story so far Elements of Programming Languages Lecture 12: Imperative programming James Cheney University of Edinburgh November 4, 2016 So far we ve mostly considered pure computations. Once a variable

More information

Classical Themes of Computer Science

Classical Themes of Computer Science Functional Programming (Part 1/2) Bernhard K. Aichernig Institute for Software Technology Graz University of Technology Austria Winter Term 2017/18 Version: November 22, 2017 1/49 Agenda I Functional Programming?

More information

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

Haskell Overview II (2A) Young Won Lim 9/26/16 (2A) Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

Introduction to the Lambda Calculus. Chris Lomont

Introduction to the Lambda Calculus. Chris Lomont Introduction to the Lambda Calculus Chris Lomont 2010 2011 2012 www.lomont.org Leibniz (1646-1716) Create a universal language in which all possible problems can be stated Find a decision method to solve

More information

The Haskell HOP: Higher-order Programming

The Haskell HOP: Higher-order Programming The Haskell HOP: Higher-order Programming COS 441 Slides 6 Slide content credits: Ranjit Jhala, UCSD Agenda Haskell so far: First-order functions This time: Higher-order functions: Functions as data, arguments

More information

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010 Pure (Untyped) λ-calculus Andrey Kruglyak, 2010 1 Pure (untyped) λ-calculus An example of a simple formal language Invented by Alonzo Church (1936) Used as a core language (a calculus capturing the essential

More information

Lecture 5: Lazy Evaluation and Infinite Data Structures

Lecture 5: Lazy Evaluation and Infinite Data Structures Lecture 5: Lazy Evaluation and Infinite Data Structures Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense October 3, 2017 How does Haskell evaluate a

More information

CS 11 Haskell track: lecture 1

CS 11 Haskell track: lecture 1 CS 11 Haskell track: lecture 1 This week: Introduction/motivation/pep talk Basics of Haskell Prerequisite Knowledge of basic functional programming e.g. Scheme, Ocaml, Erlang CS 1, CS 4 "permission of

More information

SOFTWARE ARCHITECTURE 6. LISP

SOFTWARE ARCHITECTURE 6. LISP 1 SOFTWARE ARCHITECTURE 6. LISP Tatsuya Hagino hagino@sfc.keio.ac.jp slides URL https://vu5.sfc.keio.ac.jp/sa/ 2 Compiler vs Interpreter Compiler Translate programs into machine languages Compilers are

More information

Introduction. chapter Functions

Introduction. chapter Functions chapter 1 Introduction In this chapter we set the stage for the rest of the book. We start by reviewing the notion of a function, then introduce the concept of functional programming, summarise the main

More information

Higher Order Functions in Haskell

Higher Order Functions in Haskell Higher Order Functions in Haskell Evan Misshula 2018-09-10 Outline Curried Functions Curried comparison Example partial application partial application of a string function Returned functions ZipWith flip

More information

Introduction 2 Lisp Part III

Introduction 2 Lisp Part III Introduction 2 Lisp Part III Andreas Wichert LEIC-T (Página da cadeira: Fenix) (I) Home Work (II) Data Types and Generic Programming (III) Lisp is Science (IV) Lisp Macros (V) More about Common Lisp an

More information

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

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson Introduction to Functional Programming in Racket CS 550 Programming Languages Jeremy Johnson 1 Objective To introduce functional programming in racket Programs are functions and their semantics involve

More information

Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators

Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Lambda Calculus alpha-renaming, beta reduction, applicative and normal evaluation orders, Church-Rosser theorem, combinators Carlos Varela Rennselaer Polytechnic Institute February 11, 2010 C. Varela 1

More information

Monads. COS 441 Slides 16

Monads. COS 441 Slides 16 Monads COS 441 Slides 16 Last time: Agenda We looked at implementation strategies for languages with errors, with printing and with storage We introduced the concept of a monad, which involves 3 things:

More information

Control Structures. Christopher Simpkins CS 3693, Fall Chris Simpkins (Georgia Tech) CS 3693 Scala / 1

Control Structures. Christopher Simpkins CS 3693, Fall Chris Simpkins (Georgia Tech) CS 3693 Scala / 1 Control Structures Christopher Simpkins csimpkin@spsu.edu CS 3693, Fall 2011 Chris Simpkins (Georgia Tech) CS 3693 Scala 2011-08-31 1 / 1 Control Structures Scala has only four built-in control structures:

More information

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University Functional Programming in Java CSE 219, Stony Brook University What is functional programming? There is no single precise definition of functional programming (FP) We should think of it as a programming

More information

.consulting.solutions.partnership. Clojure by Example. A practical introduction to Clojure on the JVM

.consulting.solutions.partnership. Clojure by Example. A practical introduction to Clojure on the JVM .consulting.solutions.partnership Clojure by Example A practical introduction to Clojure on the JVM Clojure By Example 1 Functional Progamming Concepts 3 2 Clojure Basics 4 3 Clojure Examples 5 4 References

More information

College Functors, Applicatives

College Functors, Applicatives College 2016-2017 Functors, Applicatives Wouter Swierstra with a bit of Jurriaan Hage Utrecht University Contents So far, we have seen monads define a common abstraction over many programming patterns.

More information

Applicative, traversable, foldable

Applicative, traversable, foldable Applicative, traversable, foldable Advanced functional programming - Lecture 3 Wouter Swierstra 1 Beyond the monad So far, we have seen how monads define a common abstraction over many programming patterns.

More information

Lecture 4: Higher Order Functions

Lecture 4: Higher Order Functions Lecture 4: Higher Order Functions Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 26, 2017 HIGHER ORDER FUNCTIONS The order of a function

More information

Functional Programming Principles in Scala. Martin Odersky

Functional Programming Principles in Scala. Martin Odersky Functional Programming Principles in Scala Martin Odersky Programming Paradigms Paradigm: In science, a paradigm describes distinct concepts or thought patterns in some scientific discipline. Main programming

More information

Monads in Haskell. Nathanael Schilling. December 12, 2014

Monads in Haskell. Nathanael Schilling. December 12, 2014 Monads in Haskell Nathanael Schilling December 12, 2014 Abstract In Haskell, monads provide a mechanism for mapping functions of the type a -> m b to those of the type m a -> m b. This mapping is dependent

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ October 13, 2004 Lambda Calculus and Type

More information

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009 CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009 Lecture notes for CS 6110 (Spring 09) taught by Andrew Myers at Cornell; edited by Amal Ahmed, Fall 09. 1 Static vs. dynamic scoping The scope of a variable

More information

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

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped) 1 LAMBDA CALCULUS (untyped) 2 The Untyped Lambda Calculus (λ) Designed by Alonzo Church (1930s) Turing Complete (Turing was his doctoral student!) Models functions, always as 1-input Definition: terms,

More information

CITS3211 FUNCTIONAL PROGRAMMING

CITS3211 FUNCTIONAL PROGRAMMING CITS3211 FUNCTIONAL PROGRAMMING 9. The λ calculus Summary: This lecture introduces the λ calculus. The λ calculus is the theoretical model underlying the semantics and implementation of functional programming

More information

CSCI.4430/6969 Programming Languages Lecture Notes

CSCI.4430/6969 Programming Languages Lecture Notes CSCI.4430/6969 Programming Languages Lecture Notes August 28, 2006 1 Brief History of Programming Languages Ada Augusta, the Countess of Lovelace, the daughter of the poet Lord Byron, is attributed as

More information

CSE : Python Programming

CSE : Python Programming CSE 399-004: Python Programming Lecture 10: Functional programming, Memoization March 26, 2007 http://www.seas.upenn.edu/~cse39904/ Announcements Should have received email about meeting times Length:

More information

CMSC330. Objects, Functional Programming, and lambda calculus

CMSC330. Objects, Functional Programming, and lambda calculus CMSC330 Objects, Functional Programming, and lambda calculus 1 OOP vs. FP Object-oriented programming (OOP) Computation as interactions between objects Objects encapsulate mutable data (state) Accessed

More information

Monads and all that I. Monads. John Hughes Chalmers University/Quviq AB

Monads and all that I. Monads. John Hughes Chalmers University/Quviq AB Monads and all that I. Monads John Hughes Chalmers University/Quviq AB Binary Trees in Haskell data Tree a = Leaf a Branch (Tree a) (Tree a) deriving (Eq,Show) Cf Coq: Inductive tree (A:Set) : Set := leaf

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

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

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II) Processadors de Llenguatge II Functional Paradigm Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Paradigm Shift Imperative Paradigm State Machine

More information

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

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics National Institute of Informatics May 31, June 7, June 14, 2010 All Right Reserved. Outline I 1 Parser Type 2 Monad Parser Monad 3 Derived Primitives 4 5 6 Outline Parser Type 1 Parser Type 2 3 4 5 6 What

More information

Applicative, traversable, foldable

Applicative, traversable, foldable Applicative, traversable, foldable Advanced functional programming - Lecture 4 Wouter Swierstra and Alejandro Serrano 1 Beyond the monad So far, we have seen how monads define a common abstraction over

More information