data made out of
functions#ylj2016@KenScambler
λλλλλ λλ λλ λ λ λ λλ λ λ λλ λλ λ λ λ λ λλλλ λ λ λ λλ λλ λλλλλ
For faster monads!
Diogenes of Sinope412 – 323 BC
Diogenes of Sinope412 – 323 BC
• Simplest man of all time• Obnoxious hobo• Lived in a barrel
I’ve been using this bowl like a sucker!
Um…. what
"abcd"
IF x THEN y ELSE z
WHILE cond {…}
[a, b, c, d]
BOOL
INT
STRUCT { fields…}
λa -> b
"abcd"
IF x THEN y ELSE z
WHILE cond {…}
[a, b, c, d]
BOOL
INT
STRUCT { fields…}
λa -> b
Strings are pretty much
arrays
"abcd"
IF x THEN y ELSE z
WHILE
[a, b, c, d]
BOOL
INT
STRUCT { fields…}
λa -> b
Recursion can do loops
"abcd"
IF x THEN y ELSE z
WHILE
[a,b,c,d]
BOOL
INT
STRUCT { fields…}
λa -> b
Recursive data structures can do
lists
"abcd"
IF x THEN y ELSE z
WHILE
[a,b,c,d]
BOOL
INT
STRUCT { fields…}
λa -> b
Ints can do bools
"abcd"
IF x THEN y ELSE z
WHILE
[a,b,c,d]
BOOL
INT
STRUCT { fields…}
λa -> b
"abcd"IF x THEN
WHILE
[a,b,c,d]
BOOLINTSTRUCT
λa -> b
Alonzo Church1903 - 1995
λa -> bLambda calculus
λa -> b
Alonzo Church1903 - 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
FALSEor
TRUE
FALSEor
result
“What we do if it’s true”
“What we do if it’s false”
TRUE
FALSEor
result
TRUE
FALSEor
result
resultresult
()
()
result
resultresult
result
resultresult
r r r
The Church encoding of a boolean is:
type CBool = forall r. r -> r -> r
cTrue :: CBoolcTrue x y = x
cFalse :: CBoolcFalse x y = y
cNot :: CBool -> CBoolcNot cb = cb cFalse cTrue
cAnd :: CBool -> CBool -> CBoolcAnd cb1 cb2 = cb1 cb2 cFalse
cOr :: CBool -> CBool -> CBoolcOr cb1 cb2 = cb1 cTrue cb2
Natural numbers01234…
Natural numbers00 +10 +1 +10 +1 +1 +10 +1 +1 +1 +1…
Natural numbers00 +10 +1 +10 +1 +1 +10 +1 +1 +1 +1…
Giuseppe Peano1858 - 1932
Natural numbers form
a data structure!
Zero
Succ(Nat)or
Nat =
Natural Peano numbers
Giuseppe Peano1858 - 1932
orNat =
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
resultresultor
Zero
Succ(Nat)
Nat
()
result
resultresult
Nat result
resultresult
Nat result
resultresult()
Nat()Nat
()Nat
()Nat
result
resultresult
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 = zc1 f z = f zc2 f z = f (f z)c3 f z = f (f (f z))c4 f z = f (f (f (f z)))
cSucc :: CNat -> CNatcSucc cn f = f . cn f
cPlus :: CNat -> CNat -> CNatcPlus cn1 cn2 f = cn1 f . cn2 f
cMult :: CNat -> CNat -> CNatcMult cn1 cn2 = cn1 . cn2
type CNat = forall r. (r -> r) -> r -> r
c0, c1, c2, c3, c4 :: CNat
c0 f = idc1 f = fc2 f = f . fc3 f = f . f . fc4 f = f . f . f . f
cSucc :: CNat -> CNatcSucc cn f = f . cn f
cPlus :: CNat -> CNat -> CNatcPlus cn1 cn2 f = cn1 f . cn2 f
cMult :: CNat -> CNat -> CNatcMult cn1 cn2 = cn1 . cn2
PerformanceNative ints Peano numbers Church numbers
addition
Really really fast
O(n)
O(n2)multiplication
O(n) O(n)
O(1)
O(1)
PerformanceNative ints Peano numbers Church numbers
addition
Really really fast
O(n)
O(n2)multiplication
O(n) O(n)
O(1)
O(1)
Church encoding cheat sheet
A | B(A, B)
SingletonRecursion
(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
resultresult
(a, List a) result
resultresult
()
(a, ) result
resultresult
result
a result
resultresult
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
Functorsa
Functors
f aa
Functors
f (f a)They compose!
f aa
Functors
f (f (f a))
What if we make a “Church numeral” out of them?
f (f a)f aa
Free monads
f (f (f (f a)))f (f (f a))
f (f a)f aa
Free monad >>=
a
Free monad >>=
afmap
Free monad >>=
f a
Free monad >>=
f afmap
Free monad >>=
f afmap
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
resultresult
f (Free f a) result
resultresult
a
f result
resultresult
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∴
Top Related