SlideShare a Scribd company logo
data made
out of
functions
#ylj2016
@KenScambler
λλλλλ
λλ λλ
λ λ
λ λ
λ λ λ λ
λ λ
λ λ λ λ
λ λλλλ λ
λ λ
λλ λλ
λλλλλ
For faster
monads!
Diogenes of Sinope
412 – 323 BC
Diogenes of Sinope
412 – 323 BC
• Simplest man of all time
• Obnoxious hobo
• Lived in a barrel
I’ve been using this bowl
like a sucker!
Data made out of functions
Data made out of functions
Um….
what
"abcd"
IF x THEN y ELSE z
WHILE cond {…}
[a, b, c, d]
BOOL
INT
STRUCT {
fields…
}
λa -> b
IF x THEN y ELSE z
WHILE cond {…}
[a, b, c, d]
BOOL
INT
STRUCT {
fields…
}
λa -> b
Strings are
pretty much
arrays
IF x THEN y ELSE z
[a, b, c, d]
BOOL
INT
STRUCT {
fields…
}
λa -> b
Recursion can
do loops
IF x THEN y ELSE z
[a,b,c,d]
BOOL
INT
STRUCT {
fields…
}
λa -> b
Recursive data
structures can do
lists
IF x THEN y ELSE z
[a,b,c,d]
INT
STRUCT {
fields…
}
λa -> b
Ints can do bools
IF x THEN y ELSE z
[a,b,c,d]
INT
STRUCT {
fields…
}
λa -> b
[a,b,c,d]
STRUCT
λa -> b
Alonzo Church
1903 - 1995
λa -> b
Lambda calculus
λa -> b
Alonzo Church
1903 - 1995
Lambda calculus
We can make any
data structure out of
functions!
Church encoding
Booleans
Bool
Church booleans
resultBool
Church booleans
resultBool
If we define everything you
can do with a structure, isn’t
that the same as defining
the structure itself?
TRUE
FALSE
or
TRUE
FALSE
or
result
“What we do if
it’s true”
“What we do if
it’s false”
TRUE
FALSE
or
result
TRUE
FALSE
or
result
result
result
()
()
result
result
result
result
result
result
r r r
The Church encoding
of a boolean is:
type CBool = forall r. r -> r -> r
cTrue :: CBool
cTrue x y = x
cFalse :: CBool
cFalse x y = y
cNot :: CBool -> CBool
cNot cb = cb cFalse cTrue
cAnd :: CBool -> CBool -> CBool
cAnd cb1 cb2 = cb1 cb2 cFalse
cOr :: CBool -> CBool -> CBool
cOr cb1 cb2 = cb1 cTrue cb2
Natural numbers
0
1
2
3
4
…
Natural numbers
0
0 +1
0 +1 +1
0 +1 +1 +1
0 +1 +1 +1 +1
…
Natural numbers
0
0 +1
0 +1 +1
0 +1 +1 +1
0 +1 +1 +1 +1
…
Giuseppe Peano
1858 - 1932
Natural
numbers form
a data
structure!
Zero
Succ(Nat)
or
Nat =
Natural Peano numbers
Giuseppe Peano
1858 - 1932
or
Nat =
Now lets turn it
into functions!
Zero
Succ(Nat)
Zero
Succ(Nat)
or
result
“If it’s a successor”
or “If it’s zero”
resultZero
Succ(Nat)
result
result
resultor
Zero
Succ(Nat)
Nat
()
result
result
result
Nat result
result
result
Nat result
result
result()
Nat
()
Nat
()
Nat
()
Nat
result
result
result
result
(r r) r
The Church encoding
of natural numbers is:
r
type CNat = forall r. (r -> r) -> r -> r
c0, c1, c2, c3, c4 :: CNat
c0 f z = z
c1 f z = f z
c2 f z = f (f z)
c3 f z = f (f (f z))
c4 f z = f (f (f (f z)))
cSucc :: CNat -> CNat
cSucc cn f = f . cn f
cPlus :: CNat -> CNat -> CNat
cPlus cn1 cn2 f = cn1 f . cn2 f
cMult :: CNat -> CNat -> CNat
cMult cn1 cn2 = cn1 . cn2
type CNat = forall r. (r -> r) -> r -> r
c0, c1, c2, c3, c4 :: CNat
c0 f = id
c1 f = f
c2 f = f . f
c3 f = f . f . f
c4 f = f . f . f . f
cSucc :: CNat -> CNat
cSucc cn f = f . cn f
cPlus :: CNat -> CNat -> CNat
cPlus cn1 cn2 f = cn1 f . cn2 f
cMult :: CNat -> CNat -> CNat
cMult cn1 cn2 = cn1 . cn2
Performance
Native ints Peano numbers Church numbers
addition
print
O(n)
O(n2)
multiplication
O(n) O(n)
O(1)
O(1)
Performance
Native ints Peano numbers Church numbers
addition
print
O(n)
O(n2)
multiplication
O(n) O(n)
O(1)
O(1)
Church encoding cheat sheet
A | B
(A, B)
Singleton
Recursion
(a r) (b r) r
(a r)b r
r
r r
A a r
Nil
Cons(a, List a)
or
List a =
Cons lists
Nil
Cons(a, List a)
or
result
result
result
(a, List a) result
result
result
()
(a, ) result
result
result
result
a result
result
result
result
r r
The Church encoding
of lists is:
r(a ) r
r r
The Church encoding
of lists is:
r(a ) r
AKA: foldr
Functors
a
Functors
f a
a
Functors
f (f a)
They compose!
f a
a
Functors
f (f (f a))
What if we make a
“Church numeral” out of
them?
f (f a)
f a
a
Free monads
f (f (f (f a)))
f (f (f a))
f (f a)
f a
a
Free monad >>=
a
Free monad >>=
a
fmap
Free monad >>=
f a
Free monad >>=
f a
fmap
Free monad >>=
f a
fmap
Free monad >>=
f (f a)
Free monad >>=
f (f a)
fmap
Free monad >>=
f (f a)
fmap
Free monad >>=
f (f a)
fmap
Free monad >>=
f (f (f a))
Free monad >>=
f (f (f a))
fmap
Free monad >>=
f (f (f a))
fmap
Free monad >>=
f (f (f a))
fmap
Free monad >>=
f (f (f a))
fmap
λn  [n+1, n*2]
3
λn  [n+1, n*2]
4 6
λn  [n+1, n*2]
4 6 fmap
λn  [n+1, n*2]
5 8 7 12
λn  [n+1, n*2]
5 8 7 12
fmap
λn  [n+1, n*2]
5 8 7 12 fmap
λn  [n+1, n*2]
6 10 9 16 8 14 13 24
λn  Wrap [Pure (n+1), Pure (n*2)]
3
λn  Wrap [Pure (n+1), Pure (n*2)]
>>=3
4 6
λn  Wrap [Pure (n+1), Pure (n*2)]
4 6
λn  Wrap [Pure (n+1), Pure (n*2)]
>>=
4 6
λn  Wrap [Pure (n+1), Pure (n*2)]
fmap
4 6
λn  Wrap [Pure (n+1), Pure (n*2)]
>>=
λn  Wrap [Pure (n+1), Pure (n*2)]
5 8 7 12
λn  Wrap [Pure (n+1), Pure (n*2)]
5 8 7 12
>>=
λn  Wrap [Pure (n+1), Pure (n*2)]
5 8 7 12
fmap
λn  Wrap [Pure (n+1), Pure (n*2)]
5 8 7 12 >>=
λn  Wrap [Pure (n+1), Pure (n*2)]
5 8 7 12 fmap
λn  Wrap [Pure (n+1), Pure (n*2)]
>>=5 8 7 12
λn  Wrap [Pure (n+1), Pure (n*2)]
6 10 9 16 8 14 13 24
Pure a
Wrap f (Free f a)
or
Free a =
Free monads
Pure a
Wrap f (Free f a)
or
result
result
result
f (Free f a) result
result
result
a
f result
result
result
a
result
r r
The Church encoding
of free monads is:
(f ) rr(a )
r r(f ) rr(a )
>>=
CFree f b
Bind is constant time!
λa -> b
λa -> b
∴
λa -> b
∴

More Related Content

What's hot (20)

PPTX
Episode 2: The LLM / GPT / AI Prompt / Data Engineer Roadmap
Anant Corporation
 
PDF
20 prompts for chatGPT that make life easier for developers.pdf
AD Techlogix - Website & Mobile App Development Company
 
PDF
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
PDF
28 Pitching Essentials
Michael Parker
 
PDF
The power of creative collaboration
Table19
 
PDF
You Don't Know SEO
Michael King
 
PPTX
WTF - Why the Future Is Up to Us - pptx version
Tim O'Reilly
 
PDF
21 Actionable Growth Hacking Tactics
Jon Yongfook
 
PDF
LaunchRock
500 Startups
 
PDF
Airbyte - Seed deck
Airbyte
 
PDF
State of the Cloud 2023—The AI era
Bessemer Venture Partners
 
PDF
Top Productivity Working Hacks by Jan Rezab
Jan Rezab
 
PDF
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
PPTX
How to unlock the secrets of effortless keyword research with ChatGPT.pptx
Daniel Smullen
 
PDF
Tesla Investor Presentation - Model S
startuphome
 
PDF
Vector database
Guy Korland
 
PDF
Working With Big Data
Seth Familian
 
PDF
Crap. The Content Marketing Deluge.
Velocity Partners
 
PDF
Airbnb Pitch Deck From 2008
Ryan Gum
 
PDF
Coinbase Seed Round Pitch Deck
Brian Armstrong
 
Episode 2: The LLM / GPT / AI Prompt / Data Engineer Roadmap
Anant Corporation
 
20 prompts for chatGPT that make life easier for developers.pdf
AD Techlogix - Website & Mobile App Development Company
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
28 Pitching Essentials
Michael Parker
 
The power of creative collaboration
Table19
 
You Don't Know SEO
Michael King
 
WTF - Why the Future Is Up to Us - pptx version
Tim O'Reilly
 
21 Actionable Growth Hacking Tactics
Jon Yongfook
 
LaunchRock
500 Startups
 
Airbyte - Seed deck
Airbyte
 
State of the Cloud 2023—The AI era
Bessemer Venture Partners
 
Top Productivity Working Hacks by Jan Rezab
Jan Rezab
 
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How to unlock the secrets of effortless keyword research with ChatGPT.pptx
Daniel Smullen
 
Tesla Investor Presentation - Model S
startuphome
 
Vector database
Guy Korland
 
Working With Big Data
Seth Familian
 
Crap. The Content Marketing Deluge.
Velocity Partners
 
Airbnb Pitch Deck From 2008
Ryan Gum
 
Coinbase Seed Round Pitch Deck
Brian Armstrong
 

Viewers also liked (20)

PPSX
Presentation on Medicated Chewing Gums
Manoj Kumar Tekuri
 
PDF
The Deep Web - How the Deep Web Works
Omar Samy
 
PPTX
Deep Web - what to do and what not to do
Cysinfo Cyber Security Community
 
PPTX
Deep Web
Ahmed Khan
 
PDF
Human Body
Stacy Schanks
 
PPTX
Japan - An Emerging Civilization
Eleven
 
PPT
Bubble gum
Ms Wilson
 
PPT
Impossible Is Nothing
Richard Dedor
 
PDF
Dos and Don'ts of an Engineering Statement of Purpose
SOP Writing
 
ODP
Impossible is Nothing?
Shriram Sivaramakrishnan
 
PPTX
10 facts about japan
AbdAlrahman siyamek
 
PPSX
Nobel Peace Prize 2014: Malala Yousafzai and Kailash Satyarthi
maditabalnco
 
PPT
Nobel prize
asiyat1975
 
PPTX
Deep web
ANKIT OJHA
 
PDF
Clean and green hydrocarbons ignite publish
Krzysztof (Kris) Palka
 
PDF
Sustainable Innovation Day
Peter Bertels
 
PDF
10 Practical Ways to Be More Efficient at Work
Weekdone.com
 
PDF
iOS Scroll Performance
Kyle Sherman
 
PDF
Testing at Spotify
Andrii Dzynia
 
Presentation on Medicated Chewing Gums
Manoj Kumar Tekuri
 
The Deep Web - How the Deep Web Works
Omar Samy
 
Deep Web - what to do and what not to do
Cysinfo Cyber Security Community
 
Deep Web
Ahmed Khan
 
Human Body
Stacy Schanks
 
Japan - An Emerging Civilization
Eleven
 
Bubble gum
Ms Wilson
 
Impossible Is Nothing
Richard Dedor
 
Dos and Don'ts of an Engineering Statement of Purpose
SOP Writing
 
Impossible is Nothing?
Shriram Sivaramakrishnan
 
10 facts about japan
AbdAlrahman siyamek
 
Nobel Peace Prize 2014: Malala Yousafzai and Kailash Satyarthi
maditabalnco
 
Nobel prize
asiyat1975
 
Deep web
ANKIT OJHA
 
Clean and green hydrocarbons ignite publish
Krzysztof (Kris) Palka
 
Sustainable Innovation Day
Peter Bertels
 
10 Practical Ways to Be More Efficient at Work
Weekdone.com
 
iOS Scroll Performance
Kyle Sherman
 
Testing at Spotify
Andrii Dzynia
 
Ad

Similar to Data made out of functions (20)

PPTX
Datamadeoutoffunctions 160201105803
FIKRI VIZAY
 
PPTX
Strong functional programming
Eric Torreborre
 
PDF
Why Haskell Matters
romanandreg
 
PDF
Genetic programming
Yun-Yan Chi
 
PDF
The Fuss about || Haskell | Scala | F# ||
Ashwin Rao
 
PDF
Optimizing with persistent data structures (LLVM Cauldron 2016)
Igalia
 
PPTX
Intro f# functional_programming
Mauro Ghiani
 
PDF
Functional Programming in C++
sankeld
 
PPS
Presentation of GetTogether on Functional Programming
Filip De Sutter
 
PDF
Introduction to Functional Languages
suthi
 
PDF
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
PDF
Intro To Agda
Larry Diehl
 
PDF
Functional programming with F#
Remik Koczapski
 
PPTX
Functional linear data structures in f#
Jack Fox
 
PDF
Functional Programming in F#
Dmitri Nesteruk
 
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
PDF
DublinJS - Functional Programming
bobjlong
 
PDF
CORCON2014: Does programming really need data structures?
Marco Benini
 
PPTX
Exploring Tailrec Through Time Until Kotlin.pptx
João Esperancinha
 
PDF
Reasoning about laziness
Johan Tibell
 
Datamadeoutoffunctions 160201105803
FIKRI VIZAY
 
Strong functional programming
Eric Torreborre
 
Why Haskell Matters
romanandreg
 
Genetic programming
Yun-Yan Chi
 
The Fuss about || Haskell | Scala | F# ||
Ashwin Rao
 
Optimizing with persistent data structures (LLVM Cauldron 2016)
Igalia
 
Intro f# functional_programming
Mauro Ghiani
 
Functional Programming in C++
sankeld
 
Presentation of GetTogether on Functional Programming
Filip De Sutter
 
Introduction to Functional Languages
suthi
 
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
Intro To Agda
Larry Diehl
 
Functional programming with F#
Remik Koczapski
 
Functional linear data structures in f#
Jack Fox
 
Functional Programming in F#
Dmitri Nesteruk
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
DublinJS - Functional Programming
bobjlong
 
CORCON2014: Does programming really need data structures?
Marco Benini
 
Exploring Tailrec Through Time Until Kotlin.pptx
João Esperancinha
 
Reasoning about laziness
Johan Tibell
 
Ad

More from kenbot (12)

PPTX
Grow your own tech leads
kenbot
 
PPTX
Applied category theory: the emerging science of compositionality
kenbot
 
PPTX
Responsible DI: Ditch the Frameworks
kenbot
 
PPTX
FP adoption at REA
kenbot
 
PPTX
Lenses for the masses - introducing Goggles
kenbot
 
PPTX
Good functional programming is good programming
kenbot
 
PPTX
Imagine a world without mocks
kenbot
 
PPTX
2 Years of Real World FP at REA
kenbot
 
PPTX
Your data structures are made of maths!
kenbot
 
PPTX
Category theory for beginners
kenbot
 
PPTX
The disaster of mutable state
kenbot
 
PPTX
Running Free with the Monads
kenbot
 
Grow your own tech leads
kenbot
 
Applied category theory: the emerging science of compositionality
kenbot
 
Responsible DI: Ditch the Frameworks
kenbot
 
FP adoption at REA
kenbot
 
Lenses for the masses - introducing Goggles
kenbot
 
Good functional programming is good programming
kenbot
 
Imagine a world without mocks
kenbot
 
2 Years of Real World FP at REA
kenbot
 
Your data structures are made of maths!
kenbot
 
Category theory for beginners
kenbot
 
The disaster of mutable state
kenbot
 
Running Free with the Monads
kenbot
 

Recently uploaded (20)

PPT
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
PDF
Choosing the Right Database for Indexing.pdf
Tamanna
 
PDF
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
PDF
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
PDF
Copia de Strategic Roadmap Infographics by Slidesgo.pptx (1).pdf
ssuserd4c6911
 
PPTX
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
PPTX
The _Operations_on_Functions_Addition subtruction Multiplication and Division...
mdregaspi24
 
PPTX
recruitment Presentation.pptxhdhshhshshhehh
devraj40467
 
PDF
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
PPTX
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
PDF
What does good look like - CRAP Brighton 8 July 2025
Jan Kierzyk
 
PPTX
Advanced_NLP_with_Transformers_PPT_final 50.pptx
Shiwani Gupta
 
PDF
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
PPTX
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
PDF
JavaScript - Good or Bad? Tips for Google Tag Manager
📊 Markus Baersch
 
PDF
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
PDF
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
PPTX
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
PPTX
Climate Action.pptx action plan for climate
justfortalabat
 
PPTX
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
Choosing the Right Database for Indexing.pdf
Tamanna
 
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
Copia de Strategic Roadmap Infographics by Slidesgo.pptx (1).pdf
ssuserd4c6911
 
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
The _Operations_on_Functions_Addition subtruction Multiplication and Division...
mdregaspi24
 
recruitment Presentation.pptxhdhshhshshhehh
devraj40467
 
The European Business Wallet: Why It Matters and How It Powers the EUDI Ecosy...
Lal Chandran
 
apidays Singapore 2025 - From Data to Insights: Building AI-Powered Data APIs...
apidays
 
What does good look like - CRAP Brighton 8 July 2025
Jan Kierzyk
 
Advanced_NLP_with_Transformers_PPT_final 50.pptx
Shiwani Gupta
 
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
JavaScript - Good or Bad? Tips for Google Tag Manager
📊 Markus Baersch
 
Avatar for apidays apidays PRO June 07, 2025 0 5 apidays Helsinki & North 2...
apidays
 
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
Climate Action.pptx action plan for climate
justfortalabat
 
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 

Data made out of functions