Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for...

148
Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) Northeastern University

Transcript of Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for...

Page 1: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional Programming is Easy, and Good for You

Matthias Felleisen (PLT)Northeastern University

Page 2: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

I am not a salesman.

Page 3: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional Programming

Page 4: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional Programming Functional Programming Languages

Page 5: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional Programming Functional Programming Languages!=

Page 6: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional Programming Functional Programming Languages!=Theorem

Proof:

Page 7: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional Programming Functional Programming Languages!=Theorem

Proof:

Page 8: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional Programming Functional Programming Languages

Page 9: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional Programming Functional Programming Languages

pureClean

mostlyOCaml

Page 10: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional Programming Functional Programming Languages

pureClean

mostlyOCaml

lazyHaskell

strictall others

Page 11: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional Programming Functional Programming Languages

pureClean

mostlyOCaml

lazyHaskell

strictall others

typedSML

untypedScheme

Page 12: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional Programming Functional Programming Languages

pureClean

mostlyOCaml

lazyHaskell

strictall others

typedSML

untypedScheme

first-orderACL2

higher-orderall others

Page 13: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional Programming Functional Programming Languages

pureClean

mostlyOCaml

lazyHaskell

strictall others

typedSML

untypedScheme

first-orderACL2

higher-orderall others

standard VMF#, Scala

special VMRacket

Page 14: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional Programming Functional Programming Languages

pureClean

mostlyOCaml

lazyHaskell

strictall others

typedSML

untypedScheme

first-orderACL2

higher-orderall others

standard VMF#, Scala

special VMRacket

distributedErlang

parallelClojure

Page 15: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

If all of this is functional programming (languages),isn’t it all overwhelming and difficult?

Page 16: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

If all of this is functional programming (languages),isn’t it all overwhelming and difficult?

{Not at all. And I am here to explainwhatwhy+/-

Page 17: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

What is Functional Programming? What is a Functional Programming Language?

Page 18: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Pop Quiz

Page 19: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Pop Quiz

Though [it] came from many motivations, ... one was to find a more flexible version of assignment, and then to try to eliminate it altogether.

Favor immutability.

Use value objects when possible.

: Who said this?

Page 20: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Though [it] came from many motivations, ... one was to find a more flexible version of assignment, and then to try to eliminate it altogether. Alan Kay, History of Sma!talk (1993)

Favor immutability. Joshua Bloch,

Effective Java (2001)

Use value objects when possible. Kent Beck,

Test Driven Development (2001)

Answer: The OO Experts

Page 21: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

So one definition of functional programming is

no (few) assignment statements no (few) mutable objects.

One Definition of Functional Programming

Page 22: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

So one definition of functional programming is

no (few) assignment statements no (few) mutable objects.

One Definition of Functional Programming

some method

Page 23: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

So one definition of functional programming is

no (few) assignment statements no (few) mutable objects.

One Definition of Functional Programming

same inputsame input

some method

Page 24: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

So one definition of functional programming is

no (few) assignment statements no (few) mutable objects.

One Definition of Functional Programming

same inputsame input

some method

same output

(most of the time)

Page 25: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

some method

another method

your program

Another Definition

Page 26: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

some method

another method

your program

Another Definition

implicitcommunication

Page 27: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

some method

another method

your program

Another Definition

implicitcommunication

Page 28: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

some method

another method

explicitcomposition

your program

Another Definition

Page 29: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

What does this mean concretely?

Page 30: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

According to either definition, you can program functionally

in any programming language.

Page 31: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

According to either definition, you can program functionally

in any programming language.A functional language ensures that you don’t

accidentally cheat.

Page 32: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

midnight`click’

`click’

initialstates

intermediatestates

finalstates

traffic light simulator

Page 33: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

imperative OOPL

initial: setToRedonTick: setTimeonClick: nextColorstopWhen:atMidnight,renderWarningtoDraw: renderTrafficLight

type State = Color x TimeState current = ...

void setToRed() { ... }void nextColor() { ... }void renderTrafficLight() { ... }void setTime() { ... }boolean atMidnight() { ... }void renderWarning() { ... }

Page 34: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

imperative OOPL

initial: setToRedonTick: setTimeonClick: nextColorstopWhen:atMidnight,renderWarningtoDraw: renderTrafficLight

type State = Color x TimeState current = ...

void setToRed() { ... }void nextColor() { ... }void renderTrafficLight() { ... }void setTime() { ... }boolean atMidnight() { ... }void renderWarning() { ... }

Page 35: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

initial: setToRedonTick: setTimeonClick: nextColorstopWhen: atMidnight,renderWarningtoDraw: renderTrafficLight

functional

type State = Initial U Intermediate U Final

Initial setToRed()State nextColor(State current) Image renderLight(State current)State setTime(State current)boolean atMidnight(State current) : FinalImage renderWarning(Final current)

Page 36: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

initial: setToRedonTick: setTimeonClick: nextColorstopWhen: atMidnight,renderWarningtoDraw: renderTrafficLight

functional

type State = Initial U Intermediate U Final

Initial setToRed()State nextColor(State current) Image renderLight(State current)State setTime(State current)boolean atMidnight(State current) : FinalImage renderWarning(Final current)

explicit state transformations

allow local reasoning

Page 37: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

initial: setToRedonTick: setTimeonClick: nextColorstopWhen: atMidnight,renderWarningtoDraw: renderTrafficLight

functional

type State = Initial U Intermediate U Final

Initial setToRed()State nextColor(State current) Image renderLight(State current)State setTime(State current)boolean atMidnight(State current) : FinalImage renderWarning(Final current)

explicit state transformations

allow local reasoning

Page 38: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

setToRed();renderLight();nextColor();nextColor();setTime();renderLight();nextColor();

if atMidnight() renderWarning()else renderLight();

Imperative Programming

Page 39: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

setToRed();renderLight();nextColor();nextColor();setTime();renderLight();nextColor();

if atMidnight() renderWarning()else renderLight();

Imperative Programming Functional ProgrammingState s1 = setToRed()Image i1 = renderLight(s1)State s2 = nextColor(s1)State s3 = nextColor(s2)State s4 = setTime(s3)Image i4 = renderLight(s4)State s5 = nextColor(s4)

Image i5 = atMidnight(s5) ? renderWarning(s5), renderLight(s5)

Page 40: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

It all looks easy.

Page 41: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

It all looks easy.

So what’s the catch?

Page 42: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Imagine a state that uses a record, which contains vector in each slot, and each record contains maps that

map names to lists of immutable data.And imagine that you want to equip

the monster with a dagger.

type State = { monsters : Vector<Monster>, fighter : Status, turns : Natural }type Monster = Map<String,List<Weapon>> type Weapon = ...

Page 43: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Imagine a state that uses a record, which contains vector in each slot, and each record contains maps that

map names to lists of immutable data.And imagine that you want to equip

the monster with a dagger.

type State = { monsters : Vector<Monster>, fighter : Status, turns : Natural }type Monster = Map<String,List<Weapon>> type Weapon = ...

Page 44: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Imagine a state that uses a record, which contains vector in each slot, and each record contains maps that

map names to lists of immutable data.And imagine that you want to equip

the monster with a dagger.

type State = { monsters : Vector<Monster>, fighter : Status, turns : Natural }type Monster = Map<String,List<Weapon>> type Weapon = ...

State

Page 45: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Imagine a state that uses a record, which contains vector in each slot, and each record contains maps that

map names to lists of immutable data.And imagine that you want to equip

the monster with a dagger.

type State = { monsters : Vector<Monster>, fighter : Status, turns : Natural }type Monster = Map<String,List<Weapon>> type Weapon = ...

State

Page 46: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Imagine a state that uses a record, which contains vector in each slot, and each record contains maps that

map names to lists of immutable data.And imagine that you want to equip

the monster with a dagger.

type State = { monsters : Vector<Monster>, fighter : Status, turns : Natural }type Monster = Map<String,List<Weapon>> type Weapon = ...

State

Page 47: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

state.monsters[i][“orc”].addList(“dagger”);

Imperative Programming

type State = { monsters : Vector<Monster>, fighter : Status, turns : Natural }type Monster = Map<String,List<Weapon>> type Weapon = ...

Page 48: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Context x List<Weapon> <c,w> = unzip(state);List<Weapon> new_list = addList(“dagger”);zip(c,new_list);

Functional Programming

type State = { monsters : Vector<Monster>, fighter : Status, turns : Natural }type Monster = Map<String,List<Weapon>> type Weapon = ...

Page 49: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Context x List<Weapon> <c,w> = unzip(state);List<Weapon> new_list = addList(“dagger”);zip(c,new_list);

Functional Programming

type State = { monsters : Vector<Monster>, fighter : Status, turns : Natural }type Monster = Map<String,List<Weapon>> type Weapon = ...

complex operations

Page 50: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

State

Context x List<Weapon> <c,w> = unzip(state);List<Weapon> new_list = addList(“dagger”);zip(c,new_list);

Functional Programming

Page 51: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

State

Context x List<Weapon> <c,w> = unzip(state);List<Weapon> new_list = addList(“dagger”);zip(c,new_list);

Functional Programming

going in

Page 52: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

State

Context x List<Weapon> <c,w> = unzip(state);List<Weapon> new_list = addList(“dagger”);zip(c,new_list);

Functional Programming

coming out

going in

Page 53: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

a problem of expressiveness

(“notational” overhead)

a problem of algorithmics

(“slow” performance)

Page 54: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

a problem of expressiveness

(“notational” overhead)

a problem of algorithmics

(“slow” performance)

solution 1: zip/unzip & functional data structures

Page 55: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

a problem of expressiveness

(“notational” overhead)

a problem of algorithmics

(“slow” performance)

solution 1: zip/unzip & functional data structures solution 2: monads

and other fancy constructs

Page 56: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

a problem of expressiveness

(“notational” overhead)

a problem of algorithmics

(“slow” performance)

solution 1: zip/unzip & functional data structures solution 2: monads

and other fancy constructs

solution 3: “bite the bullet” --allow mutation in FP and FPLs

Page 57: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

solution 1: functional datastructures do not truly

eliminate notational overhead

a problem of expressiveness

Page 58: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

solution 1: functional datastructures do not truly

eliminate notational overhead

solution 2: monadsgets close. The remaining type overhead is arguably

an advantage. It helps tame side effects.

a problem of expressiveness

Page 59: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

solution 1: functional datastructures do not truly

eliminate notational overhead

solution 2: monadsgets close. The remaining type overhead is arguably

an advantage. It helps tame side effects. solution 3: mutation in FP and FPLs

eliminates the problem as much as desired. Danger: it opens the flood

gate for careless programmers.

a problem of expressiveness

Page 60: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

functional data structures: we have no proof that functional data structures are as efficient as imperative programming.

a problem of algorithmics:

theory

Page 61: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

functional data structures: we have no proof that functional data structures are as efficient as imperative programming.

monads: theyare implemented imperatively.

Period.

a problem of algorithmics:

theory

Page 62: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

functional data structures: we have no proof that functional data structures are as efficient as imperative programming.

monads: theyare implemented imperatively.

Period. assignments in FPLs: they eliminates the problem as much as desired. Danger: it tempts programmers to use mutation too much.

a problem of algorithmics:

theory

Page 63: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

mix and match: people tend to combine monads or mutation with

functional data structures.

a problem of algorithmics:

in practice

Page 64: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

mix and match: people tend to combine monads or mutation with

functional data structures.

measuring end-to-end performance: efficiency is in

practice indistinguishable from imperative programming.

a problem of algorithmics:

in practice

Page 65: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

mix and match: people tend to combine monads or mutation with

functional data structures.

measuring end-to-end performance: efficiency is in

practice indistinguishable from imperative programming.

catch: it takes experience to reach this point.

a problem of algorithmics:

in practice

Page 66: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

About Myself

I am not a purist.I am not neutral.

Page 67: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

About Myself

I am not a purist.I am not neutral.

research: objects, assignment statements, design patterns, web servlets, continuations,

modules, functional I/O, etc.

programming: mostly functional, but OO and

imperative as neede

Page 68: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

About Myself

I am not a purist.I am not neutral.

research: objects, assignment statements, design patterns, web servlets, continuations,

modules, functional I/O, etc.

programming: mostly functional, but OO and

imperative as neede

teaching: start with functional programming in purely

functional, strict languagesfor 10,000s of students, startingin 7th grade all the way to M.S.

Page 69: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Why Functional Programming? Why a Functional Programming Language?

Page 70: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

program

Page 71: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

programrun

Page 72: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

programcreate

run

Page 73: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

programcreate

runtest

Page 74: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

programcreate

runtest

maintain

Page 75: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

programcreate

runtest

maintain

port

Page 76: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

programcreate

runtest

maintain

port

Programs must be written for people to read, and only incidentally for machines to execute. from: Abelson & Sussman, SICP

Page 77: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

The cost of software is a function of the cost of programmer communication.

Page 78: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

The cost of software is a function of the cost of programmer communication.

Functional programming and better functional programming languages

greatly reduce the cost of communication and thus the cost of software.

Page 79: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

There are many sides to the cost story: human, training, technical.

Page 80: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

1995:DrScheme

15 yrs of FPin high schools

Page 81: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

1995:DrScheme

15 yrs of FPin high schools

2005: Bootstrapfor grades 6-8

Page 82: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

1995:DrScheme

15 yrs of FPin high schools

2005: Bootstrapfor grades 6-8

Animations

Interactive Games

Distributed Games,Chat Rooms

Page 83: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

1995:DrScheme

15 yrs of FPin high schools

2005: Bootstrapfor grades 6-8

If these students can do FP, it is easy

Animations

Interactive Games

Distributed Games,Chat Rooms

Page 84: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

1995:DrScheme

15 yrs of FPin high schools

2005: Bootstrapfor grades 6-8

If these students can do FP, it is easy

Why did these students improve so much in math?

Why do they pass the state test?

Animations

Interactive Games

Distributed Games,Chat Rooms

Page 85: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Northeastern University: 5 year programs, including three 6-month

supervised co-op positions in industry

2001: conventional first-year

introduction to OO (Java) programming and discrete math

Page 86: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Northeastern University: 5 year programs, including three 6-month

supervised co-op positions in industry

2001: conventional first-year

introduction to OO (Java) programming and discrete math

only 1/3 of the students get co-op positions that involve programming

Page 87: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Northeastern University: 5 year programs, including three 6-month

supervised co-op positions in industry

2001: conventional first-year

introduction to OO (Java) programming and discrete math

only 1/3 of the students get co-op positions that involve programming

2006: functional-then-OO first-year introduction to programming

and discrete math

Page 88: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Northeastern University: 5 year programs, including three 6-month

supervised co-op positions in industry

2001: conventional first-year

introduction to OO (Java) programming and discrete math

only 1/3 of the students get co-op positions that involve programming

over 2/3 of the students get co-op positions that involve programming

2006: functional-then-OO first-year introduction to programming

and discrete math

Page 89: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Northeastern University: 5 year programs, including three 6-month

supervised co-op positions in industry

2001: conventional first-year

introduction to OO (Java) programming and discrete math

only 1/3 of the students get co-op positions that involve programming

over 2/3 of the students get co-op positions that involve programming

2006: functional-then-OO first-year introduction to programming

and discrete math

Graduate dean: “Industry asks, why can’t your MS students program as well as your

undergraduates?”

Page 90: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Northeastern University: 2 year MS programs (one co-op)

now comes with a 4-month introduction to Functional Program Design

called “Bootcamp”

Page 91: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Teaching FP has a highly beneficial effect on programmerseven if they don’t end up programming that way.

Page 92: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Teaching FP has a highly beneficial effect on programmerseven if they don’t end up programming that way.

Time to look at some technical points.

Page 93: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

f(x,y,z) = ...⎰ f(g(x), h(y), i(z,x)) dx ...

Mathematics

From mathematical models to programs

Page 94: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

f(x,y,z) = ...⎰ f(g(x), h(y), i(z,x)) dx ...

Mathematics

f(x,y,z) = ...integrate(f(g(x), h(y), i(z,x)))...

Program

From mathematical models to programs

Page 95: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

f(x,y,z) = ...⎰ f(g(x), h(y), i(z,x)) dx ...

Mathematics

f(x,y,z) = ...integrate(f(g(x), h(y), i(z,x)))...

Program

Yes, they basically look the same and it is easy

to convince yourself thatthey mean the same.

From mathematical models to programs

Page 96: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

type Contract = zero | scale of Contract * Double | and of Contract * Contract | until of Contract * Observation | ...

From algebraic types to functions

Page 97: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

type Contract = zero | scale of Contract * Double | and of Contract * Contract | until of Contract * Observation | ...

fun Number value(Contract c, Model m) = case c zero -> ... | scale(base,fac) -> . value(base,m) . | and(c1,c2) -> . value(c1,m) ... . value(c2,m) ... | until(base,obs) -> . value(base,m) ... | ...

algebraic types translate directly into a function outline

From algebraic types to functions

Page 98: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

type Contract = zero | scale of Contract * Double | and of Contract * Contract | until of Contract * Observation | ...

fun Number value(Contract c, Model m) = case c zero -> ... | scale(base,fac) -> . value(base,m) . | and(c1,c2) -> . value(c1,m) ... . value(c2,m) ... | until(base,obs) -> . value(base,m) ... | ...

algebraic types translate directly into a function outline

Imagine all the OO design patterns you need in Java.

From algebraic types to functions

Page 99: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

type State = Color x Time

void setToRed() { ... }void nextColor() { ... }void renderTrafficLight() { ... }void setTime() { ... }boolean atMidnight() { ... }void renderWarning() { ... }

ImperativeFrom function signatures to understanding

Page 100: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

type State = Color x Time

void setToRed() { ... }void nextColor() { ... }void renderTrafficLight() { ... }void setTime() { ... }boolean atMidnight() { ... }void renderWarning() { ... }

type State = Initial U Intermediate U Final

Initial setToRed()State nextColor(State current) Image renderLight(State current)State setTime(State current)boolean atMidnight(State current) : FinalImage renderWarning(Final current)

Functional

ImperativeFrom function signatures to understanding

Page 101: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

type State = Color x Time

void setToRed() { ... }void nextColor() { ... }void renderTrafficLight() { ... }void setTime() { ... }boolean atMidnight() { ... }void renderWarning() { ... }

type State = Initial U Intermediate U Final

Initial setToRed()State nextColor(State current) Image renderLight(State current)State setTime(State current)boolean atMidnight(State current) : FinalImage renderWarning(Final current)

Functional

Imperative

Type signatures convey a lot of information.

From function signatures to understanding

Page 102: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

type State = Color x Time

void setToRed() { ... }void nextColor() { ... }void renderTrafficLight() { ... }void setTime() { ... }boolean atMidnight() { ... }void renderWarning() { ... }

type State = Initial U Intermediate U Final

Initial setToRed()State nextColor(State current) Image renderLight(State current)State setTime(State current)boolean atMidnight(State current) : FinalImage renderWarning(Final current)

Functional

Imperative

Type signatures convey a lot of information.

VOID conveys nothing.

From function signatures to understanding

Page 103: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

type State = Color x Time

void setToRed() { ... }void nextColor() { ... }void renderTrafficLight() { ... }void setTime() { ... }boolean atMidnight() { ... }void renderWarning() { ... }

type State = Initial U Intermediate U Final

Initial setToRed()State nextColor(State current) Image renderLight(State current)State setTime(State current)boolean atMidnight(State current) : FinalImage renderWarning(Final current)

Functional

Imperative

Type signatures convey a lot of information.

VOID conveys nothing.

From function signatures to understanding

Subtract $10 for every VOID return type in your programmers code.

Page 104: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

test case { setUpForSetTime(); setTime(); testCurrentState(expectedState); testFrameConditions(); tearDownSetTime()}

ImperativeFrom functions to testing

Page 105: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

test case { setUpForSetTime(); setTime(); testCurrentState(expectedState); testFrameConditions(); tearDownSetTime()}

test case { compare(setTime(someState),expectedState);}

Functional

ImperativeFrom functions to testing

Page 106: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

test case { setUpForSetTime(); setTime(); testCurrentState(expectedState); testFrameConditions(); tearDownSetTime()}

test case { compare(setTime(someState),expectedState);}

Functional

Imperative

state is transferred explicitlyand can be understood in isolation

From functions to testing

Page 107: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

test case { setUpForSetTime(); setTime(); testCurrentState(expectedState); testFrameConditions(); tearDownSetTime()}

test case { compare(setTime(someState),expectedState);}

Functional

Imperative

state is transferred explicitlyand can be understood in isolation

From functions to testing

Tests in the functional world become “one liners”. And that works for

compositions, too.

Page 108: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

search_good_solution( criteria, generate_all_solutions(model, state0));

Function Composition in Action

Page 109: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

search_winning_move( improve_likelihood(current_state), generate_all_moves(model, state0));

Page 110: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

search_winning_move( improve_likelihood(current_state), generate_all_moves(model, state0));

all?

Page 111: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

search_winning_move( improve_likelihood(current_state), generate_all_moves(model, state0));

Yes, on demand. Lazy data structures enable

a powerful, yet simple compositional style

all?

Page 112: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

search_winning_move( improve_likelihood(current_state), generate_all_moves(model, state0));

game tree

Page 113: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

search_winning_move( improve_likelihood(current_state), generate_all_moves(model, state0));

game tree

evaluate some nodes, not all

Page 114: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

search_winning_move( improve_likelihood(current_state), generate_all_moves(model, state0));

game tree

evaluate some nodes, not all

Imperative OOP can express this idea, but only in extremely ugly ways,

too ugly for this slide.

Page 115: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Function composition is pervasive, even in the strict world.

Page 116: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Financial Contracts as Functional Compostion

type Contract ... Observation ... Currency

fun Contract zero() ...fun Contract one(Currency c) ...fun Contract when(Obs t, Contract c) ...fun Contract scale(Double s, Contract c)...fun Observation at(Date d) : Obs ...

Combinator DSL

Page 117: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Financial Contracts as Functional Compostion

type Contract ... Observation ... Currency

fun Contract zero() ...fun Contract one(Currency c) ...fun Contract when(Obs t, Contract c) ...fun Contract scale(Double s, Contract c)...fun Observation at(Date d) : Obs ...

Combinator DSL

fun zero_coupon_discount_bond(t,x,k) = when (at t) (scale (konst x) (one k))

Page 118: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Financial Contracts as Functional Compostion

type Contract ... Observation ... Currency

fun Contract zero() ...fun Contract one(Currency c) ...fun Contract when(Obs t, Contract c) ...fun Contract scale(Double s, Contract c)...fun Observation at(Date d) : Obs ...

Combinator DSL

fun zero_coupon_discount_bond(t,x,k) = when (at t) (scale (konst x) (one k))

One Contract

Page 119: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Financial Contracts as Functional Compostion

type Contract ... Observation ... Currency

fun Contract zero() ...fun Contract one(Currency c) ...fun Contract when(Obs t, Contract c) ...fun Contract scale(Double s, Contract c)...fun Observation at(Date d) : Obs ...

Combinator DSL

fun zero_coupon_discount_bond(t,x,k) = when (at t) (scale (konst x) (one k))

One ContractSimon Peyton Jones

Page 120: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Simple functions represent basic ideas.

Page 121: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Simple functions represent basic ideas.

Combinator functions combine ideas.

Page 122: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Simple functions represent basic ideas.

Combinator functions combine ideas.

With function composition programmers create and communicate

programs in combinator DSLs.

Page 123: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional programming languages in the LISP tradition use a “template” approach to

DSLs in addition to combinators(Scheme, Clojure, Racket,

Template Haskell).

Page 124: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

The last part of the functional story:parallelism.

Compilers think, too.

Page 125: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

some expression

another expression

your program

Remember the Definition

implicitcommunication

Page 126: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

some expression another expression

your program

aFun( , )implicit

communication

Page 127: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

some expression another expression

your program

aFun( , )run in parallel

Page 128: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

some expression another expression

your program

aFun( , )run in parallel

In the purely functional world, the compiler does not need proof

of non-interference. It is built into the programming language.

Page 129: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Implicit parallelism is free in functional programming languages.

Page 130: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Sadly, this story is naive and unrealistic, and yet it contains the key to a parallel future.

Page 131: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Sadly, this story is naive and unrealistic, and yet it contains the key to a parallel future.

In the imperative world mutation creates too few opportunities for automatic parallel execution.

In the functional world a lack of dependencies means too many opportunities for automatic parallel execution.

Page 132: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Sadly, this story is naive and unrealistic, and yet it contains the key to a parallel future.

In the imperative world mutation creates too few opportunities for automatic parallel execution.

In the functional world a lack of dependencies means too many opportunities for automatic parallel execution.

The imperative world will see explicit parallel programming and the big battle against race condition bugs.

Page 133: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Sadly, this story is naive and unrealistic, and yet it contains the key to a parallel future.

In the imperative world mutation creates too few opportunities for automatic parallel execution.

In the functional world a lack of dependencies means too many opportunities for automatic parallel execution.

The imperative world will see explicit parallel programming and the big battle against race condition bugs.

The functional world will provide explicit parallel programming with fewer race conditions.

Page 134: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

25 years of research on parallelism for FORTRAN calls for mostly functional intermediate compiler representations (PDGs, SSAs).

Explicit parallelism is easy in functional programming

languages.

Page 135: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

25 years of research on parallelism for FORTRAN calls for mostly functional intermediate compiler representations (PDGs, SSAs).

Functional programming languages make the dependencies explicit and thus facilitate the compiler’s reasoning task.

Explicit parallelism is easy in functional programming

languages.

Page 136: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

So what is my favorite functional language?

Page 137: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

What is my favoritefunctional programming language?

Page 138: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

The Racket language - pattern matching et al. - classes - cross-platform GUIs - extensive libraries - rich web programming

Page 139: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

The Racket language - pattern matching et al. - classes - cross-platform GUIs - extensive libraries - rich web programming

The Lazy Racket language - streams - lazy trees

Page 140: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

The Racket language - pattern matching et al. - classes - cross-platform GUIs - extensive libraries - rich web programming

The Lazy Racket language - streams - lazy trees

The Typed Racket language - union types & subtyping - first-class polymorphism - accommodates existing idioms

Page 141: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

The Racket language - pattern matching et al. - classes - cross-platform GUIs - extensive libraries - rich web programming

The Lazy Racket language - streams - lazy trees

The Typed Racket language - union types & subtyping - first-class polymorphism - accommodates existing idioms

The FrTime language - functional reactive programming

The Web language

The Scribble languageThe Slideshow language

Page 142: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

The Racket language - pattern matching et al. - classes - cross-platform GUIs - extensive libraries - rich web programming

The Lazy Racket language - streams - lazy trees

The Typed Racket language - union types & subtyping - first-class polymorphism - accommodates existing idioms

The FrTime language - functional reactive programming

The Web language

The Scribble languageThe Slideshow language

The Foundation (10 core constructs)

Page 143: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

The Racket language - pattern matching et al. - classes - cross-platform GUIs - extensive libraries - rich web programming

The Lazy Racket language - streams - lazy trees

The Typed Racket language - union types & subtyping - first-class polymorphism - accommodates existing idioms

The FrTime language - functional reactive programming

The Web language

The Scribble languageThe Slideshow language

The Foundation (10 core constructs)

powerful DSL Framework

Page 144: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

The Racket language - pattern matching et al. - classes - cross-platform GUIs - extensive libraries - rich web programming

The Lazy Racket language - streams - lazy trees

The Typed Racket language - union types & subtyping - first-class polymorphism - accommodates existing idioms

The FrTime language - functional reactive programming

The Web language

The Scribble languageThe Slideshow language

The Foundation (10 core constructs)

powerful DSL FrameworkMatthew Flatt, UUtah

Page 145: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Summary

Page 146: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Functional programming is about clear, concise

communication between programmers.

A good transition needs training, but training pays off.

Functional programming languages keep you honest

about being functional.

Page 147: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Thank You

Page 148: Functional Programming is Easy, and Good for You · Functional Programming is Easy, and Good for You Matthias Felleisen (PLT) ... in any programming language. A functional language

Though Smalltalk came from many motivations, ... one was to find a more flexible version of assignment, and then to try to eliminate it altogether. Alan Kay, History of Sma!talk (1993)

Favor immutability. Joshua Bloch,

Effective Java (2001)

Use value objects when possible. Kent Beck,

Test Driven Development (2001)

Thank You