Upcoming SlideShare
×

# Fate and functional programming

323 views
263 views

Published on

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.

Published in: Technology
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
323
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
2
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Fate and functional programming

1. 1. Fate Free will Functional πrogramming Rino Jose Thursday, October 31, 13 1
2. 2. Deﬁnitions Thursday, October 31, 13 2
3. 3. Deﬁnitions Set: Collection of unique elements Thursday, October 31, 13 2
4. 4. Deﬁnitions Set: Collection of unique elements Function: Rule that associates elements in one set with those in another α β γ π Thursday, October 31, 13 2
5. 5. Function: digitOfPi Thursday, October 31, 13 3
6. 6. Function: digitOfPi digitOfPi :: Int -> Digit Thursday, October 31, 13 3
7. 7. Function: digitOfPi digitOfPi :: Int -> Digit 1 2 3 4 ∶ Thursday, October 31, 13 0 1 2 3 4 5 6 7 8 9 3
8. 8. Function: digitOfPi digitOfPi :: Int -> Digit 1 2 3 4 ∶ Thursday, October 31, 13 0 1 2 3 4 5 6 7 8 9 3
9. 9. Algorithm is Irrelevant Thursday, October 31, 13 4
10. 10. Algorithm is Irrelevant • Only the association/mapping matters • Thursday, October 31, 13 May need an algorithm to ﬁnd the mapping 4
11. 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 ﬁnd the mapping This is “referential transparency” Implies no side effects when algorithm is run 4
12. 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 ﬁnd the mapping Algorithms exist for computing digits of π But digits are predetermined by how π is deﬁned 4
13. 13. π? Thursday, October 31, 13 5
14. 14. π? Thursday, October 31, 13 5
15. 15. π? Thursday, October 31, 13 5
16. 16. π? π= Thursday, October 31, 13 circumference diameter 5
17. 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 ﬁnd the mapping Algorithms exist for computing digits of π However, digits of π exist by deﬁnition of π 6
18. 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 ﬁnd the mapping Algorithms exist for computing digits of π However, digits of π exist by deﬁnition of π 6
19. 19. Multiple Arguments? Thursday, October 31, 13 7
20. 20. Multiple Arguments? sliceOfPi start num -> Int sliceOfPi 2 3 = 141 Thursday, October 31, 13 7
21. 21. Multiple Arguments? sliceOfPi start num -> Int sliceOfPi 2 3 = 141 1 2 3 ∶ 1 2 ∶ 141 ∶ start Thursday, October 31, 13 1 2 3 ∶ num Int 7
22. 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. 23. Multiple Arguments? What if we called sliceOfPi with only one argument? 1 2 3 ∶ Thursday, October 31, 13 f1 f2 f3 8
24. 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. 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. 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. 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. 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
29. 29. Pipelines are Functional Thursday, October 31, 13 10
30. 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. 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. 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
33. 33. ETL is a pipeline Thursday, October 31, 13 11
34. 34. ETL is a pipeline Extract | Transform | Load Thursday, October 31, 13 11
35. 35. ETL is a pipeline Extract | Transform | Load E Thursday, October 31, 13 T L 11
36. 36. ETL is a pipeline Extract | Transform | Load E T L Why not just Extract and Load? Thursday, October 31, 13 11
37. 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
38. 38. Nondeterminism Thursday, October 31, 13 12
39. 39. Nondeterminism Nondeterminism: randomness in input and output Thursday, October 31, 13 12
40. 40. Nondeterminism Nondeterminism: randomness in input and output digitOfPi :: Int -> Digit Thursday, October 31, 13 No uncertainty 12
41. 41. Nondeterminism Nondeterminism: randomness in input and output digitOfPi :: Int -> Digit n ∈ {1, 2, 4} Thursday, October 31, 13 No uncertainty Input uncertainty 12
42. 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. 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. 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
45. 45. Deepank’s Roulette Thursday, October 31, 13 13
46. 46. Deepank’s Roulette • Thursday, October 31, 13 Deepank is in a room with a roulette wheel 13
47. 47. Deepank’s Roulette • • Thursday, October 31, 13 Deepank is in a room with a roulette wheel Bets on red 13
48. 48. Deepank’s Roulette • • • Thursday, October 31, 13 Deepank is in a room with a roulette wheel Bets on red Spins wheel 13
49. 49. 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, 13
50. 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. 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. 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. 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. 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. 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. 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. 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
58. 58. Multiple Universes Thursday, October 31, 13 14
59. 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. 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
61. 61. Fate and π Thursday, October 31, 13 15
62. 62. Fate and π Conjecture: π contains all ﬁnite sequences of digits. Thursday, October 31, 13 15
63. 63. Fate and π Conjecture: π contains all ﬁnite sequences of digits. Profound if true Thursday, October 31, 13 15
64. 64. Fate and π Conjecture: π contains all ﬁnite sequences of digits. Profound if true π contains... Thursday, October 31, 13 15
65. 65. Fate and π Conjecture: π contains all ﬁnite sequences of digits. Profound if true π contains... • ...a transcript of everything you will ever say Thursday, October 31, 13 15
66. 66. Fate and π Conjecture: π contains all ﬁnite 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. 67. Fate and π Conjecture: π contains all ﬁnite 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. 68. Fate and π Conjecture: π contains all ﬁnite 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. 69. Fate and π Conjecture: π contains all ﬁnite 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. 70. Fate and π Conjecture: π contains all ﬁnite 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
71. 71. Free Will Thursday, October 31, 13 16
72. 72. Free Will Thursday, October 31, 13 \$\$ xx ( Multiverse Theory There is a universe associated with every combination of nondeterminism. 16
73. 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. 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. 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. 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
77. 77. Nondeterminism in SW Thursday, October 31, 13 18
78. 78. Nondeterminism in SW Nondeterminism makes testing and debugging harder Thursday, October 31, 13 18
79. 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. 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. 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. 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
83. 83. Reduce Nondeterminism Thursday, October 31, 13 19
84. 84. Reduce Nondeterminism •Use ETL as a model Thursday, October 31, 13 19
85. 85. Reduce Nondeterminism •Use ETL as a model •Make functions referentially transparent Thursday, October 31, 13 19
86. 86. Reduce Nondeterminism •Use ETL as a model •Make functions referentially transparent •Isolate code that changes state Thursday, October 31, 13 19
87. 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. 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
89. 89. Databending Thursday, October 31, 13 20
90. 90. Databending Thursday, October 31, 13 20
91. 91. Databending Extract Raw Data Thursday, October 31, 13 20
92. 92. Databending Extract Raw Data Condition Data Thursday, October 31, 13 20
93. 93. Databending Bend Data Extract Raw Data Condition Data Thursday, October 31, 13 20
94. 94. Databending Load Data Bend Data Extract Raw Data Condition Data Thursday, October 31, 13 20
95. 95. Databending Load Data Bend Data Extract Raw Data Condition Data Snapshot Service • Data is snapshotted Thursday, October 31, 13 20
96. 96. Databending Load Data Bend Data Extract Raw Data Condition Data Snapshot Service • Data is snapshotted • Stages provide robustness Thursday, October 31, 13 20
97. 97. Databending Load Data Bend Data Extract Raw Data Condition Data Snapshot Service • Data is snapshotted • Stages provide robustness • Nondeterminism decreases Thursday, October 31, 13 20
98. 98. Takeaway Thoughts Thursday, October 31, 13 21
99. 99. Takeaway Thoughts • Thursday, October 31, 13 Use functional ideas to reduce nondeterminism 21
100. 100. Takeaway Thoughts • • Thursday, October 31, 13 Use functional ideas to reduce nondeterminism Make good choices to select good universes 21
101. 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
102. 102. Thursday, October 31, 13 22
103. 103. uestions? Thursday, October 31, 13 22