Datamadeoutoffunctions 160201105803

Post on 16-Feb-2017

92 views 1 download

Transcript of Datamadeoutoffunctions 160201105803

data made out of

functions#lca2016 FP miniconf

@KenScambler

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

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:

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

PerformanceNative ints Peano numbers Church numbers

addition

print

Really really fast

O(n)

O(n2)multiplication

O(n) O(n)

O(1)

O(1)

PerformanceNative ints Peano numbers Church numbers

addition

print

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

Lists

Cons lists

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

λa -> b

λa -> b∴

λa -> b∴