このページは http://www.slideshare.net/MarkHopkins5/seven-trees-in-one の内容を掲載しています。

掲載を希望されないスライド著者の方は、こちらよりご連絡下さい。

1年以上前 (2015/05/21)にアップロードin学び

There exists a surprising O(1) bijection between the datatype of unlabelled (planar) binary trees...

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

- Seven trees in one

Mark Hopkins

@antiselfdual

Commonwealth Bank

LambdaJam 2015

Mark Hopkins @antiselfdual Commonwealth Bank

Seven trees in one - T = 1 + T 2

Unlabelled binary trees

data Tree = Leaf | Node Tree Tree

Mark Hopkins @antiselfdual Commonwealth Bank

Unlabelled binary trees

data Tree = Leaf | Node Tree Tree

T = 1 + T 2

Mark Hopkins @antiselfdual Commonwealth Bank

T 2 − T + 1 = 0

√

−b ±

b2 − 4ac

T =

2a

√

= 1 ±

3 i

2

2

= e±πi/3

T = 1 + T 2

Suspend disbelief, and solve for T .

Mark Hopkins @antiselfdual Commonwealth Bank

√

−b ±

b2 − 4ac

T =

2a

√

= 1 ±

3 i

2

2

= e±πi/3

T = 1 + T 2

Suspend disbelief, and solve for T .

T 2 − T + 1 = 0

Mark Hopkins @antiselfdual Commonwealth Bank

√

= 1 ±

3 i

2

2

= e±πi/3

T = 1 + T 2

Suspend disbelief, and solve for T .

T 2 − T + 1 = 0

√

−b ±

b2 − 4ac

T =

2a

Mark Hopkins @antiselfdual Commonwealth Bank

= e±πi/3

T = 1 + T 2

Suspend disbelief, and solve for T .

T 2 − T + 1 = 0

√

−b ±

b2 − 4ac

T =

2a

√

= 1 ±

3 i

2

2

Mark Hopkins @antiselfdual Commonwealth Bank

T = 1 + T 2

Suspend disbelief, and solve for T .

T 2 − T + 1 = 0

√

−b ±

b2 − 4ac

T =

2a

√

= 1 ±

3 i

2

2

= e±πi/3

Mark Hopkins @antiselfdual Commonwealth Bank

Im

i

T

1

Re

−1

−T

−i

Mark Hopkins @antiselfdual Commonwealth Bank

No, obviously wrong.

What about

T 7 = T ?

Not obviously wrong . . .

⇒

true!

So T 6 = 1.

Mark Hopkins @antiselfdual Commonwealth Bank

What about

T 7 = T ?

Not obviously wrong . . .

⇒

true!

So T 6 = 1. No, obviously wrong.

Mark Hopkins @antiselfdual Commonwealth Bank

Not obviously wrong . . .

⇒

true!

So T 6 = 1. No, obviously wrong.

What about

T 7 = T ?

Mark Hopkins @antiselfdual Commonwealth Bank

⇒

true!

So T 6 = 1. No, obviously wrong.

What about

T 7 = T ?

Not obviously wrong . . .

Mark Hopkins @antiselfdual Commonwealth Bank

So T 6 = 1. No, obviously wrong.

What about

T 7 = T ?

Not obviously wrong . . .

⇒

true!

Mark Hopkins @antiselfdual Commonwealth Bank

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.

Theorem

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

Mark Hopkins @antiselfdual Commonwealth Bank

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.

Theorem

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

i.e.

we can pattern match on any 7-tuple of trees and put them

together into one tree.

Mark Hopkins @antiselfdual Commonwealth Bank

Actually holds for any k = 1 mod 6.

Not true for other values.

Theorem

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

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

Not true for other values.

Theorem

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

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

Theorem

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

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

Not surjective, since we can never reach Leaf.

T 2 → T

f :: ( Tree , Tree ) → Tree

t t1 t2 = Node t1 t2

Mark Hopkins @antiselfdual Commonwealth Bank

T 2 → T

f :: ( Tree , Tree ) → Tree

t t1 t2 = Node t1 t2

Not surjective, since we can never reach Leaf.

Mark Hopkins @antiselfdual Commonwealth Bank

Not surjective either.

T → T 2

f :: Tree → ( Tree , Tree )

f t = Node t Leaf

Mark Hopkins @antiselfdual Commonwealth Bank

T → T 2

f :: Tree → ( Tree , Tree )

f t = Node t Leaf

Not surjective either.

Mark Hopkins @antiselfdual Commonwealth Bank

Bijective! but not O(1).

T 2 → T , but cleverer

f :: ( Tree , Tree ) → Tree

f ( t1 , t2 ) = go ( Node t1 t2 )

wh ere

go t

= if l e f t O n l y t then left t else t

l e f t O n l y t = t == Leaf

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

Mark Hopkins @antiselfdual Commonwealth Bank

but not O(1).

T 2 → T , but cleverer

f :: ( Tree , Tree ) → Tree

f ( t1 , t2 ) = go ( Node t1 t2 )

wh ere

go t

= if l e f t O n l y t then left t else t

l e f t O n l y t = t == Leaf

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

Bijective!

Mark Hopkins @antiselfdual Commonwealth Bank

T 2 → T , but cleverer

f :: ( Tree , Tree ) → Tree

f ( t1 , t2 ) = go ( Node t1 t2 )

wh ere

go t

= if l e f t O n l y t then left t else t

l e f t O n l y t = t == Leaf

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

Bijective! but not O(1).

Mark Hopkins @antiselfdual Commonwealth Bank

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

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

Where did this come from

T = 1 + T 2

Mark Hopkins @antiselfdual Commonwealth Bank

Where did this come from

T = 1 + T 2

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

Mark Hopkins @antiselfdual Commonwealth Bank

Penny game

T 0

T 1

T 2

T 3

T 4

T 5

T 6

T 7

T 8

start with a penny in position 1.

aim is to move it to position 7 by splitting and combining

Mark Hopkins @antiselfdual Commonwealth Bank

Mark Hopkins @antiselfdual Commonwealth Bank

Mark Hopkins @antiselfdual Commonwealth Bank

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.

Why did this work?

If we have a type isomorphism T ∼

= p(T ) then

Mark Hopkins @antiselfdual Commonwealth Bank

⇒ 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.

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

⇒ q1(z) ∼

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

And, under some conditions, the reverse implications hold.

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

And, under some conditions, the reverse implications hold.

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

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

Summary

Simple arithmetic helps us find non-obvious type isomorphisms

Mark Hopkins @antiselfdual Commonwealth Bank

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

Extensions

Are there extensions to datatypes of decorated trees?

(multivariate polynomials)

What applications are there?

Mark Hopkins @antiselfdual Commonwealth Bank

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

parts

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

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

Rich theory behind isomorphisms of polynomial types

brings together a number of fields

distributive categories

theory of rigs (semirings)

combinatorial species

type theory

Mark Hopkins @antiselfdual Commonwealth Bank

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

by composing bijections (the penny game)

implement the function and its inverse in a language of your

choice

T = 1 + T + T 2

Show that T 5 ∼

= T

by a nonsense argument using complex numbers

Challenge

Consider this datatype (Motzkin trees):

data Tree = Zero | One Tree | Two Tree Tree

Mark Hopkins @antiselfdual Commonwealth Bank

implement the function and its inverse in a language of your

choice

Show that T 5 ∼

= T

by a nonsense argument using complex numbers

by composing bijections (the penny game)

Challenge

Consider this datatype (Motzkin trees):

data Tree = Zero | One Tree | Two Tree Tree

T = 1 + T + T 2

Mark Hopkins @antiselfdual Commonwealth Bank

Challenge

Consider this datatype (Motzkin trees):

data Tree = Zero | One Tree | Two Tree Tree

T = 1 + T + T 2

Show that T 5 ∼

= 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