SlideShare a Scribd company logo
1 of 40
Download to read offline
The other side of
                   functional programming
                   Haskell: purity, types, and a damn good time

Thursday, April 30, 2009
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
Principal concerns

                    Laziness



                    Purity



                    Strong, static types



Thursday, April 30, 2009
Being lazy with style



Thursday, April 30, 2009
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
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
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
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
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
Purity is the new black



Thursday, April 30, 2009
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
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
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
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
Adventures with types



Thursday, April 30, 2009
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
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
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
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
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
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
Typing a list
                    What 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
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
User-defined containers
                    Here’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
Algebraic data types
                    data ClientError =

                            BadRequest

                           | Unauthorized

                           | Forbidden

                           | NotFound

                            ...


Thursday, April 30, 2009
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
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
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
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
Real world concerns



Thursday, April 30, 2009
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
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
Concurrency
                    Haskell 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
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
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
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
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
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
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
Thank you for your time!



Thursday, April 30, 2009

More Related Content

Viewers also liked

GLBT ALMS 2008 program
GLBT ALMS 2008 programGLBT ALMS 2008 program
GLBT ALMS 2008 program
glbtalms
 
Pothiz july-2010
Pothiz july-2010Pothiz july-2010
Pothiz july-2010
Pothi.com
 
Sketch Ppt Snapshot
Sketch Ppt SnapshotSketch Ppt Snapshot
Sketch Ppt Snapshot
rachanasalvi
 

Viewers also liked (20)

Introduction to Functional Programming with Haskell and JavaScript
Introduction to Functional Programming with Haskell and JavaScriptIntroduction to Functional Programming with Haskell and JavaScript
Introduction to Functional Programming with Haskell and JavaScript
 
Functional programming seminar (haskell)
Functional programming seminar (haskell)Functional programming seminar (haskell)
Functional programming seminar (haskell)
 
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data science
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
 
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
 
UBD - Użytkowanie baz danych wprowadzenie cz.1
UBD - Użytkowanie baz danych wprowadzenie cz.1UBD - Użytkowanie baz danych wprowadzenie cz.1
UBD - Użytkowanie baz danych wprowadzenie cz.1
 
Web 2.0 Presentatie 2009
Web 2.0 Presentatie 2009Web 2.0 Presentatie 2009
Web 2.0 Presentatie 2009
 
Vier in balans deltion 20032012
Vier in balans deltion 20032012Vier in balans deltion 20032012
Vier in balans deltion 20032012
 
Presentacio Puigcerda 09
Presentacio Puigcerda 09Presentacio Puigcerda 09
Presentacio Puigcerda 09
 
Report.lzl
Report.lzlReport.lzl
Report.lzl
 
GLBT ALMS 2008 program
GLBT ALMS 2008 programGLBT ALMS 2008 program
GLBT ALMS 2008 program
 
Pothiz july-2010
Pothiz july-2010Pothiz july-2010
Pothiz july-2010
 
An Inside Look at Campaign 2008
An Inside Look at Campaign 2008An Inside Look at Campaign 2008
An Inside Look at Campaign 2008
 
Sketch Ppt Snapshot
Sketch Ppt SnapshotSketch Ppt Snapshot
Sketch Ppt Snapshot
 
Micro-Interactions
Micro-InteractionsMicro-Interactions
Micro-Interactions
 
HeartMath Præsentation 22/10 i Albertslund
HeartMath Præsentation 22/10 i AlbertslundHeartMath Præsentation 22/10 i Albertslund
HeartMath Præsentation 22/10 i Albertslund
 
Seven Domains of Predictability - BPMCM 2014
Seven Domains of Predictability - BPMCM 2014Seven Domains of Predictability - BPMCM 2014
Seven Domains of Predictability - BPMCM 2014
 
Hagelin Invincibility Brochure A 4
Hagelin Invincibility Brochure A 4Hagelin Invincibility Brochure A 4
Hagelin Invincibility Brochure A 4
 

More from Bryan O'Sullivan (10)

Pronk like you mean it
Pronk like you mean itPronk like you mean it
Pronk like you mean it
 
Real World Haskell: Lecture 7
Real World Haskell: Lecture 7Real World Haskell: Lecture 7
Real World Haskell: Lecture 7
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5
 
Real World Haskell: Lecture 4
Real World Haskell: Lecture 4Real World Haskell: Lecture 4
Real World Haskell: Lecture 4
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1
 
CUFP 2009 Keynote - Real World Haskell
CUFP 2009 Keynote - Real World HaskellCUFP 2009 Keynote - Real World Haskell
CUFP 2009 Keynote - Real World Haskell
 
DEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World HaskellDEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World Haskell
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 

Recently uploaded (20)

BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 

The other side of functional programming: Haskell for Erlang people

  • 1. The other side of functional programming Haskell: purity, types, and a damn good time Thursday, April 30, 2009
  • 2. 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
  • 3. Principal concerns Laziness Purity Strong, static types Thursday, April 30, 2009
  • 4. Being lazy with style Thursday, April 30, 2009
  • 5. 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. Purity is the new black Thursday, April 30, 2009
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. Typing a list What 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
  • 23. 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
  • 24. User-defined containers Here’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
  • 25. Algebraic data types data ClientError = BadRequest | Unauthorized | Forbidden | NotFound ... Thursday, April 30, 2009
  • 26. 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
  • 27. 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
  • 28. 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
  • 29. 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
  • 31. 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
  • 32. 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
  • 33. Concurrency Haskell 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
  • 34. 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
  • 35. 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
  • 36. 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
  • 37. 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
  • 38. 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
  • 39. 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
  • 40. Thank you for your time! Thursday, April 30, 2009