Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler...

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

Transcript of Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler...

Page 1: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

data made out of

functions

#ylj2016

@KenScambler

λλλλλλλ λλ

λ λλ λλ λ λ λλ λλ λ λ λλ λλλλ λλ λλλ λλ

λλλλλ

For faster monads!

Page 2: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Diogenes of Sinope412 – 323 BC

Page 3: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Diogenes of Sinope412 – 323 BC

• Simplest man of all time• Obnoxious hobo• Lived in a barrel

Page 4: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

I’ve been using this bowl like a sucker!

Page 5: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive
Page 6: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive
Page 7: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Um…. what

Page 8: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

"abcd"

IF x THEN y ELSE z

WHILE cond {…}

[a, b, c, d]

BOOL

INT

STRUCT {fields…

}

λa -> b

Page 9: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

IF x THEN y ELSE z

WHILE cond {…}

[a, b, c, d]

BOOL

INT

STRUCT {fields…

}

λa -> b

Strings are pretty much

arrays

Page 10: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

IF x THEN y ELSE z

[a, b, c, d]

BOOL

INT

STRUCT {fields…

}

λa -> b

Recursion can do loops

Page 11: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

IF x THEN y ELSE z

[a,b,c,d]

BOOL

INT

STRUCT {fields…

}

λa -> b

Recursive data structures can do

lists

Page 12: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

IF x THEN y ELSE z

[a,b,c,d]

INT

STRUCT {fields…

}

λa -> b

Ints can do bools

Page 13: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

IF x THEN y ELSE z

[a,b,c,d]

INT

STRUCT {fields…

}

λa -> b

Page 14: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

[a,b,c,d]

STRUCT

λa -> b

Page 15: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Alonzo Church1903 - 1995

λa -> b

Lambda calculus

Page 16: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λa -> b

Alonzo Church1903 - 1995

Lambda calculus

Page 17: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

We can make any data structure out of

functions!

Church encoding

Page 18: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Booleans

Bool

Page 19: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Church booleans

resultBool

Page 20: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Church booleans

resultBool

If we define everything you can do with a structure, isn’t

that the same as defining the structure itself?

Page 21: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

TRUE

FALSEor

Page 22: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

TRUE

FALSEor

result

“What we do if it’s true”

Page 23: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

“What we do if it’s false”

TRUE

FALSEor

result

Page 24: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

TRUE

FALSEor

result

result

result

Page 25: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

()

()

result

result

result

Page 26: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

result

result

result

Page 27: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

r r r

The Church encoding of a boolean is:

Page 28: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

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

Page 29: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Natural numbers0

1

2

3

4

Page 30: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Natural numbers0

0 +1

0 +1 +1

0 +1 +1 +1

0 +1 +1 +1 +1

Page 31: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Natural numbers0

0 +1

0 +1 +1

0 +1 +1 +1

0 +1 +1 +1 +1

…Giuseppe Peano1858 - 1932

Natural numbers form

a data structure!

Page 32: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Zero

Succ(Nat)or

Nat =

Natural Peano numbers

Giuseppe Peano1858 - 1932

Page 33: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

orNat =

Now lets turn it into functions!

Zero

Succ(Nat)

Page 34: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Zero

Succ(Nat)or

result

“If it’s a successor”

Page 35: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

or “If it’s zero”

resultZero

Succ(Nat)

Page 36: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

result

result

resultor

Zero

Succ(Nat)

Page 37: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Nat

()

result

result

result

Page 38: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Nat result

result

result

Page 39: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Nat result

result

result()Nat

()Nat

()Nat

()Nat

Page 40: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

result

result

result

result

Page 41: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

(r r) r

The Church encoding of natural numbers is:

r

Page 42: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

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

Page 43: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

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

Page 44: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Performance

Native ints Peano numbers Church numbers

addition

print

O(n)

O(n2)

multiplication

O(n) O(n)

O(1)

O(1)

Page 45: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Performance

Native ints Peano numbers Church numbers

addition

print

O(n)

O(n2)

multiplication

O(n) O(n)

O(1)

O(1)

Page 46: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

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

Page 47: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Nil

Cons(a, List a)or

List a =

Cons lists

Page 48: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Nil

Cons(a, List a)or

result

result

result

Page 49: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

(a, List a) result

result

result

()

Page 50: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

(a, ) result

result

result

result

Page 51: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

a result

result

result

result

Page 52: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

r r

The Church encoding of lists is:

r(a ) r

Page 53: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

r r

The Church encoding of lists is:

r(a ) r

AKA: foldr

Page 54: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Functors

a

Page 55: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Functors

f a

a

Page 56: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Functors

f (f a)

They compose!

f a

a

Page 57: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Functors

f (f (f a))

What if we make a “Church numeral” out of them?

f (f a)

f a

a

Page 58: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monads

f (f (f (f a)))

f (f (f a))

f (f a)

f a

a

Page 59: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

a

Page 60: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

afmap

Page 61: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

f a

Page 62: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

f afmap

Page 63: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

f afmap

Page 64: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

f (f a)

Page 65: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

f (f a)fmap

Page 66: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

f (f a)fmap

Page 67: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

f (f a)fmap

Page 68: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

f (f (f a))

Page 69: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

f (f (f a))fmap

Page 70: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

f (f (f a))fmap

Page 71: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

f (f (f a))fmap

Page 72: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Free monad >>=

f (f (f a))fmap

Page 73: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn [n+1, n*2]

3

Page 74: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn [n+1, n*2]

4 6

Page 75: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn [n+1, n*2]

4 6 fmap

Page 76: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn [n+1, n*2]

5 8 7 12

Page 77: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn [n+1, n*2]

5 8 7 12

fmap

Page 78: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn [n+1, n*2]

5 8 7 12 fmap

Page 79: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn [n+1, n*2]

6 10 9 16 8 14 13 24

Page 80: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn Wrap [Pure (n+1), Pure (n*2)]

3

Page 81: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn Wrap [Pure (n+1), Pure (n*2)]

>>=3

Page 82: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

4 6

λn Wrap [Pure (n+1), Pure (n*2)]

Page 83: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

4 6

λn Wrap [Pure (n+1), Pure (n*2)]

>>=

Page 84: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

4 6

λn Wrap [Pure (n+1), Pure (n*2)]

fmap

Page 85: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

4 6

λn Wrap [Pure (n+1), Pure (n*2)]

>>=

Page 86: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn Wrap [Pure (n+1), Pure (n*2)]

5 8 7 12

Page 87: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn Wrap [Pure (n+1), Pure (n*2)]

5 8 7 12

>>=

Page 88: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn Wrap [Pure (n+1), Pure (n*2)]

5 8 7 12

fmap

Page 89: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn Wrap [Pure (n+1), Pure (n*2)]

5 8 7 12 >>=

Page 90: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn Wrap [Pure (n+1), Pure (n*2)]

5 8 7 12 fmap

Page 91: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn Wrap [Pure (n+1), Pure (n*2)]

>>=5 8 7 12

Page 92: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λn Wrap [Pure (n+1), Pure (n*2)]

6 10 9 16 8 14 13 24

Page 93: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Pure a

Wrap f (Free f a)or

Free a =

Free monads

Page 94: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

Pure a

Wrap f (Free f a)or

result

result

result

Page 95: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

f (Free f a) result

result

result

a

Page 96: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

f result

result

result

a

result

Page 97: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

r r

The Church encoding of free monads is:

(f ) rr(a )

Page 98: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

r r(f ) rr(a )

>>=CFree f b

Bind is constant time!

Page 99: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λa -> b

Page 100: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λa -> b

Page 101: Data made out of functions - YOW! Conferences...data made out of functions #ylj2016 @KenScambler λλλλλ λλ λλ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λ λλλλ λ ... Recursive

λa -> b