Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of...

20
Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion) Jeffrey Considine (Boston University)

Transcript of Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of...

Page 1: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Efficient Algorithms for Isomorphisms of Simple Types

Yoav ZibinTechnion—Israel Institute of

TechnologyJoint work with:

Joseph (Yossi) Gil (Technion)Jeffrey Considine (Boston University)

Page 2: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Type Isomorphism

Two types are isomorphic iff there is a one-to-one mapping between their values, for example

Easier to grasp in arithmetical notation

int real string int real string

int real string real strin

int real string int real int strin

g nt

g

i

II R R

I I I

S S

R

I R S S I

S

R

R S

(Distributive)

(Currying)

(Associative & Commutative)

Page 3: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

First Order Ishomorphism

Tarski’s High-School Algebra Problem [1951]:

The following axioms are complete if the expressions involve only products and exponentiations[Soloviev’83]

CB B

C C C

C

AB BA

A B

AB

A

A

C AB

B

A

C

e e dd d ea bc c a b

?

Page 4: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

The problem and Our results

Input: two types of size n, given as expression trees Output: are the types isomorphic?

Key idea: solve the problem for all sub-expressions of the two types

Input: a collection of types whose total size n Output: a partitioning into equivalence classes

Varianttimespace

First order isomorphismn2 log n n log2 n

n2 n

Linear isomorphism(without the distributive axiom )

n log n nn n

Page 5: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Practical Motivation

Search for a function in a large library, using its type as a key

Functions with isomorphic types are returned Example (using second order isomorphism)

We only deal with first order isomorphism

LanguageNameType

ML of EdinburghCAML

itlistlist_it

(`a`b`b)`a list`b`b

Haskellfoldl(`b`a`b)`b`a list`b

SML of New Jerseyfold(`a `b`b)`a list`b`b

The Edinburgh SML Library

fold_left

(`a `b`b)`b`a list`b

Page 6: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Linear Isomorphism

Without the distributive axiom Essence of previous algorithms

Stage 1: bring types to a normal form Stage 2: sort the terms of product types Stage 3: compare the resulting structures

Our Observation: Sorting Multi-set equality Time: O(n log n) O(n) Example:

abracadabra = carrabadaba Sorting: aaaaabbcdrr Multi-set equality: [in the paper]

?

Page 7: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Our Normal form for Linear Isomorphism

Exhaustively apply the rule The representation remains linear

Alternating products-functions

BCB CA A

ab cd e dc ba

a

b c d e d c b a

Page 8: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Comparing normal forms

a

b c d e d c b a

c

d a b eb a d c

a

b c d e d c b a

c

d a b eb a d c

a

b c d e d c b a

c

d a b eb a d c

For height=0: partition primitive types For odd heights: partition products (as multi-sets) For even heights: partition functions (as ordered pairs)

Iterate by height

a

b c d e d c b a

c

d a b eb a d c

a

b c d e d c b a

c

d a b eb a d c

The types are isomorphic

Page 9: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Back to First Order Isomorphism

Exhaustively apply :

Recursively sort the terms of each product

Rule 1 (R.1):

Rule 2 (R.2):

BC

C

B

C C

CA A

AB A B

R.2 R.1 R.2

R.2 R.1,R.1

d e e e de ded

e dd e

e de e

dd e e d ee e e d

a bc a bc a bc a b c

c a b c a b c a b

sort terms

sort terms

e de de e de de

de e ed e de de

a b c a b c

c a b a b c

The equality is

true

Page 10: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Catch: exponential blowup

Due to the distributive law:

a

a ab c

a a a ab c b c

a a a a a a a ab c b c c b c

bc

b

de

d

d e

d e d e

e

fg

fg

fg

f g

C C CAB A B

The “C ” sub-expression is

duplicated

Page 11: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Expression Tree Graph

Apply instead the “sharing” rule:

The resulting graph is a directed acyclic graph (DAG)

Could still lead to O(n2) space [Next Slide] This rule increase the representation by a constant It can be applied at most n2 times

C A BAB

C

The “C ” sub-expression is shared

Page 12: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Our observationExhaustively apply the sharing rule, with the “outer-most” opportunity first

hh

gg

gf

f

f

f

a b cd

a b cd

a b cd

a b c d

f f

g g

hg

h h

h

a b cd

a bc d

ab c d

a b c d

inner-most 1st: O(n2 ) space outer-most 1st: O(n) space

Page 13: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Sharing of terms in products

mnmn

fde def

de def

def

ff

de de

def def

a bc gh

a bc gh

a bc gh

a b c gh

a b c g h

m n

d e ff

d e

Sharing forest

Page 14: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Sharing (cont.)

Products have 3 kinds of terms Primitive types: a, b, c, … Exponents: XY

Shared products: , , , , , , , , … Catch question: how to discover that and are

isomorphic? Naïve solution:

Calculate the inherited termsi-terms()= i-terms()={d,e,f,m,n}

Requires O(n2) time and space Tree Partitioning [next slides]

Requires O(n log2 n) time and O(n) space

m n

d e ff

d e

Sharing forest

Page 15: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Tree Partitioning

Input: a tree , and a multi-set terms(v) for each node v

Output: a partitioning of the nodes according to the inherited multi-sets i-terms(v)

{ }

{a ,b,c} {a,a}

{d}{ }

{ }{a ,c,d}

{ }

{a,a}

{a,b,c,d} {a,b,c,d,a,c,d}

{a,b,c}

{ } terms() = {d}i-terms() = {a,b,c,d}

Page 16: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Dual representation

terms() = {}

terms() = {a,b,c}

terms() = {}

terms() = {d}

terms() = {}

terms() = {a,c,d}

terms() = {}

terms() = {a,a}

Fa = { ,,,}

Fb = { }

Fc = { ,}

Fd = { ,}

Multi-sets of nodes (products) in which the value (term) occurs

Page 17: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Efficient representation of families

Find a preorder of the tree Descendants of a node define an interval

A family F defines |F | intervals,which partition the preorderinto at most 2|F |+1 segments

Example: Fa = { ,,,}

{a} {a,a}

{a}

1 2 1 20F a

Page 18: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Intersecting all partitions

1 2 1 20

1 00

1 2 1 00

1 2 1 00

1 2 1 30

2 3 2 010

2 3 2 410

Fb

F a

F a Fb

F a Fb F c Fd

F c Fd

Fd

F c

A solution for the Tree

Partitioning problem

Page 19: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

Open problems

Our algorithms runs in O(n log2 n) time Reduce this time Obtain lower bounds

Search for a linear-time random algorithm Our algorithm assumed the input type is

represented as an expression tree Generalize our algorithm for a DAG

representation A subtyping algorithm

Page 20: Efficient Algorithms for Isomorphisms of Simple Types Yoav Zibin Technion—Israel Institute of Technology Joint work with: Joseph (Yossi) Gil (Technion)

The End

Any questions?