Fate and functional programming

103
Fate Free will Functional πrogramming Rino Jose 1 Thursday, October 31, 13

Transcript of Fate and functional programming

Page 1: Fate and functional programming

FateFree will

Functional πrogramming

Rino Jose

1Thursday, October 31, 13

Page 2: Fate and functional programming

Definitions

2Thursday, October 31, 13

Page 3: Fate and functional programming

DefinitionsSet: Collection of unique elements

2Thursday, October 31, 13

Page 4: Fate and functional programming

DefinitionsSet: Collection of unique elements

Function: Rule that associates elements in one set with those in another

α

β γπ

2Thursday, October 31, 13

Page 5: Fate and functional programming

Function: digitOfPi

3Thursday, October 31, 13

Page 6: Fate and functional programming

Function: digitOfPidigitOfPi :: Int -> Digit

3Thursday, October 31, 13

Page 7: Fate and functional programming

Function: digitOfPidigitOfPi :: Int -> Digit

0123456789

1234∶

3Thursday, October 31, 13

Page 8: Fate and functional programming

Function: digitOfPidigitOfPi :: Int -> Digit

0123456789

1234∶

3Thursday, October 31, 13

Page 9: Fate and functional programming

Algorithm is Irrelevant

4Thursday, October 31, 13

Page 10: Fate and functional programming

Algorithm is Irrelevant

• Only the association/mapping matters

• May need an algorithm to find the mapping

4Thursday, October 31, 13

Page 11: Fate and functional programming

Algorithm is Irrelevant

• Only the association/mapping matters

• May need an algorithm to find the mapping

• 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

4Thursday, October 31, 13

Page 12: Fate and functional programming

Algorithm is Irrelevant

• Only the association/mapping matters

• May need an algorithm to find the mapping

• 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 π

• Algorithms exist for computing digits of π

• But digits are predetermined by how π is defined

4Thursday, October 31, 13

Page 13: Fate and functional programming

π?

5Thursday, October 31, 13

Page 14: Fate and functional programming

π?

5Thursday, October 31, 13

Page 15: Fate and functional programming

π?

5Thursday, October 31, 13

Page 16: Fate and functional programming

π?

π = circumference

diameter

5Thursday, October 31, 13

Page 17: Fate and functional programming

Algo is Irrelevant• Only the association/mapping matters

• May need an algorithm to find the mapping

• 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 π

• Algorithms exist for computing digits of π

• However, digits of π exist by definition of π

6Thursday, October 31, 13

Page 18: Fate and functional programming

Algo is Irrelevant• Only the association/mapping matters

• May need an algorithm to find the mapping

• 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 π

• Algorithms exist for computing digits of π

• However, digits of π exist by definition of π

6Thursday, October 31, 13

Page 19: Fate and functional programming

Multiple Arguments?

7Thursday, October 31, 13

Page 20: Fate and functional programming

Multiple Arguments?sliceOfPi start num -> IntsliceOfPi 2 3 = 141

7Thursday, October 31, 13

Page 21: Fate and functional programming

Multiple Arguments?sliceOfPi start num -> IntsliceOfPi 2 3 = 141

123∶

123∶

12∶141∶

start num Int

7Thursday, October 31, 13

Page 22: Fate and functional programming

Multiple Arguments?sliceOfPi start num -> IntsliceOfPi 2 3 = 141

sliceOfPi 2 3 = firstNDigitsOfPiStartingAtDigit2 3 = 141sliceOfPi 2 3 = f2 3 = 141

f1f2f3

123∶

123∶

12∶141∶

start num Int

7Thursday, October 31, 13

Page 23: Fate and functional programming

What if we called sliceOfPi with only one argument?

f1f2f3

123∶

Multiple Arguments?

8Thursday, October 31, 13

Page 24: Fate and functional programming

sliceOfPi 2 = f2

What if we called sliceOfPi with only one argument?

f1f2f3

123∶

Multiple Arguments?

8Thursday, October 31, 13

Page 25: Fate and functional programming

sliceOfPi 2 = f2

What if we called sliceOfPi with only one argument?

f1f2f3

123∶

Partial function application

Multiple Arguments?

8Thursday, October 31, 13

Page 26: Fate and functional programming

sliceOfPi 2 = f2

What if we called sliceOfPi with only one argument?

f1f2f3

123∶

Partial function application

a.k.a. currying after Haskell Curry

Multiple Arguments?

8Thursday, October 31, 13

Page 27: Fate and functional programming

f1f2f3

123∶

123∶

12∶141∶

This looks very similar to a unix pipeline

Multiple Arguments?

9Thursday, October 31, 13

Page 28: Fate and functional programming

f1f2f3

123∶

123∶

12∶141∶

This looks very similar to a unix pipeline

stdin

stdout

args

Multiple Arguments?

9Thursday, October 31, 13

Page 29: Fate and functional programming

Pipelines are Functional

10Thursday, October 31, 13

Page 30: Fate and functional programming

Pipelines are Functional

Each step in a Unix pipeline is a function call:ls . | grep r | grep i | grep n | grep o | wc -l

10Thursday, October 31, 13

Page 31: Fate and functional programming

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 -> Stringgrep :: String -> String -> Stringwc :: String -> String

10Thursday, October 31, 13

Page 32: Fate and functional programming

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 -> Stringgrep :: String -> String -> Stringwc :: String -> String

Pipelines are referentially transparent: ls | grep r | grep i > value.txt cat value.txt | grep n | grep o | wc -l

10Thursday, October 31, 13

Page 33: Fate and functional programming

ETL is a pipeline

11Thursday, October 31, 13

Page 34: Fate and functional programming

ETL is a pipelineExtract | Transform | Load

11Thursday, October 31, 13

Page 35: Fate and functional programming

ETL is a pipelineExtract | Transform | Load

E T L

11Thursday, October 31, 13

Page 36: Fate and functional programming

ETL is a pipelineExtract | Transform | Load

Why not just Extract and Load?

E T L

11Thursday, October 31, 13

Page 37: Fate and functional programming

ETL is a pipelineExtract | Transform | Load

Why not just Extract and Load?

The transform step makes data more regular, reducing the nondeterminism in the input data sources.

E T L

11Thursday, October 31, 13

Page 38: Fate and functional programming

Nondeterminism

12Thursday, October 31, 13

Page 39: Fate and functional programming

NondeterminismNondeterminism: randomness in input and output

12Thursday, October 31, 13

Page 40: Fate and functional programming

NondeterminismNondeterminism: randomness in input and output

digitOfPi :: Int -> Digit No uncertainty

12Thursday, October 31, 13

Page 41: Fate and functional programming

NondeterminismNondeterminism: randomness in input and output

digitOfPi :: Int -> Digit No uncertainty

n ∈ {1, 2, 4} Input uncertainty

12Thursday, October 31, 13

Page 42: Fate and functional programming

NondeterminismNondeterminism: randomness in input and output

digitOfPi :: Int -> Digit No uncertainty

n ∈ {1, 2, 4} Input uncertainty

Output uncertaintydigitOfPi n ∈ {1, 3}

12Thursday, October 31, 13

Page 43: Fate and functional programming

Nondeterminism

What is the result? 1 or 3?

Nondeterminism: randomness in input and output

digitOfPi :: Int -> Digit No uncertainty

n ∈ {1, 2, 4} Input uncertainty

Output uncertaintydigitOfPi n ∈ {1, 3}

12Thursday, October 31, 13

Page 44: Fate and functional programming

Nondeterminism

What is the result? 1 or 3?

Nondeterminism: randomness in input and output

digitOfPi :: Int -> Digit No uncertainty

Domain of

quantum computers

n ∈ {1, 2, 4} Input uncertainty

Output uncertaintydigitOfPi n ∈ {1, 3}

12Thursday, October 31, 13

Page 45: Fate and functional programming

Deepank’s Roulette

13Thursday, October 31, 13

Page 46: Fate and functional programming

Deepank’s Roulette• Deepank is in a room with a roulette wheel

13Thursday, October 31, 13

Page 47: Fate and functional programming

Deepank’s Roulette• Deepank is in a room with a roulette wheel

• Bets on red

13Thursday, October 31, 13

Page 48: Fate and functional programming

Deepank’s Roulette• Deepank is in a room with a roulette wheel

• Bets on red

• Spins wheel

13Thursday, October 31, 13

Page 49: Fate and functional programming

Deepank’s Roulette• Deepank is in a room with a roulette wheel

• Bets on red

• Spins wheel

• If lands on red,

13Thursday, October 31, 13

Page 50: Fate and functional programming

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

13Thursday, October 31, 13

Page 51: Fate and functional programming

Deepank’s Roulette• Deepank is in a room with a roulette wheel

• Bets on red

• Spins wheel

• If lands on red,

• If lands on black,

he takes his winnings home

13Thursday, October 31, 13

Page 52: Fate and functional programming

Deepank’s Roulette• Deepank is in a room with a roulette wheel

• Bets on red

• Spins wheel

• If lands on red,

• If lands on black,

he takes his winnings home

he dies

13Thursday, October 31, 13

Page 53: Fate and functional programming

Deepank’s Roulette• Deepank is in a room with a roulette wheel

• Bets on red

• Spins wheel

• If lands on red,

• If lands on black,

• Close door and spin wheel

he takes his winnings home

he dies

13Thursday, October 31, 13

Page 54: Fate and functional programming

Deepank’s Roulette• Deepank is in a room with a roulette wheel

• Bets on red

• Spins wheel

• If lands on red,

• If lands on black,

• Close door and spin wheel

• Is Deepank richer or dead?

he takes his winnings home

he dies

13Thursday, October 31, 13

Page 55: Fate and functional programming

Deepank’s Roulette• Deepank is in a room with a roulette wheel

• Bets on red

• Spins wheel

• If lands on red,

• If lands on black,

• Close door and spin wheel

• Is Deepank richer or dead?$$

he takes his winnings home

he dies

13Thursday, October 31, 13

Page 56: Fate and functional programming

Deepank’s Roulette• Deepank is in a room with a roulette wheel

• Bets on red

• Spins wheel

• If lands on red,

• If lands on black,

• Close door and spin wheel

• Is Deepank richer or dead?$$ xx(

he takes his winnings home

he dies

13Thursday, October 31, 13

Page 57: Fate and functional programming

Deepank’s Roulette• Deepank is in a room with a roulette wheel

• Bets on red

• Spins wheel

• If lands on red,

• If lands on black,

• Close door and spin wheel

• Is Deepank richer or dead?

• Both − depends on the universe

$$ xx(

he takes his winnings home

he dies

13Thursday, October 31, 13

Page 58: Fate and functional programming

Multiple Universes

14Thursday, October 31, 13

Page 59: Fate and functional programming

Multiple UniversesIn any given universe, everything that has ever happened or ever

will happen exists independently and unchangeable.

14Thursday, October 31, 13

Page 60: Fate and functional programming

Multiple UniversesIn any given universe, everything that has ever happened or ever

will happen exists independently and unchangeable.

Just like the digits of pi

3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198938095257201065485863278865936153381827968230301952035301852968995773622599413891249721775283479131515574857242454150695950829533116861727855889075098381754637464939319255060400927701671139009848824012858361603563707660104710181942955596198946767837449448255379774726847104047534646208046684259069491293313677028989152104752162056966024058038150193511253382430035587640247496473263914199272604269927967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938008164706001614524919217321721477235014144197356854816136115735255213347574184946843852332390739414334547762416862 518983569485562099219222184272550254256887671790494601653466804988627232791786085784383827967976681454100953883786360950680064225125205117392984896084128488 626945604241965285022210661186306744278622039194945047123713786960956364371917 287467764657573962413890865832645995813390478027590099465764078951269468398352 595709825822620522489407726719478268482601476990902640136394437455305068203496 252451749399651431429809190659250937221696461515709858387410597885959772975498 930161753928468138268683868942774155991855925

14Thursday, October 31, 13

Page 61: Fate and functional programming

Fate and π

15Thursday, October 31, 13

Page 62: Fate and functional programming

Fate and πConjecture: π contains all finite sequences of digits.

15Thursday, October 31, 13

Page 63: Fate and functional programming

Fate and πConjecture: π contains all finite sequences of digits.

Profound if true

15Thursday, October 31, 13

Page 64: Fate and functional programming

Fate and πConjecture: π contains all finite sequences of digits.

Profound if true

π contains...

15Thursday, October 31, 13

Page 65: Fate and functional programming

Fate and πConjecture: π contains all finite sequences of digits.

Profound if true

• ...a transcript of everything you will ever sayπ contains...

15Thursday, October 31, 13

Page 66: Fate and functional programming

Fate and πConjecture: π contains all finite sequences of digits.

Profound if true

• ...a transcript of everything you will ever say• ...a tarball of next week’s mobile source code

π contains...

15Thursday, October 31, 13

Page 67: Fate and functional programming

Fate and πConjecture: π contains all finite sequences of digits.

Profound if true

• ...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

π contains...

15Thursday, October 31, 13

Page 68: Fate and functional programming

Fate and πConjecture: π contains all finite sequences of digits.

Profound if true

• ...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

π contains...

15Thursday, October 31, 13

Page 69: Fate and functional programming

Fate and πConjecture: π contains all finite sequences of digits.

Profound if true

• ...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 π.

π contains...

15Thursday, October 31, 13

Page 70: Fate and functional programming

Fate and πConjecture: π contains all finite sequences of digits.

Profound if true

• ...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 π.

π contains...

15Thursday, October 31, 13

Page 71: Fate and functional programming

Free Will

16Thursday, October 31, 13

Page 72: Fate and functional programming

Free WillMultiverse Theory

There is a universe associated with every combination of

nondeterminism.

$$

xx(

16Thursday, October 31, 13

Page 73: Fate and functional programming

Free WillMultiverse Theory

There is a universe associated with every combination of

nondeterminism.

Terminator principleThere is no fate but

what we make.

$$

xx(

16Thursday, October 31, 13

Page 74: Fate and functional programming

Free WillMultiverse Theory

There is a universe associated with every combination of

nondeterminism.

Make good choicesWe select our universe

by making choices.

Terminator principleThere is no fate but

what we make.

$$

xx(

16Thursday, October 31, 13

Page 75: Fate and functional programming

Free WillMultiverse Theory

There is a universe associated with every combination of

nondeterminism.

Make good choicesWe select our universe

by making choices.

This may also

explain jinxes

Terminator principleThere is no fate but

what we make.

$$

xx(

16Thursday, October 31, 13

Page 76: Fate and functional programming

Nondeterminism

What is the result? 1 or 3?

Nondeterminism: randomness in input and output

digitOfPi :: Int -> Digit No uncertainty

Use case for

quantum computers

n ∈ {1, 2, 4} Input uncertainty

Output uncertaintydigitOfPi n ∈ {1, 3}

17Thursday, October 31, 13

Page 77: Fate and functional programming

Nondeterminism in SW

18Thursday, October 31, 13

Page 78: Fate and functional programming

Nondeterminism in SWNondeterminism makes testing and debugging harder

18Thursday, October 31, 13

Page 79: Fate and functional programming

Nondeterminism in SWNondeterminism makes testing and debugging harder

• More scenarios to set up• More cases to check• More universes to consider

18Thursday, October 31, 13

Page 80: Fate and functional programming

Nondeterminism in SWNondeterminism makes testing and debugging harder

Sources of nondeterminism

• More scenarios to set up• More cases to check• More universes to consider

18Thursday, October 31, 13

Page 81: Fate and functional programming

Nondeterminism in SWNondeterminism makes testing and debugging harder

• Side effects• Un-DRY code• Breaking “Law of Demeter”• Irregular return values

Sources of nondeterminism

• More scenarios to set up• More cases to check• More universes to consider

18Thursday, October 31, 13

Page 82: Fate and functional programming

Nondeterminism in SWNondeterminism makes testing and debugging harder

• Side effects• Un-DRY code• Breaking “Law of Demeter”• Irregular return values

Ask YourselfIs this function increasing or reducing nondeterminism?

Sources of nondeterminism

• More scenarios to set up• More cases to check• More universes to consider

18Thursday, October 31, 13

Page 83: Fate and functional programming

Reduce Nondeterminism

19Thursday, October 31, 13

Page 84: Fate and functional programming

Reduce Nondeterminism

•Use ETL as a model

19Thursday, October 31, 13

Page 85: Fate and functional programming

Reduce Nondeterminism

•Use ETL as a model

•Make functions referentially transparent

19Thursday, October 31, 13

Page 86: Fate and functional programming

Reduce Nondeterminism

•Use ETL as a model

•Make functions referentially transparent

•Isolate code that changes state

19Thursday, October 31, 13

Page 87: Fate and functional programming

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

19Thursday, October 31, 13

Page 88: Fate and functional programming

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.

19Thursday, October 31, 13

Page 89: Fate and functional programming

Databending

20Thursday, October 31, 13

Page 90: Fate and functional programming

Databending

20Thursday, October 31, 13

Page 91: Fate and functional programming

Databending

ExtractRaw Data

20Thursday, October 31, 13

Page 92: Fate and functional programming

Databending

ExtractRaw Data

ConditionData

20Thursday, October 31, 13

Page 93: Fate and functional programming

Databending

ExtractRaw Data

ConditionData

BendData

20Thursday, October 31, 13

Page 94: Fate and functional programming

Databending

ExtractRaw Data

ConditionData

BendData

LoadData

20Thursday, October 31, 13

Page 95: Fate and functional programming

Databending

ExtractRaw Data

ConditionData

BendData

LoadData

Snapshot Service

• Data is snapshotted

20Thursday, October 31, 13

Page 96: Fate and functional programming

Databending

ExtractRaw Data

ConditionData

BendData

LoadData

Snapshot Service

• Data is snapshotted• Stages provide robustness

20Thursday, October 31, 13

Page 97: Fate and functional programming

Databending

ExtractRaw Data

ConditionData

BendData

LoadData

Snapshot Service

• Data is snapshotted• Stages provide robustness• Nondeterminism decreases

20Thursday, October 31, 13

Page 98: Fate and functional programming

Takeaway Thoughts

21Thursday, October 31, 13

Page 99: Fate and functional programming

Takeaway Thoughts• Use functional ideas to reduce nondeterminism

21Thursday, October 31, 13

Page 100: Fate and functional programming

Takeaway Thoughts• Use functional ideas to reduce nondeterminism

• Make good choices to select good universes

21Thursday, October 31, 13

Page 101: Fate and functional programming

Takeaway Thoughts• Use functional ideas to reduce nondeterminism

• Make good choices to select good universes

• Don’t play roulette with Deepank

21Thursday, October 31, 13

Page 102: Fate and functional programming

22Thursday, October 31, 13

Page 103: Fate and functional programming

uestions?

22Thursday, October 31, 13