Upload
mark-hopkins
View
131
Download
3
Embed Size (px)
Citation preview
Seven trees in one
Mark Hopkins@antiselfdual
Commonwealth Bank
LambdaJam 2015
Mark Hopkins @antiselfdual Commonwealth Bank 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
Unlabelled binary trees
data Tree = Leaf | Node Tree Tree
T = 1+ T 2
Mark Hopkins @antiselfdual Commonwealth Bank 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
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
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
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
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
Re
Im
1
−1
i
−i
T
−T
Mark Hopkins @antiselfdual Commonwealth Bank 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
So T 6 = 1. No, obviously wrong.
What aboutT 7 = T?
Not obviously wrong . . .⇒ true!
Mark Hopkins @antiselfdual Commonwealth Bank 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
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
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
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
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
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
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
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
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
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
T → T 2
f :: Tree → (Tree , Tree)f t = Node t Leaf
Not surjective either.
Mark Hopkins @antiselfdual Commonwealth Bank 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
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
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
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
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
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
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
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
Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
Mark Hopkins @antiselfdual Commonwealth Bank 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
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
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
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
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
Summary
I Simple arithmetic helps us find non-obvious type isomorphisms
Mark Hopkins @antiselfdual Commonwealth Bank 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
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
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
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
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
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
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
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
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
End
Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one