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

Post on 23-May-2020

18 views 0 download

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

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

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 Church1903 - 1995

λa -> b

Lambda 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

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 :: 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 numbers0

1

2

3

4

Natural numbers0

0 +1

0 +1 +1

0 +1 +1 +1

0 +1 +1 +1 +1

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!

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

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 = 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

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 >>=

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

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