data made
play

data made out of - PowerPoint PPT Presentation

data made out of functions #ylj2016 For faster @KenScambler monads!


  1. λλλλλ data made λλ λλ λ λ λ λ out of λ λ λ λ λ λ functions λ λ λ λ λ λλλλ λ λ λ λλ λλ λλλλλ #ylj2016 For faster @KenScambler monads!

  2. Diogenes of Sinope 412 – 323 BC

  3. • Simplest man of all time • Obnoxious hobo • Lived in a barrel Diogenes of Sinope 412 – 323 BC

  4. I’ve been using this bowl like a sucker!

  5. Um…. what

  6. [a, b, c, d] "abcd" IF x THEN y ELSE z INT WHILE cond { … } λ a -> b STRUCT { fields… BOOL }

  7. Strings are [a, b, c, d] pretty much arrays IF x THEN y ELSE z INT WHILE cond { … } λ a -> b STRUCT { fields… BOOL }

  8. Recursion can [a, b, c, d] do loops IF x THEN y ELSE z INT λ a -> b STRUCT { fields… BOOL }

  9. Recursive data structures can do lists IF x THEN y ELSE z INT [a,b,c,d] λ a -> b STRUCT { fields… BOOL }

  10. Ints can do bools IF x THEN y ELSE z INT [a,b,c,d] λ a -> b STRUCT { fields… }

  11. IF x THEN y ELSE z INT [a,b,c,d] λ a -> b STRUCT { fields… }

  12. λ a -> b [a,b,c,d] STRUCT

  13. λ a -> b Lambda calculus Alonzo Church 1903 - 1995

  14. λ a -> b Lambda calculus Alonzo Church 1903 - 1995

  15. Church encoding We can make any data structure out of functions!

  16. Booleans Bool

  17. Church booleans Bool result

  18. Church booleans If we define everything you can do with a structure , isn’t that the same as defining Bool result the structure itself?

  19. TRUE or FALSE

  20. “What we do if it’s true” TRUE result or FALSE

  21. “What we do if it’s false” TRUE or FALSE result

  22. TRUE result or result FALSE result

  23. () result result () result

  24. result result result

  25. The Church encoding of a boolean is: r r r

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

  27. Natural numbers 0 1 2 3 4 …

  28. Natural numbers 0 0 +1 0 +1 +1 0 +1 +1 +1 0 +1 +1 +1 +1 …

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

  30. Natural Peano numbers Succ(Nat) Nat = or Zero Giuseppe Peano 1858 - 1932

  31. Now lets turn it into functions! Succ(Nat) Nat = or Zero

  32. “If it’s a successor” Succ(Nat) result or Zero

  33. Succ(Nat) or “If it’s zero” Zero result

  34. Succ(Nat) result result or Zero result

  35. Nat result result () result

  36. Nat result result result

  37. Nat result () result Nat result () Nat () Nat () Nat

  38. result result result result

  39. The Church encoding of natural numbers is: r (r r r)

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

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

  42. Performance Native ints Peano numbers Church numbers O(n) O(1) addition O(1) O(n 2 ) multiplication O(n) O(n) print

  43. Performance Native ints Peano numbers Church numbers O(n) O(1) addition O(1) O(n 2 ) multiplication O(n) O(n) print

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

  45. Cons lists List a = Cons(a, List a) or Nil

  46. Cons(a, List a) result or result result Nil

  47. (a, List a) result result () result

  48. (a, ) result result result result

  49. a result result result result

  50. The Church encoding of lists is: ) (a r r r r

  51. The Church encoding of lists is: ) (a r r r r AKA: foldr

  52. Functors a

  53. Functors a f a

  54. Functors a f a f (f a) They compose!

  55. Functors a f a What if we make a “Church numeral” out of f (f a) them? f (f (f a))

  56. Free monads a f a f (f a) f (f (f a)) f (f (f (f a)))

  57. Free monad >>= a

  58. Free monad >>= a fmap

  59. Free monad >>= f a

  60. Free monad >>= f a fmap

  61. Free monad >>= f a fmap

  62. Free monad >>= f (f a)

  63. Free monad >>= f (f a) fmap

  64. Free monad >>= f (f a) fmap

  65. Free monad >>= f (f a) fmap

  66. Free monad >>= f (f (f a))

  67. Free monad >>= f (f (f a)) fmap

  68. Free monad >>= f (f (f a)) fmap

  69. Free monad >>= f (f (f a)) fmap

  70. Free monad >>= f (f (f a)) fmap

  71. λ n  [n+1, n*2] 3

  72. λ n  [n+1, n*2] 4 6

  73. λ n  [n+1, n*2] 4 6 fmap

  74. λ n  [n+1, n*2] 7 12 5 8

  75. λ n  [n+1, n*2] fmap 7 12 5 8

  76. λ n  [n+1, n*2] 7 12 5 8 fmap

  77. λ n  [n+1, n*2] 8 14 6 10 9 16 13 24

  78. λ n  Wrap [Pure (n+1), Pure (n*2)] 3

  79. λ n  Wrap [Pure (n+1), Pure (n*2)] 3 >>=

  80. λ n  Wrap [Pure (n+1), Pure (n*2)] 6 4

  81. λ n  Wrap [Pure (n+1), Pure (n*2)] 6 4 >>=

  82. λ n  Wrap [Pure (n+1), Pure (n*2)] 6 4 fmap

  83. λ n  Wrap [Pure (n+1), Pure (n*2)] 6 4 >>=

  84. λ n  Wrap [Pure (n+1), Pure (n*2)] 7 12 5 8

  85. λ n  Wrap [Pure (n+1), Pure (n*2)] >>= 7 12 5 8

  86. λ n  Wrap [Pure (n+1), Pure (n*2)] fmap 7 12 5 8

  87. λ n  Wrap [Pure (n+1), Pure (n*2)] 7 12 >>= 5 8

  88. λ n  Wrap [Pure (n+1), Pure (n*2)] 7 12 fmap 5 8

  89. λ n  Wrap [Pure (n+1), Pure (n*2)] 7 12 >>= 5 8

  90. λ n  Wrap [Pure (n+1), Pure (n*2)] 9 16 8 14 6 10 13 24

  91. Free monads Free a = Wrap f (Free f a) or Pure a

  92. Wrap f (Free f a) result or result Pure a result

  93. f (Free f a) result result a result

  94. f result result result a result

  95. The Church encoding of free monads is: ) (f r r r (a ) r

  96. Bind is constant time! ) (f r r r (a ) r >>= CFree f b

  97. λ a -> b

  98. λ a -> b ∴

Recommend


More recommend