Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
Próxima SlideShare
Cargando en…5
×

# Seven trees in one

576 visualizaciones

There exists a surprising O(1) bijection between the datatype of unlabelled (planar) binary trees and 7-tuples of these trees. This presentation shows how this comes about.

Sample code at https://github.com/mjhopkins/seven-trees

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Sé el primero en comentar

### Seven trees in one

1. 1. Seven trees in one Mark Hopkins @antiselfdual Commonwealth Bank LambdaJam 2015 Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
2. 2. Unlabelled binary trees data Tree = Leaf | Node Tree Tree Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
3. 3. Unlabelled binary trees data Tree = Leaf | Node Tree Tree T = 1 + T2 Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
4. 4. T = 1 + T2 Suspend disbelief, and solve for T. Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
5. 5. T = 1 + T2 Suspend disbelief, and solve for T. T2 − T + 1 = 0 Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
6. 6. T = 1 + T2 Suspend disbelief, and solve for T. T2 − T + 1 = 0 T = −b ± √ b2 − 4ac 2a Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
7. 7. T = 1 + T2 Suspend disbelief, and solve for T. T2 − T + 1 = 0 T = −b ± √ b2 − 4ac 2a = 1 2 ± √ 3 2 i Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
8. 8. T = 1 + T2 Suspend disbelief, and solve for T. T2 − T + 1 = 0 T = −b ± √ b2 − 4ac 2a = 1 2 ± √ 3 2 i = e±πi/3 Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
9. 9. Re Im 1 −1 i −i T −T Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
10. 10. So T6 = 1. Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
11. 11. So T6 = 1. No, obviously wrong. Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
12. 12. So T6 = 1. No, obviously wrong. What about T7 = T? Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
13. 13. So T6 = 1. No, obviously wrong. What about T7 = T? Not obviously wrong. . . Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
14. 14. So T6 = 1. No, obviously wrong. What about T7 = T? Not obviously wrong. . . ⇒ true! Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
15. 15. Theorem There exists an O(1) bijective function from T to T7. Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
16. 16. Theorem There exists an O(1) bijective function from T to T7. i.e. we can pattern match on any 7-tuple of trees and put them together into one tree. Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
17. 17. Theorem There exists an O(1) bijective function from T to T7. i.e. we can pattern match on any 7-tuple of trees and put them together into one tree. we can decompose any tree into the same seven trees it came from. Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
18. 18. Theorem There exists an O(1) bijective function from T to T7. i.e. we can pattern match on any 7-tuple of trees and put them together into one tree. we can decompose any tree into the same seven trees it came from. Actually holds for any k = 1 mod 6. Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
19. 19. Theorem There exists an O(1) bijective function from T to T7. i.e. we can pattern match on any 7-tuple of trees and put them together into one tree. 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
20. 20. T2 → T f :: (Tree , Tree) → Tree t t1 t2 = Node t1 t2 Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
21. 21. T2 → T f :: (Tree , Tree) → Tree t t1 t2 = Node t1 t2 Not surjective, since we can never reach Leaf. Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
22. 22. T → T2 f :: Tree → (Tree , Tree) f t = Node t Leaf Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
23. 23. T → T2 f :: Tree → (Tree , Tree) f t = Node t Leaf Not surjective either. Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
24. 24. T2 → T, but cleverer f :: (Tree , Tree) → Tree f (t1, t2) = go (Node t1 t2) where go t = if leftOnly t then left t else t leftOnly t = t == Leaf || right t == Leaf && leftOnly (left t) Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
25. 25. T2 → T, but cleverer f :: (Tree , Tree) → Tree f (t1, t2) = go (Node t1 t2) where go t = if leftOnly t then left t else t leftOnly t = t == Leaf || right t == Leaf && leftOnly (left t) Bijective! Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
26. 26. T2 → T, but cleverer f :: (Tree , Tree) → Tree f (t1, t2) = go (Node t1 t2) where go t = if leftOnly t then left t else t leftOnly t = t == Leaf || right t == Leaf && leftOnly (left t) Bijective! but not O(1). Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
27. 27. 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
28. 28. Where did this come from T = 1 + T2 Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
29. 29. Where did this come from T = 1 + T2 Tk = Tk−1 + Tk+1 Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
30. 30. Penny game T0 T1 T2 T3 T4 T5 T6 T7 T8 start with a penny in position 1. aim is to move it to position 7 by splitting and combining Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
31. 31. Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
32. 32. Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
33. 33. Why did this work? If we have a type isomorphism T ∼= p(T) then Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
34. 34. 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) Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
35. 35. 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) Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
36. 36. 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. Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
37. 37. 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
38. 38. Summary Simple arithmetic helps us ﬁnd non-obvious type isomorphisms Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
39. 39. Extensions Are there extensions to datatypes of decorated trees? (multivariate polynomials) What applications are there? Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
40. 40. Extensions Are there extensions to datatypes of decorated trees? (multivariate polynomials) What applications are there? important when writing a compiler to know when two types are isomomorphic Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
41. 41. Extensions Are there extensions to datatypes of decorated trees? (multivariate polynomials) What applications are there? important when writing a compiler to know when two types are isomomorphic It could interesting to split up a tree-shaped stream into seven parts Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
42. 42. Rich theory behind isomorphisms of polynomial types brings together a number of ﬁelds distributive categories theory of rigs (semirings) combinatorial species type theory Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
43. 43. Further reading Seven Trees in one, Andreas Blass, Journal of Pure and Applied Algebra On the generic solution to P(X) = X in distributive categories, Robbie Gates Objects of Categories as Complex Numbers, Marcelo Fiore and Tom Leinster An Objective Representation of the Gaussian Integers, Marcelo Fiore and Tom Leinster http://rfcwalters.blogspot.com.au/2010/06/robbie-gates-on- seven-trees-in-one.html http://blog.sigfpe.com/2007/09/arboreal-isomorphisms-from- nuclear.html Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
44. 44. Challenge Consider this datatype (Motzkin trees): data Tree = Zero | One Tree | Two Tree Tree Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
45. 45. Challenge Consider this datatype (Motzkin trees): data Tree = Zero | One Tree | Two Tree Tree T = 1 + T + T2 Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
46. 46. Challenge Consider this datatype (Motzkin trees): data Tree = Zero | One Tree | Two Tree Tree T = 1 + T + T2 Show that T5 ∼= T by a nonsense argument using complex numbers by composing bijections (the penny game) implement the function and its inverse in a language of your choice Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
47. 47. Photo credits The Druid’s Grove, Norbury Park: Ancient Yew Trees by Thomas Allom 1804-1872 http://www.victorianweb.org/art/illustration/allom/1.html Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one
48. 48. End Mark Hopkins @antiselfdual Commonwealth Bank Seven trees in one