the other side of functional programming: haskell for erlang people

40
The other side of functional programming Haskell: purity, types, and a damn good time Thursday, April 30, 2009

Upload: bryan-osullivan

Post on 12-May-2015

5.588 views

Category:

Education


3 download

DESCRIPTION

These are the slides I presented in a talk at the Bay Area Erlang Factory, introducing Erlang programmers to some of the ideas behind Haskell.

TRANSCRIPT

Page 1: The other side of functional programming: Haskell for Erlang people

The other side of functional programmingHaskell: purity, types, and a damn good time

Thursday, April 30, 2009

Page 2: The other side of functional programming: Haskell for Erlang people

In the beginning

Haskell was developed by academics to unify many streams of research

Committee began work in 1987

Haskell Report 1.0 published April 1, 1990

Comparable in age with Erlang

Thursday, April 30, 2009

Page 3: The other side of functional programming: Haskell for Erlang people

Principal concerns

Laziness

Purity

Strong, static types

Thursday, April 30, 2009

Page 4: The other side of functional programming: Haskell for Erlang people

Being lazy with style

Thursday, April 30, 2009

Page 5: The other side of functional programming: Haskell for Erlang people

Laziness

The original unifying theme of the designers

Evaluate an expression when its result is needed

Evaluate only the minimum needed

Thursday, April 30, 2009

Page 6: The other side of functional programming: Haskell for Erlang people

A simple lazy example

A simple Haskell function definition

square x = x * x

Define the name square

Give it a free variable x

The function body follows the =

Thursday, April 30, 2009

Page 7: The other side of functional programming: Haskell for Erlang people

Evaluate in a lazy world

What is the result of square 3 ?

The number 9 ?

Or the unevaluated expression 3 * 3 ?

(It’s the latter)

Thursday, April 30, 2009

Page 8: The other side of functional programming: Haskell for Erlang people

When do we evaluate?

When does the expression 3 * 3 turn into something meaningful?

For instance, when we need to print its result

Evaluation is driven by need

Often referred to as call-by-need

Contrast with the more familiar call-by-value

Thursday, April 30, 2009

Page 9: The other side of functional programming: Haskell for Erlang people

Laziness as default

Laziness is pervasive in Haskell code

But sometimes it is not desirable

Option: use strict evaluation when necessary

Many strict languages provide optional laziness

The apparent gulf isn’t so big after all

Thursday, April 30, 2009

Page 10: The other side of functional programming: Haskell for Erlang people

Purity is the new black

Thursday, April 30, 2009

Page 11: The other side of functional programming: Haskell for Erlang people

Purity

Haskell data is immutable

Functions are pure

Only affected by their inputs

Not subject to mutable global state

(Again, these are defaults: mutability is available as an option)

Thursday, April 30, 2009

Page 12: The other side of functional programming: Haskell for Erlang people

Why purity?

What’s a side effect?

Mutating global state

Performing I/O

Remember laziness?

Evaluation by need

Laziness and side effects don’t mix!

Thursday, April 30, 2009

Page 13: The other side of functional programming: Haskell for Erlang people

Laziness needs purity

Haskell chose laziness by default

Therefore purity was inescapable

This has big consequences

Composability: glue functions together

Safety: functions are black boxes

Thursday, April 30, 2009

Page 14: The other side of functional programming: Haskell for Erlang people

Purity as a big bet

Haskell’s laziness led to its purity

Purity is a bold step towards better software

Arguably a more important choice than laziness!

Thursday, April 30, 2009

Page 15: The other side of functional programming: Haskell for Erlang people

Adventures with types

Thursday, April 30, 2009

Page 16: The other side of functional programming: Haskell for Erlang people

Strong static types

Valid Haskell expressions are assigned types at compile time

a :: String

a = “some text”

The :: says that a has the type String

This is called a type annotation

Thursday, April 30, 2009

Page 17: The other side of functional programming: Haskell for Erlang people

Wait ... static types?

Aren’t we supposed to hate static types?

Didn’t types cause us RSI in Java and C++?

Wasn’t that part of why we escaped to the dynamically typed languages?

Crummy languages give static types a bad name

Thursday, April 30, 2009

Page 18: The other side of functional programming: Haskell for Erlang people

Yes, static types!

A Haskell compiler infers the type of an expression

It does this automatically

The type annotations that you’ve seen are optional

Handy for documentation, but superfluous

Thursday, April 30, 2009

Page 19: The other side of functional programming: Haskell for Erlang people

Simple use of types

Any sensible language will reject stuff like this

1 + “3foo”

(Notable exception: Perl)

Dynamic languages barf at runtime

Languages like Haskell reject at compile time

Thursday, April 30, 2009

Page 20: The other side of functional programming: Haskell for Erlang people

Pattern matching

Here’s the classic way to calculate a list’s length

length [] = 0

length (x:xs) = 1 + length xs

We’ve defined a function using two equations

Choose which to use by input structure

Thursday, April 30, 2009

Page 21: The other side of functional programming: Haskell for Erlang people

Matching on structure

If the input list is empty, the length is 0

length [] = 0

If the input matches the list constructor :, bind the name xs to the list’s tail and recurse

length (x:xs) = 1 + length xs

Thursday, April 30, 2009

Page 22: The other side of functional programming: Haskell for Erlang people

Typing a listWhat is the type of length?

length :: [a] → Int

The [a] above means “a list of values of some unknown type a”

The → means “returns”

In other words, we have a function that does not know or care about the elements of its input list

Thursday, April 30, 2009

Page 23: The other side of functional programming: Haskell for Erlang people

Why use static types?

Static types are about more than catching mistakes

They let the compiler make complex decisions about the program’s behaviour

Thursday, April 30, 2009

Page 24: The other side of functional programming: Haskell for Erlang people

User-defined containersHere’s a widely used Haskell type

data Maybe a = Just a

| Nothing

We can pattern-match to inspect the structure of a user-defined type

isJust (Just x) = True

isJust Nothing = False

Thursday, April 30, 2009

Page 25: The other side of functional programming: Haskell for Erlang people

Algebraic data types

data ClientError =

BadRequest

| Unauthorized

| Forbidden

| NotFound

...

Thursday, April 30, 2009

Page 26: The other side of functional programming: Haskell for Erlang people

What does this buy?

If my function takes a HttpResponse

The compiler guarantees that I’ll never be given a HttpRequest

It guarantees that I’ll never see an unknown HttpResponse

It warns me if a pattern match omits a valid response

Thursday, April 30, 2009

Page 27: The other side of functional programming: Haskell for Erlang people

Safety with types

Static types give stronger guarantees than testing

A simple example:

“I know my function can never receive an argument of an invalid type”

More ambitious:

“This code can never perform I/O”

Thursday, April 30, 2009

Page 28: The other side of functional programming: Haskell for Erlang people

More serious type safety

We can omit features that other languages bake in

Ship them as libraries instead

A recent example:

Java-style checked exceptions as a library

Throwable exceptions are inferred

Thursday, April 30, 2009

Page 29: The other side of functional programming: Haskell for Erlang people

More serious types

We can model and enforce complex behaviour

Examples:

Information only flows from less secure to more secure code

Communicating processes follow a well-defined messaging protocol

Thursday, April 30, 2009

Page 30: The other side of functional programming: Haskell for Erlang people

Real world concerns

Thursday, April 30, 2009

Page 31: The other side of functional programming: Haskell for Erlang people

Performance

Haskell is ranked #3 on the Alioth Shootout

Usually within 1x to 5x of C’s performance

Great profiling tools help with tuning

It’s easy to write fast, concise Haskell

Community knowledge of how is a bit scattered

Thursday, April 30, 2009

Page 32: The other side of functional programming: Haskell for Erlang people

Going native

Haskell has a beautiful FFI

Call into and out of C code easily

Nifty libraries for other languages

Interop with .NET

Act as an Erlang node

Thursday, April 30, 2009

Page 33: The other side of functional programming: Haskell for Erlang people

ConcurrencyHaskell has a fantastic concurrent runtime

Works with multiple cores

Millions of concurrent threads

Advanced, but easy to use programming model

The default choices of immutable data and pure functions really help to write correct, scalable code

Thursday, April 30, 2009

Page 34: The other side of functional programming: Haskell for Erlang people

Thread synchronisation

Software Transactional Memory

Database-like transactional concurrency to regular code

Much safer than mutexes

Strongly typed message channels

Networked message support as a library

Thursday, April 30, 2009

Page 35: The other side of functional programming: Haskell for Erlang people

Parallel terminology

Parallel and concurrent programming are different

Parallel: how do I get one answer faster?

Concurrent: how do I do 80,000 different things per second?

Thursday, April 30, 2009

Page 36: The other side of functional programming: Haskell for Erlang people

Parallel programming

Mature support for making pure code parallel

Development version of GHC scales well on modern multicore boxes

Exciting research abounds

Nested data parallel vector code

GPU offload

Thursday, April 30, 2009

Page 37: The other side of functional programming: Haskell for Erlang people

Testing and assurance

The famous QuickCheck library arose in Haskell

Randomised property-based testing

Beats the pants off unit tests when applicable

Traditional unit testing libraries available too

Excellent code coverage analysis tools

Thursday, April 30, 2009

Page 38: The other side of functional programming: Haskell for Erlang people

Libraries

Over 1,000 libraries on http://hackage.haskell.org/

Game engines, bioinformatics, networking, database integration, music, compiler tools, ...

Single-command install of any library and its dependencies

Thursday, April 30, 2009

Page 39: The other side of functional programming: Haskell for Erlang people

Community

The best language community I know of

Stellar researchers, informed OSS hackers

Atmosphere is friendly, welcoming, and smart

Notable absence of rock stars

#haskell is 5th biggest channel on Freenode

Many great online learning resources

Thursday, April 30, 2009

Page 40: The other side of functional programming: Haskell for Erlang people

Thank you for your time!

Thursday, April 30, 2009