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

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 JavaScriptWill Kurt
 
Functional programming seminar (haskell)
Functional programming seminar (haskell)Functional programming seminar (haskell)
Functional programming seminar (haskell)Bikram Thapa
 
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data scienceJohn Cant
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven DesignRyan Riley
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskellLuca Molteni
 
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!priort
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskellfaradjpour
 
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.1EwaB
 
Presentacio Puigcerda 09
Presentacio Puigcerda 09Presentacio Puigcerda 09
Presentacio Puigcerda 09jdegra
 
GLBT ALMS 2008 program
GLBT ALMS 2008 programGLBT ALMS 2008 program
GLBT ALMS 2008 programglbtalms
 
Pothiz july-2010
Pothiz july-2010Pothiz july-2010
Pothiz july-2010Pothi.com
 
An Inside Look at Campaign 2008
An Inside Look at Campaign 2008An Inside Look at Campaign 2008
An Inside Look at Campaign 2008tarekrizk
 
Sketch Ppt Snapshot
Sketch Ppt SnapshotSketch Ppt Snapshot
Sketch Ppt Snapshotrachanasalvi
 
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 AlbertslundStig Brandt
 
Seven Domains of Predictability - BPMCM 2014
Seven Domains of Predictability - BPMCM 2014Seven Domains of Predictability - BPMCM 2014
Seven Domains of Predictability - BPMCM 2014Keith Swenson
 
Hagelin Invincibility Brochure A 4
Hagelin Invincibility Brochure A 4Hagelin Invincibility Brochure A 4
Hagelin Invincibility Brochure A 4AMTR
 

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

Real World Haskell: Lecture 7
Real World Haskell: Lecture 7Real World Haskell: Lecture 7
Real World Haskell: Lecture 7Bryan O'Sullivan
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Bryan O'Sullivan
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Bryan O'Sullivan
 
Real World Haskell: Lecture 4
Real World Haskell: Lecture 4Real World Haskell: Lecture 4
Real World Haskell: Lecture 4Bryan O'Sullivan
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Bryan O'Sullivan
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Bryan O'Sullivan
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Bryan O'Sullivan
 
CUFP 2009 Keynote - Real World Haskell
CUFP 2009 Keynote - Real World HaskellCUFP 2009 Keynote - Real World Haskell
CUFP 2009 Keynote - Real World HaskellBryan O'Sullivan
 
DEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World HaskellDEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World HaskellBryan O'Sullivan
 

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

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 

Recently uploaded (20)

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 

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