48
Seven trees in one Mark Hopkins @antiselfdual Commonwealth Bank LambdaJam 2015 Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Seven trees in one

Embed Size (px)

Citation preview

Page 1: Seven trees in one

Seven trees in one

Mark Hopkins@antiselfdual

Commonwealth Bank

LambdaJam 2015

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 2: Seven trees in one

Unlabelled binary trees

data Tree = Leaf | Node Tree Tree

T = 1+ T 2

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 3: Seven trees in one

Unlabelled binary trees

data Tree = Leaf | Node Tree Tree

T = 1+ T 2

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 4: Seven trees in one

T = 1+ T 2

Suspend disbelief, and solve forT .

T 2 − T + 1 = 0

T =−b ±

√b2 − 4ac2a

= 12 ±

√3

2 i

= e±πi/3

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 5: Seven trees in one

T = 1+ T 2

Suspend disbelief, and solve forT .

T 2 − T + 1 = 0

T =−b ±

√b2 − 4ac2a

= 12 ±

√3

2 i

= e±πi/3

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 6: Seven trees in one

T = 1+ T 2

Suspend disbelief, and solve forT .

T 2 − T + 1 = 0

T =−b ±

√b2 − 4ac2a

= 12 ±

√3

2 i

= e±πi/3

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 7: Seven trees in one

T = 1+ T 2

Suspend disbelief, and solve forT .

T 2 − T + 1 = 0

T =−b ±

√b2 − 4ac2a

= 12 ±

√3

2 i

= e±πi/3

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 8: Seven trees in one

T = 1+ T 2

Suspend disbelief, and solve forT .

T 2 − T + 1 = 0

T =−b ±

√b2 − 4ac2a

= 12 ±

√3

2 i

= e±πi/3

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 9: Seven trees in one

Re

Im

1

−1

i

−i

T

−T

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 10: Seven trees in one

So T 6 = 1.

No, obviously wrong.What about

T 7 = T?

Not obviously wrong . . .⇒ true!

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 11: Seven trees in one

So T 6 = 1. No, obviously wrong.

What aboutT 7 = T?

Not obviously wrong . . .⇒ true!

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 12: Seven trees in one

So T 6 = 1. No, obviously wrong.What about

T 7 = T?

Not obviously wrong . . .⇒ true!

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 13: Seven trees in one

So T 6 = 1. No, obviously wrong.What about

T 7 = T?

Not obviously wrong . . .

⇒ true!

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 14: Seven trees in one

So T 6 = 1. No, obviously wrong.What about

T 7 = T?

Not obviously wrong . . .⇒ true!

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 15: Seven trees in one

TheoremThere exists an O(1) bijective function from T to T 7.

i.e.I we can pattern match on any 7-tuple of trees and put them

together into one tree.I we can decompose any tree into the same seven trees it came

from.Actually holds for any k = 1 mod 6.Not true for other values.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 16: Seven trees in one

TheoremThere exists an O(1) bijective function from T to T 7.i.e.

I we can pattern match on any 7-tuple of trees and put themtogether into one tree.

I we can decompose any tree into the same seven trees it camefrom.

Actually holds for any k = 1 mod 6.Not true for other values.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 17: Seven trees in one

TheoremThere exists an O(1) bijective function from T to T 7.i.e.

I we can pattern match on any 7-tuple of trees and put themtogether into one tree.

I we can decompose any tree into the same seven trees it camefrom.

Actually holds for any k = 1 mod 6.Not true for other values.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 18: Seven trees in one

TheoremThere exists an O(1) bijective function from T to T 7.i.e.

I we can pattern match on any 7-tuple of trees and put themtogether into one tree.

I we can decompose any tree into the same seven trees it camefrom.

Actually holds for any k = 1 mod 6.

Not true for other values.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 19: Seven trees in one

TheoremThere exists an O(1) bijective function from T to T 7.i.e.

I we can pattern match on any 7-tuple of trees and put themtogether into one tree.

I we can decompose any tree into the same seven trees it camefrom.

Actually holds for any k = 1 mod 6.Not true for other values.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 20: Seven trees in one

T 2 → T

f :: (Tree , Tree) → Treet t1 t2 = Node t1 t2

Not surjective, since we can never reach Leaf.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 21: Seven trees in one

T 2 → T

f :: (Tree , Tree) → Treet t1 t2 = Node t1 t2

Not surjective, since we can never reach Leaf.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 22: Seven trees in one

T → T 2

f :: Tree → (Tree , Tree)f t = Node t Leaf

Not surjective either.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 23: Seven trees in one

T → T 2

f :: Tree → (Tree , Tree)f t = Node t Leaf

Not surjective either.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 24: Seven trees in one

T 2 → T , but cleverer

f :: (Tree , Tree) → Treef (t1, t2) = go (Node t1 t2)

wherego t = if leftOnly t then left t else tleftOnly t = t == Leaf

|| right t == Leaf && leftOnly (left t)

Bijective! but not O(1).

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 25: Seven trees in one

T 2 → T , but cleverer

f :: (Tree , Tree) → Treef (t1, t2) = go (Node t1 t2)

wherego t = if leftOnly t then left t else tleftOnly t = t == Leaf

|| right t == Leaf && leftOnly (left t)

Bijective!

but not O(1).

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 26: Seven trees in one

T 2 → T , but cleverer

f :: (Tree , Tree) → Treef (t1, t2) = go (Node t1 t2)

wherego t = if leftOnly t then left t else tleftOnly t = t == Leaf

|| right t == Leaf && leftOnly (left t)

Bijective! but not O(1).

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 27: Seven trees in one

A solution

f :: T → (T, T, T, T, T, T, T)

f L = (L,L,L,L,L,L,L)

f (N t1 L) = (t1,N L L,L,L,L,L,L)

f (N t1 (N t2 L)) = (N t1 t2,L,L,L,L,L,L)

f (N t1 (N t2 (N t3 L))) = (t1,N (N t2 t3) L,L,L,L,L,L)

f (N t1 (N t2 (N t3 (N t4 L)))) = (t1,N t2 (N t3 t4),L,L,L,L,L)

f (N t1 (N t2 (N t3 (N t4 (N L L))))) = (t1,t2,N t3 t4,L,L,L,L)

f (N t1 (N t2 (N t3 (N t4 (N (N t5 L) L))))) = (t1,t2,t3,N t4 t5,L,L,L)

f (N t1 (N t2 (N t3 (N t4 (N (N t5 (N t6 L)) L))))) = (t1,t2,t3,t4,N t5 t6,L,L)

f (N t1 (N t2 (N t3 (N t4 (N (N t5 (N t6 (N t7 t8))) L))))) = (t1,t2,t3,t4,t5,t6,N t7 t8)

f (N t1 (N t2 (N t3 (N t4 (N t5 (N t6 t7 )))))) = (t1,t2,t3,t4,t5,N t6 t7,L)

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 28: Seven trees in one

Where did this come from

T = 1+ T 2

T k = T k−1 + T k+1

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 29: Seven trees in one

Where did this come from

T = 1+ T 2

T k = T k−1 + T k+1

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 30: Seven trees in one

Penny game

T 0 T 1 T 2 T 3 T 4 T 5 T 6 T 7 T 8

I start with a penny in position 1.I aim is to move it to position 7 by splitting and combining

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 31: Seven trees in one

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 32: Seven trees in one

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 33: Seven trees in one

Why did this work?If we have a type isomorphism T ∼= p(T ) then

q1(T ) ∼= q2(T ) as types

⇐⇒ q1(x) ∼= q2(x) in the rig N[x ]/(p(x) = x)

⇒ q1(x) ∼= q2(x) in the ring Z[x ]/(p(x) = x)

⇒ q1(z) ∼= q2(z) for all z ∈ C such that p(z) = z .

And, under some conditions, the reverse implications hold.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 34: Seven trees in one

Why did this work?If we have a type isomorphism T ∼= p(T ) then

q1(T ) ∼= q2(T ) as types

⇐⇒ q1(x) ∼= q2(x) in the rig N[x ]/(p(x) = x)

⇒ q1(x) ∼= q2(x) in the ring Z[x ]/(p(x) = x)

⇒ q1(z) ∼= q2(z) for all z ∈ C such that p(z) = z .

And, under some conditions, the reverse implications hold.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 35: Seven trees in one

Why did this work?If we have a type isomorphism T ∼= p(T ) then

q1(T ) ∼= q2(T ) as types

⇐⇒ q1(x) ∼= q2(x) in the rig N[x ]/(p(x) = x)

⇒ q1(x) ∼= q2(x) in the ring Z[x ]/(p(x) = x)

⇒ q1(z) ∼= q2(z) for all z ∈ C such that p(z) = z .

And, under some conditions, the reverse implications hold.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 36: Seven trees in one

Why did this work?If we have a type isomorphism T ∼= p(T ) then

q1(T ) ∼= q2(T ) as types

⇐⇒ q1(x) ∼= q2(x) in the rig N[x ]/(p(x) = x)

⇒ q1(x) ∼= q2(x) in the ring Z[x ]/(p(x) = x)

⇒ q1(z) ∼= q2(z) for all z ∈ C such that p(z) = z .

And, under some conditions, the reverse implications hold.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 37: Seven trees in one

Why did this work?If we have a type isomorphism T ∼= p(T ) then

q1(T ) ∼= q2(T ) as types

⇐⇒ q1(x) ∼= q2(x) in the rig N[x ]/(p(x) = x)

⇒ q1(x) ∼= q2(x) in the ring Z[x ]/(p(x) = x)

⇒ q1(z) ∼= q2(z) for all z ∈ C such that p(z) = z .

And, under some conditions, the reverse implications hold.

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 38: Seven trees in one

Summary

I Simple arithmetic helps us find non-obvious type isomorphisms

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 39: Seven trees in one

Extensions

I Are there extensions to datatypes of decorated trees?(multivariate polynomials)

I What applications are there?

I important when writing a compiler to know when two typesare isomomorphic

I It could interesting to split up a tree-shaped stream into sevenparts

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 40: Seven trees in one

Extensions

I Are there extensions to datatypes of decorated trees?(multivariate polynomials)

I What applications are there?I important when writing a compiler to know when two types

are isomomorphic

I It could interesting to split up a tree-shaped stream into sevenparts

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 41: Seven trees in one

Extensions

I Are there extensions to datatypes of decorated trees?(multivariate polynomials)

I What applications are there?I important when writing a compiler to know when two types

are isomomorphicI It could interesting to split up a tree-shaped stream into seven

parts

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 42: Seven trees in one

I Rich theory behind isomorphisms of polynomial typesI brings together a number of fields

I distributive categoriesI theory of rigs (semirings)I combinatorial speciesI type theory

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 43: Seven trees in one

Further reading

I Seven Trees in one, Andreas Blass, Journal of Pure andApplied Algebra

I On the generic solution to P(X ) = X in distributivecategories, Robbie Gates

I Objects of Categories as Complex Numbers, Marcelo Fiore andTom Leinster

I An Objective Representation of the Gaussian Integers, MarceloFiore and Tom Leinster

I http://rfcwalters.blogspot.com.au/2010/06/robbie-gates-on-seven-trees-in-one.html

I http://blog.sigfpe.com/2007/09/arboreal-isomorphisms-from-nuclear.html

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 44: Seven trees in one

Challenge

Consider this datatype (Motzkin trees):

data Tree = Zero | One Tree | Two Tree Tree

T = 1+ T + T 2

Show that T 5 ∼= T

I by a nonsense argument using complex numbers

I by composing bijections (the penny game)I implement the function and its inverse in a language of your

choice

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 45: Seven trees in one

Challenge

Consider this datatype (Motzkin trees):

data Tree = Zero | One Tree | Two Tree Tree

T = 1+ T + T 2

Show that T 5 ∼= T

I by a nonsense argument using complex numbersI by composing bijections (the penny game)

I implement the function and its inverse in a language of yourchoice

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 46: Seven trees in one

Challenge

Consider this datatype (Motzkin trees):

data Tree = Zero | One Tree | Two Tree Tree

T = 1+ T + T 2

Show that T 5 ∼= T

I by a nonsense argument using complex numbersI by composing bijections (the penny game)I implement the function and its inverse in a language of your

choice

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 47: Seven trees in one

Photo credits

I The Druid’s Grove, Norbury Park: Ancient Yew Trees byThomas Allom 1804-1872http://www.victorianweb.org/art/illustration/allom/1.html

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one

Page 48: Seven trees in one

End

Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one