Introduction to functional programming concepts and their application to data. Discuss pros/cons of a functional style. Discuss relationship between functional programming and the nature of the universe.
4. Definitions
Set: Collection of unique elements
Function: Rule that associates elements in one set with
those in another
α
β
γ
π
Thursday, October 31, 13
2
10. Algorithm is Irrelevant
•
Only the association/mapping matters
•
Thursday, October 31, 13
May need an algorithm to find the mapping
4
11. Algorithm is Irrelevant
•
Only the association/mapping matters
•
•
Once mapping is known, it can be used in place of
the algo without changing any behavior
•
•
Thursday, October 31, 13
May need an algorithm to find the mapping
This is “referential transparency”
Implies no side effects when algorithm is run
4
12. Algorithm is Irrelevant
•
Only the association/mapping matters
•
•
Once mapping is known, it can be used in place of
the algo without changing any behavior
•
•
•
This is “referential transparency”
Implies no side effects when algorithm is run
Example: digits of π
•
•
Thursday, October 31, 13
May need an algorithm to find the mapping
Algorithms exist for computing digits of π
But digits are predetermined by how π is defined
4
17. Algo is Irrelevant
•
Only the association/mapping matters
•
•
But once mapping is known, it can be used in place
of the algo without changing any behavior
•
•
•
This is “referential transparency”
Implies no side effects from algos used
Example: digits of π
•
•
Thursday, October 31, 13
May need an algorithm to find the mapping
Algorithms exist for computing digits of π
However, digits of π exist by definition of π
6
18. Algo is Irrelevant
•
Only the association/mapping matters
•
•
But once mapping is known, it can be used in place
of the algo without changing any behavior
•
•
•
This is “referential transparency”
Implies no side effects from algos used
Example: digits of π
•
•
Thursday, October 31, 13
May need an algorithm to find the mapping
Algorithms exist for computing digits of π
However, digits of π exist by definition of π
6
22. Multiple Arguments?
sliceOfPi start num -> Int
sliceOfPi 2 3 = 141
sliceOfPi 2 3 = firstNDigitsOfPiStartingAtDigit2 3 = 141
sliceOfPi 2 3 = f2 3 = 141
1
2
3
∶
start
Thursday, October 31, 13
f1
f2
f3
1
2
3
∶
1
2
∶
141
∶
num
Int
7
23. Multiple Arguments?
What if we called sliceOfPi with only one argument?
1
2
3
∶
Thursday, October 31, 13
f1
f2
f3
8
24. Multiple Arguments?
What if we called sliceOfPi with only one argument?
sliceOfPi 2 = f2
1
2
3
∶
Thursday, October 31, 13
f1
f2
f3
8
25. Multiple Arguments?
What if we called sliceOfPi with only one argument?
sliceOfPi 2 = f2
Partial function application
1
2
3
∶
Thursday, October 31, 13
f1
f2
f3
8
26. Multiple Arguments?
What if we called sliceOfPi with only one argument?
sliceOfPi 2 = f2
Partial function application
a.k.a. currying after Haskell Curry
1
2
3
∶
Thursday, October 31, 13
f1
f2
f3
8
27. Multiple Arguments?
This looks very similar to a unix pipeline
1
2
3
∶
Thursday, October 31, 13
f1
f2
f3
1
2
3
∶
1
2
∶
141
∶
9
28. Multiple Arguments?
This looks very similar to a unix pipeline
1
2
3
∶
Thursday, October 31, 13
stdin
f1
f2
f3
args
1
2
3
∶
stdout
1
2
∶
141
∶
9
30. Pipelines are Functional
Each step in a Unix pipeline is a function call:
ls . | grep r | grep i | grep n | grep o | wc -l
Thursday, October 31, 13
10
31. Pipelines are Functional
Each step in a Unix pipeline is a function call:
ls . | grep r | grep i | grep n | grep o | wc -l
ls
:: Dir
-> String
grep :: String -> String -> String
wc
:: String -> String
Thursday, October 31, 13
10
32. Pipelines are Functional
Each step in a Unix pipeline is a function call:
ls . | grep r | grep i | grep n | grep o | wc -l
ls
:: Dir
-> String
grep :: String -> String -> String
wc
:: String -> String
Pipelines are referentially transparent:
ls | grep r | grep i > value.txt
cat value.txt | grep n | grep o | wc -l
Thursday, October 31, 13
10
34. ETL is a pipeline
Extract | Transform | Load
Thursday, October 31, 13
11
35. ETL is a pipeline
Extract | Transform | Load
E
Thursday, October 31, 13
T
L
11
36. ETL is a pipeline
Extract | Transform | Load
E
T
L
Why not just Extract and Load?
Thursday, October 31, 13
11
37. ETL is a pipeline
Extract | Transform | Load
E
T
L
Why not just Extract and Load?
The transform step makes data more
regular, reducing the nondeterminism
in the input data sources.
Thursday, October 31, 13
11
42. Nondeterminism
Nondeterminism: randomness in input and output
digitOfPi :: Int -> Digit
n ∈ {1, 2, 4}
digitOfPi n ∈ {1, 3}
Thursday, October 31, 13
No uncertainty
Input uncertainty
Output uncertainty
12
43. Nondeterminism
Nondeterminism: randomness in input and output
digitOfPi :: Int -> Digit
No uncertainty
n ∈ {1, 2, 4}
digitOfPi n ∈ {1, 3}
Input uncertainty
Output uncertainty
What is the result? 1 or 3?
Thursday, October 31, 13
12
44. Nondeterminism
Nondeterminism: randomness in input and output
digitOfPi :: Int -> Digit
No uncertainty
n ∈ {1, 2, 4}
digitOfPi n ∈ {1, 3}
Input uncertainty
Output uncertainty
What is the result? 1 or 3?
Domain of
s
uantum computer
q
Thursday, October 31, 13
12
50. Deepank’s Roulette
•
•
•
Deepank is in a room with a roulette wheel
Bets on red
Spins wheel
•
Thursday, October 31, 13
If lands on red, he takes his winnings home
13
51. Deepank’s Roulette
•
•
•
Deepank is in a room with a roulette wheel
Bets on red
Spins wheel
•
•
Thursday, October 31, 13
If lands on red, he takes his winnings home
If lands on black,
13
52. Deepank’s Roulette
•
•
•
Deepank is in a room with a roulette wheel
Bets on red
Spins wheel
•
•
Thursday, October 31, 13
If lands on red, he takes his winnings home
If lands on black, he dies
13
53. Deepank’s Roulette
•
•
•
Deepank is in a room with a roulette wheel
Bets on red
Spins wheel
•
•
•
Thursday, October 31, 13
If lands on red, he takes his winnings home
If lands on black, he dies
Close door and spin wheel
13
54. Deepank’s Roulette
•
•
•
Deepank is in a room with a roulette wheel
Bets on red
Spins wheel
•
•
If lands on red, he takes his winnings home
If lands on black, he dies
•
•
Thursday, October 31, 13
Close door and spin wheel
Is Deepank richer or dead?
13
55. Deepank’s Roulette
•
•
•
Deepank is in a room with a roulette wheel
Bets on red
Spins wheel
•
•
•
If lands on red, he takes his winnings home
If lands on black, he dies
Close door and spin wheel
$$
•
Thursday, October 31, 13
Is Deepank richer or dead?
13
56. Deepank’s Roulette
•
•
•
Deepank is in a room with a roulette wheel
Bets on red
Spins wheel
•
•
•
If lands on red, he takes his winnings home
If lands on black, he dies
Close door and spin wheel
•
Thursday, October 31, 13
xx
(
$$
Is Deepank richer or dead?
13
57. Deepank’s Roulette
•
•
•
Deepank is in a room with a roulette wheel
Bets on red
Spins wheel
•
•
•
If lands on red, he takes his winnings home
If lands on black, he dies
Close door and spin wheel
•
Is Deepank richer or dead?
•
Thursday, October 31, 13
xx
(
$$
Both − depends on the universe
13
59. Multiple Universes
In any given universe, everything
that has ever happened or ever
will happen exists independently
and unchangeable.
Thursday, October 31, 13
14
60. Multiple Universes
In any given universe, everything
that has ever happened or ever
will happen exists independently
and unchangeable.
Just like the digits of pi
3.141592653589793238462643383279502884197169399375
10582097494459230781640628620899862803482534211706
79821480865132823066470938446095505822317253594081
28481117450284102701938521105559644622948954930381
96442881097566593344612847564823378678316527120190
91456485669234603486104543266482133936072602491412
73724587006606315588174881520920962829254091715364
36789259036001133053054882046652138414695194151160
94330572703657595919530921861173819326117931051185
Thursday, October 31, 13
14
62. Fate and π
Conjecture: π contains all finite sequences of digits.
Thursday, October 31, 13
15
63. Fate and π
Conjecture: π contains all finite sequences of digits.
Profound if true
Thursday, October 31, 13
15
64. Fate and π
Conjecture: π contains all finite sequences of digits.
Profound if true
π contains...
Thursday, October 31, 13
15
65. Fate and π
Conjecture: π contains all finite sequences of digits.
Profound if true
π contains...
• ...a transcript of everything you will ever say
Thursday, October 31, 13
15
66. Fate and π
Conjecture: π contains all finite sequences of digits.
Profound if true
π contains...
• ...a transcript of everything you will ever say
• ...a tarball of next week’s mobile source code
Thursday, October 31, 13
15
67. Fate and π
Conjecture: π contains all finite sequences of digits.
Profound if true
π contains...
• ...a transcript of everything you will ever say
• ...a tarball of next week’s mobile source code
• ...a video of you with thought bubbles...in Spanish
Thursday, October 31, 13
15
68. Fate and π
Conjecture: π contains all finite sequences of digits.
Profound if true
π contains...
• ...a transcript of everything you will ever say
• ...a tarball of next week’s mobile source code
• ...a video of you with thought bubbles...in Spanish
• ...all human knowledge and all variations of it
Thursday, October 31, 13
15
69. Fate and π
Conjecture: π contains all finite sequences of digits.
Profound if true
π contains...
• ...a transcript of everything you will ever say
• ...a tarball of next week’s mobile source code
• ...a video of you with thought bubbles...in Spanish
• ...all human knowledge and all variations of it
If true, our existence is probably part of an algorithm
to compute the digits of π.
Thursday, October 31, 13
15
70. Fate and π
Conjecture: π contains all finite sequences of digits.
Profound if true
π contains...
• ...a transcript of everything you will ever say
• ...a tarball of next week’s mobile source code
• ...a video of you with thought bubbles...in Spanish
• ...all human knowledge and all variations of it
If true, our existence is probably part of an algorithm
to compute the digits of π.
Thursday, October 31, 13
15
72. Free Will
Thursday, October 31, 13
$$
xx
(
Multiverse Theory
There is a universe associated
with every combination of
nondeterminism.
16
73. Free Will
$$
xx
(
Multiverse Theory
There is a universe associated
with every combination of
nondeterminism.
Terminator principle
There is no fate but
what we make.
Thursday, October 31, 13
16
74. Free Will
$$
xx
(
Multiverse Theory
There is a universe associated
with every combination of
nondeterminism.
Terminator principle
There is no fate but
what we make.
Make good choices
We select our universe
by making choices.
Thursday, October 31, 13
16
75. Free Will
$$
xx
(
Multiverse Theory
There is a universe associated
with every combination of
nondeterminism.
Terminator principle
There is no fate but
what we make.
Make good choices
We select our universe
by making choices.
This may also
xplain jinxes
e
Thursday, October 31, 13
16
76. Nondeterminism
Nondeterminism: randomness in input and output
digitOfPi :: Int -> Digit
No uncertainty
n ∈ {1, 2, 4}
digitOfPi n ∈ {1, 3}
Input uncertainty
Output uncertainty
What is the result? 1 or 3?
se case for
U
computers
quantum
Thursday, October 31, 13
17
79. Nondeterminism in SW
Nondeterminism makes testing and debugging harder
• More scenarios to set up
• More cases to check
• More universes to consider
Thursday, October 31, 13
18
80. Nondeterminism in SW
Nondeterminism makes testing and debugging harder
• More scenarios to set up
• More cases to check
• More universes to consider
Sources of nondeterminism
Thursday, October 31, 13
18
81. Nondeterminism in SW
Nondeterminism makes testing and debugging harder
• More scenarios to set up
• More cases to check
• More universes to consider
Sources of nondeterminism
• Side effects
• Un-DRY code
• Breaking “Law of Demeter”
• Irregular return values
Thursday, October 31, 13
18
82. Nondeterminism in SW
Nondeterminism makes testing and debugging harder
• More scenarios to set up
• More cases to check
• More universes to consider
Sources of nondeterminism
• Side effects
• Un-DRY code
• Breaking “Law of Demeter”
• Irregular return values
Ask Yourself
Is this function increasing or
reducing nondeterminism?
Thursday, October 31, 13
18
86. Reduce Nondeterminism
•Use ETL as a model
•Make functions referentially transparent
•Isolate code that changes state
Thursday, October 31, 13
19
87. Reduce Nondeterminism
•Use ETL as a model
•Make functions referentially transparent
•Isolate code that changes state
•Ensure function results are regular
• Use placeholders so callers won’t need to check
• Keep the result set as unvaried as possible
• Unleash your unit tests here
Thursday, October 31, 13
19
88. Reduce Nondeterminism
•Use ETL as a model
•Make functions referentially transparent
•Isolate code that changes state
•Ensure function results are regular
• Use placeholders so callers won’t need to check
• Keep the result set as unvaried as possible
• Unleash your unit tests here
Reducing nondeterminism lets you make
stronger assumptions about the code.
Thursday, October 31, 13
19
101. Takeaway Thoughts
•
•
Make good choices to select good universes
•
Thursday, October 31, 13
Use functional ideas to reduce nondeterminism
Don’t play roulette with Deepank
21