Presentation
-
Upload
pnathanlogos -
Category
Technology
-
view
187 -
download
3
description
Transcript of Presentation
-5ptλ
Intro Definitions Features Example Wrap
Practical Functional Programming
Paul Nathan
Logos Bible Software,Bellingham, WA
Logos Tech Day, 2013
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Who
Who am I?
Paul Nathan, M.S.C.S.
Full Time SW Developer
Passionate about programming and its languages
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Why
All language influences how we think
Programming languages shape our capabilities
Expand our minds beyond yesterday
Because languages are cool!
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Why
All language influences how we think
Programming languages shape our capabilities
Expand our minds beyond yesterday
Because languages are cool!
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Why
All language influences how we think
Programming languages shape our capabilities
Expand our minds beyond yesterday
Because languages are cool!
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Why
All language influences how we think
Programming languages shape our capabilities
Expand our minds beyond yesterday
Because languages are cool!
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Overview
1 Intro
2 Definitions
3 Features
4 Example
5 Wrap
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
What is functional programming?
Bad Joke
Ask three computer scientists, get three answers and two researchprojects
Languages which claim they are
First class functions
“Anti” side-effects
Advanced type system?
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Claimed FP Langs
Partial list of FP languages...Agda, Clean, Clojure, Common Lisp, Erlang, Javascript, F# .NET,Haskell, Kernel, ML, occam, OCaml, Python, Ruby, Rust, Scala,Scheme... and more.
Targeted Languages
Common Lisp
Haskell
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Considerations
Functional programming(FP) is where most of programminglanguage research from 1975 on has lived.
Significant work has been done in the areas of correctness,control structures, static analysis, limits of computation, andmore.
Academic roots feeding into modern tools: C#, F#, Java,C++11, and more.
FP is based around the lambda calculus rather than the vonNeumman machine - “Math” vs “Engineering”.
Paradigm oriented around recursion, proofs, correctness, andsimplicity.
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Common Features
First class functions
“Anti” side-effects
Less/different OO
Declarative
Composable (sometimes)
Advanced type system (sometimes)
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
First Class Functions
Functions are “real” objects in the language. They are:
not compiled out (C)
not syntactic sugar (C# delegates)
passable/assignable
Example in Common Lisp
;;oddp is a regular function
CL-USER> (type-of #’oddp)
FUNCTION
;;passing oddp into mapcar
CL-USER> (mapcar #’oddp ’(1 2 3 4))
(T NIL T NIL)
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Different OO 01/10
Some ideas...
Types without objects
Usually multiple-dispatch
Usually no functions bound to an objectInstead of:
template<typename T> class CClass {
T function();
};
The usual idiom is similar to:
template<typename T> T function(T thing);
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Different OO 10/10
... and who “usually” uses what...
Many functions & few types (Clojure) “Everything” is adict/list/tuple.
Multimethods & many types (Common Lisp) “Everything”specializes on incoming types
Many types & many functions (Haskell) “Everything” ispushed into types for type checking
...the above is a crass generalization...
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Declarative
Say what you want done (Haskell)
let odds = map odd list_of_ints
Instead of how to do it (C++)
vector<bool> odds = new vector<int>();
for(int i = 0; i < list_of_ints.length(); i++)
odds.push_back(odd(list_of_ints[i]));
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Anti Side Effects
Side effects work against shared memory
Side effects work against parallelism
Side effects work against predictability.
Therefore
FP languages either deprecate side effects or go “pure”, strippingthem out entirely.
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Composable
Composable
With fewer objects/types, minimal side effects and adeclarative approach, a “composable” approach becomesmore practical in the architecture.
No “uber” framework, many “pluggable” mini-frameworks
Protocols: define an interface without a hierarchy - anythingthat behaves that way is a member.
Clojure a fascinating example.
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Advanced Type Systems?
Theorem 1 (Curry-Howard - approximately)
Because a program is a proof, a well-typed program is a proof ofcorrectness.
A sufficiently ’advanced’ type system allows this to be true.Sufficiently advanced languages include Haskell.
The advantage is a more reliable program - unreliable edgecases in interactions are prohibited by the type system.
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Practical Advanced Type Systems
In practice, this means...
... no pointers
... no null pointer exceptions
... union types
... pattern matching
... immutability
... no type cast runtime errors
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Practical Advanced Type Systems
In practice, this means...
... no pointers
... no null pointer exceptions
... union types
... pattern matching
... immutability
... no type cast runtime errors
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Practical Advanced Type Systems
In practice, this means...
... no pointers
... no null pointer exceptions
... union types
... pattern matching
... immutability
... no type cast runtime errors
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Practical Advanced Type Systems
In practice, this means...
... no pointers
... no null pointer exceptions
... union types
... pattern matching
... immutability
... no type cast runtime errors
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Practical Advanced Type Systems
In practice, this means...
... no pointers
... no null pointer exceptions
... union types
... pattern matching
... immutability
... no type cast runtime errors
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Practical Advanced Type Systems
In practice, this means...
... no pointers
... no null pointer exceptions
... union types
... pattern matching
... immutability
... no type cast runtime errors
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Demo problem
Demo problem
Measure similarity between users according to the SKUs they buy.
Similarity between two sets A and B is defined here as the JaccardIndex:
|A∩B||A∪B|
where 1.0 denotes 100% similarity and 0.0 denotes 0.0% similarity
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Data structures
The data structures needed to handle this are:
sku - ID + description
user - ID + name
purchase (link between user and sku) - sku + id + cost
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Haskell - datatypes
Haskell1 data Sku = Sku SkuID String
2 deriving (Show , Eq)
34 data User = User UserID String
5 deriving (Show , Eq)
67 data Purchase = Purchase UserID SkuID Dollars
8 deriving (Show , Eq)
910 -- Datatype to pretty -print simiarities
11 data Similarity = Similarity UserID UserID Double
12 deriving (Show , Eq)
1314 -- pattern matching
15 instance Ord Similarity where
16 compare (Similarity _ _ valuea) (Similarity _ _ valueb) = compare valueb
valuea
deriving (...) adds in machinery for pretty printing and equality.instance Ord... adds in machinery for comparisons.
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Common Lisp - datatypes
Common Lisp
1 (defstruct user id name)
23 (defstruct sku sku name)
45 (defstruct purchase userid sku cost)
Under the hood: getters/setters, pretty-printing, and a constructorcreated. No equality, ordering.
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Haskell - Jaccard
Haskell12 -- Force integer ->floating point division
3 -- Note the type signature.
4 fdiv :: (Fractional a, Integral a1 , Integral a2) => a1 -> a2 -> a
5 fdiv a b = (fromIntegral a) / (fromIntegral b)
67 jaccard_index :: (Eq a1, Fractional a) => [a1] -> [a1] -> a
8 jaccard_index a b =
9 (length (intersect a b)) ‘fdiv ‘ (length (union a b))
Surprise: Does not perform automatic conversions from integer tofloat. We can specify the precise behavior of the divide though.
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Common Lisp - Jaccard
Common Lisp’s Jaccard
1 (defun jaccard-index (a b)
2 (/
3 (length (intersection a b))
4 (length (union a b))))
Looks good, tastes great - turns out Common Lisp has a ’rational’type.What about equality of the members of ’a’ and ’b’? What if atype error lurks there?
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Comments
Both programs effectively implement the same approach.
Type errors in Lisp were caught at run-time; in Haskell theywere caught at compile time.
Haskell can deboilerplate using types; Common Lisp candeboilerplate using macros & run-time reflection
These statements can be generalized to their respectivefamilies.
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Summary 01/10
Functional programming is...
an alternative approach to program design
rooted in lambda calculus
oriented towards function-based abstractions
avoidant of side effects
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Summary 10/10
Practically, functional programming involves...
...learning new ways of thinking
...doing more with less code
...fewer bugs in different areas
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
What must I do to be functional?
Which concepts are “the biggest bang for the buck” for you?
Learn a tool that gets you those.
Then make a bigger bang
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
Questions
λ
Questions
?
Paul Nathan Practical Functional Programming
-5ptλ
Intro Definitions Features Example Wrap
References
http://james-iry.blogspot.com/2009/05/
brief-incomplete-and-mostly-wrong.html
http://fsharpforfunandprofit.com/
http://www.cse.chalmers.se/~rjmh/Papers/whyfp.pdf
http://www.manning.com/fogus2/JoC2e_meap_ch1.pdf
Paul Nathan Practical Functional Programming