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