85
Non-linear Associative-Commutative Many-to-One Pattern Matching with Sequence Variables Manuel Krebber June 30, 2017 Manuel Krebber | June 30, 2017 0/31 Non-linear Associative-Commutative Many-to-One Pattern Matching with Sequence V Manuel Krebber | June 30, 2017 1/31

Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Non-linear Associative-CommutativeMany-to-One Pattern Matching with

Sequence Variables

Manuel Krebber

June 30, 2017

Manuel Krebber | June 30, 2017 0/31

Non-linear Associative-Commutative Many-to-One Pattern Matching with Sequence Variables

Manuel Krebber | June 30, 2017 1/31

Page 2: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

1 Introduction

2 Types of Matching

3 Algorithms

4 Experiments

5 Conclusions

Introduction

Table of Contents

Manuel Krebber | June 30, 2017 2/31

Page 3: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Function Symbols: f , g

Constant Symbols: a, b, c

Variables: x

x

x ,yyy

Examples: a, f (xxx , b),f (g(a, b), c)

f

g

a

b

c

Introduction

Term

Manuel Krebber | June 30, 2017 3/31

Page 4: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Function Symbols: f , g

Constant Symbols: a, b, c

Variables: x

x

x ,yyy

Examples: a, f (xxx , b),f (g(a, b), c)

f

g

a

b

c

Introduction

Term

Manuel Krebber | June 30, 2017 3/31

Page 5: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Function Symbols: f , g

Constant Symbols: a, b, c

Variables: x

x

x ,yyy

Examples: a, f (xxx , b),f (g(a, b), c)

f

g

a

b

c

Introduction

Term

Manuel Krebber | June 30, 2017 3/31

Page 6: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Function Symbols: f , g

Constant Symbols: a, b, c

Variables: x

x

x ,yyy

Examples: a, f (xxx , b),f (g(a, b), c)

f

g

a

b

c

Introduction

Term

Manuel Krebber | June 30, 2017 3/31

Page 7: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Definition: Find substitution � : X 9 T such that�̂(pattern) = subject

Example: f (xxx ,yyy)

f (a, g(b))

x

x

x 7! a

y

y

y 7! g(b)

Subject:

Pattern:

Introduction

Pattern Matching

Manuel Krebber | June 30, 2017 4/31

Page 8: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Definition: Find substitution � : X 9 T such that�̂(pattern) = subject

Example: f (xxx ,yyy)

f (a, g(b))

x

x

x 7! a

y

y

y 7! g(b)

Subject:

Pattern:

Introduction

Pattern Matching

Manuel Krebber | June 30, 2017 4/31

Page 9: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Definition: Find substitution � : X 9 T such that�̂(pattern) = subject

Example: f (xxx ,yyy)

f (a, g(b))

x

x

x 7! a

y

y

y 7! g(b)

Subject:

Pattern:

Introduction

Pattern Matching

Manuel Krebber | June 30, 2017 4/31

Page 10: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Functional programming languages

I Computer algebra systems (Mathematica)I Term rewriting systemsI In this case: Linear Algebra

Introduction

Applications

Manuel Krebber | June 30, 2017 5/31

Page 11: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Functional programming languagesI Computer algebra systems (Mathematica)

I Term rewriting systemsI In this case: Linear Algebra

Introduction

Applications

Manuel Krebber | June 30, 2017 5/31

Page 12: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Functional programming languagesI Computer algebra systems (Mathematica)I Term rewriting systems

I In this case: Linear Algebra

Introduction

Applications

Manuel Krebber | June 30, 2017 5/31

Page 13: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Functional programming languagesI Computer algebra systems (Mathematica)I Term rewriting systemsI In this case: Linear Algebra

Introduction

Applications

Manuel Krebber | June 30, 2017 5/31

Page 14: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

1 Introduction

2 Types of Matching

3 Algorithms

4 Experiments

5 Conclusions

Types of Matching

Table of Contents

Manuel Krebber | June 30, 2017 6/31

Page 15: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Syntactic

I Linear (xxx + y

y

y) vs. non-linear (xxx + x

x

x)I Sequence variablesI AssociativityI CommutativityI Many-to-one vs. one-to-one

Types of Matching

Types of Matching

Manuel Krebber | June 30, 2017 7/31

Page 16: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I SyntacticI Linear (xxx + y

y

y) vs. non-linear (xxx + x

x

x)

I Sequence variablesI AssociativityI CommutativityI Many-to-one vs. one-to-one

Types of Matching

Types of Matching

Manuel Krebber | June 30, 2017 7/31

Page 17: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I SyntacticI Linear (xxx + y

y

y) vs. non-linear (xxx + x

x

x)I Sequence variablesI AssociativityI Commutativity

I Many-to-one vs. one-to-one

Types of Matching

Types of Matching

Manuel Krebber | June 30, 2017 7/31

Page 18: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I SyntacticI Linear (xxx + y

y

y) vs. non-linear (xxx + x

x

x)I Sequence variablesI AssociativityI CommutativityI Many-to-one vs. one-to-one

Types of Matching

Types of Matching

Manuel Krebber | June 30, 2017 7/31

Page 19: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

(A⇥ B)⇥ C

A B

C

=A⇥ (B ⇥ C )

A

B C

Canonical VariadicForm

Types of Matching

Associativity I

Manuel Krebber | June 30, 2017 8/31

Page 20: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

(A⇥ B)⇥ C

A B

C

=A⇥ (B ⇥ C )

A

B C

Canonical VariadicForm

Types of Matching

Associativity I

Manuel Krebber | June 30, 2017 8/31

Page 21: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

(A⇥ B)⇥ C

A B

C

=A⇥ (B ⇥ C )

A

B C

= A⇥ B ⇥ C

A B C

Canonical VariadicForm

Types of Matching

Associativity I

Manuel Krebber | June 30, 2017 8/31

Page 22: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

X

X

X ⇥M3

X

X

X

M3

(M1 ⇥M2)⇥M3

M1 M2

M3

M1 ⇥M2 ⇥M3

M1 M2 M3

� = {XXX 7! (M1 ⇥M2)}

Types of Matching

Associativity II

Manuel Krebber | June 30, 2017 9/31

Page 23: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

X

X

X ⇥M3

X

X

X

M3

(M1 ⇥M2)⇥M3

M1 M2

M3

M1 ⇥M2 ⇥M3

M1 M2 M3

� = {XXX 7! (M1 ⇥M2)}

Types of Matching

Associativity II

Manuel Krebber | June 30, 2017 9/31

Page 24: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

X

X

X ⇥M3

X

X

X

M3

(M1 ⇥M2)⇥M3

M1 M2

M3

M1 ⇥M2 ⇥M3

M1 M2 M3

� = {XXX 7! (M1 ⇥M2)}

Types of Matching

Associativity II

Manuel Krebber | June 30, 2017 9/31

Page 25: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

X

X

X ⇥M3

X

X

X

M3

(M1 ⇥M2)⇥M3

M1 M2

M3

M1 ⇥M2 ⇥M3

M1 M2 M3

� = {XXX 7! (M1 ⇥M2)}

Types of Matching

Associativity II

Manuel Krebber | June 30, 2017 9/31

Page 26: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Can match a sequence of terms

Notation: x

x

x

⇤⇤⇤ star variable, xxx+++ plus variablestar variables can match empty sequence

Example: �(f (a,xxx+++, d)) = f (a, b, c , d)� = {xxx+++ 7! (b, c)}

Associativity: �(fa(a,xxx , d)) = fa(a, b, c , d)� = {xxx 7! fa(b, c)}

Types of Matching

Sequence Variables

Manuel Krebber | June 30, 2017 10/31

Page 27: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Can match a sequence of termsNotation: x

x

x

⇤⇤⇤ star variable, xxx+++ plus variablestar variables can match empty sequence

Example: �(f (a,xxx+++, d)) = f (a, b, c , d)� = {xxx+++ 7! (b, c)}

Associativity: �(fa(a,xxx , d)) = fa(a, b, c , d)� = {xxx 7! fa(b, c)}

Types of Matching

Sequence Variables

Manuel Krebber | June 30, 2017 10/31

Page 28: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Can match a sequence of termsNotation: x

x

x

⇤⇤⇤ star variable, xxx+++ plus variablestar variables can match empty sequence

Example: �(f (a,xxx+++, d)) = f (a, b, c , d)� = {xxx+++ 7! (b, c)}

Associativity: �(fa(a,xxx , d)) = fa(a, b, c , d)� = {xxx 7! fa(b, c)}

Types of Matching

Sequence Variables

Manuel Krebber | June 30, 2017 10/31

Page 29: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Can match a sequence of termsNotation: x

x

x

⇤⇤⇤ star variable, xxx+++ plus variablestar variables can match empty sequence

Example: �(f (a,xxx+++, d)) = f (a, b, c , d)� = {xxx+++ 7! (b, c)}

Associativity: �(fa(a,xxx , d)) = fa(a, b, c , d)� = {xxx 7! fa(b, c)}

Types of Matching

Sequence Variables

Manuel Krebber | June 30, 2017 10/31

Page 30: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Can match a sequence of termsNotation: x

x

x

⇤⇤⇤ star variable, xxx+++ plus variablestar variables can match empty sequence

Example: �(f (a,xxx+++, d)) = f (a, b, c , d)� = {xxx+++ 7! (b, c)}

Associativity: �(fa(a,xxx , d)) = fa(a, b, c , d)� = {xxx 7! fa(b, c)}

Types of Matching

Sequence Variables

Manuel Krebber | June 30, 2017 10/31

Page 31: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Can match a sequence of termsNotation: x

x

x

⇤⇤⇤ star variable, xxx+++ plus variablestar variables can match empty sequence

Example: �(f (a,xxx+++, d)) = f (a, b, c , d)� = {xxx+++ 7! (b, c)}

Associativity: �(fa(a,xxx , d)) = fa(a, b, c , d)� = {xxx 7! fa(b, c)}

Types of Matching

Sequence Variables

Manuel Krebber | June 30, 2017 10/31

Page 32: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Ia+ b = b + a

I Sort the arguments: d + a+ c + b ! a+ b + c + d

I Every permutation could match:n! with n = |subject|

Types of Matching

Commutativity

Manuel Krebber | June 30, 2017 11/31

Page 33: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Ia+ b = b + a

I Sort the arguments: d + a+ c + b ! a+ b + c + d

I Every permutation could match:n! with n = |subject|

Types of Matching

Commutativity

Manuel Krebber | June 30, 2017 11/31

Page 34: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Ia+ b = b + a

I Sort the arguments: d + a+ c + b ! a+ b + c + d

I Every permutation could match:n! with n = |subject|

Types of Matching

Commutativity

Manuel Krebber | June 30, 2017 11/31

Page 35: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Synt Assoc SeqVar Comm All

Max. # matches 1�n�1m�1

� �n+m�1m�1

�n! n

m

NP complete no yes yes yes yes

n = |subject|, m = |pattern|

Types of Matching

Complexity

Manuel Krebber | June 30, 2017 12/31

Page 36: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Many patterns, one subject

I Speedup by simultaneous matchingI Use similarity between patterns

Types of Matching

Many-to-one Matching

Manuel Krebber | June 30, 2017 13/31

Page 37: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Many patterns, one subjectI Speedup by simultaneous matching

I Use similarity between patterns

Types of Matching

Many-to-one Matching

Manuel Krebber | June 30, 2017 13/31

Page 38: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Many patterns, one subjectI Speedup by simultaneous matchingI Use similarity between patterns

Types of Matching

Many-to-one Matching

Manuel Krebber | June 30, 2017 13/31

Page 39: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Algorithms

Table of Contents

Manuel Krebber | June 30, 2017 14/31

Page 40: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Algorithms - One-to-One

Table of Contents

Manuel Krebber | June 30, 2017 15/31

Page 41: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Example: Subject f (a, b) and pattern f (xxx⇤⇤⇤,yyy⇤⇤⇤)

I Try every possible distributionI Generate (weak) compositions:

0 + 2 = 2 {xxx⇤⇤⇤ 7! (),yyy⇤⇤⇤ 7! (a, b)}1 + 1 = 2 {xxx⇤⇤⇤ 7! (a),yyy⇤⇤⇤ 7! (b)}2 + 0 = 2 {xxx⇤⇤⇤ 7! (a, b),yyy⇤⇤⇤ 7! ()}

I Backtracking

Algorithms - One-to-One

Associative and Sequence Variables

Manuel Krebber | June 30, 2017 16/31

Page 42: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Example: Subject f (a, b) and pattern f (xxx⇤⇤⇤,yyy⇤⇤⇤)

I Try every possible distribution

I Generate (weak) compositions:

0 + 2 = 2 {xxx⇤⇤⇤ 7! (),yyy⇤⇤⇤ 7! (a, b)}1 + 1 = 2 {xxx⇤⇤⇤ 7! (a),yyy⇤⇤⇤ 7! (b)}2 + 0 = 2 {xxx⇤⇤⇤ 7! (a, b),yyy⇤⇤⇤ 7! ()}

I Backtracking

Algorithms - One-to-One

Associative and Sequence Variables

Manuel Krebber | June 30, 2017 16/31

Page 43: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Example: Subject f (a, b) and pattern f (xxx⇤⇤⇤,yyy⇤⇤⇤)

I Try every possible distributionI Generate (weak) compositions:

0 + 2 = 2 {xxx⇤⇤⇤ 7! (),yyy⇤⇤⇤ 7! (a, b)}1 + 1 = 2 {xxx⇤⇤⇤ 7! (a),yyy⇤⇤⇤ 7! (b)}2 + 0 = 2 {xxx⇤⇤⇤ 7! (a, b),yyy⇤⇤⇤ 7! ()}

I Backtracking

Algorithms - One-to-One

Associative and Sequence Variables

Manuel Krebber | June 30, 2017 16/31

Page 44: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Example: Subject f (a, b) and pattern f (xxx⇤⇤⇤,yyy⇤⇤⇤)

I Try every possible distributionI Generate (weak) compositions:

0 + 2 = 2 {xxx⇤⇤⇤ 7! (),yyy⇤⇤⇤ 7! (a, b)}1 + 1 = 2 {xxx⇤⇤⇤ 7! (a),yyy⇤⇤⇤ 7! (b)}2 + 0 = 2 {xxx⇤⇤⇤ 7! (a, b),yyy⇤⇤⇤ 7! ()}

I Backtracking

Algorithms - One-to-One

Associative and Sequence Variables

Manuel Krebber | June 30, 2017 16/31

Page 45: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Pattern: fc(a, g(c , d), g(xxx),xxx ,yyy⇤⇤⇤, zzz)Subject: fc(a, b, g(b), c , g(c , d), e) Permutations: 6! = 720

Match: � = {zzz 7! e}

1. Constant terms2. Matched variables3. Terms containing variables4. Repeat step 25. Regular Variables6. Sequence Variables

Algorithms - One-to-One

Steps of Commutative Matching

Manuel Krebber | June 30, 2017 17/31

Page 46: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Pattern: fc(a, g(c , d), g(xxx),xxx ,yyy⇤⇤⇤, zzz)Subject: fc(a, b, g(b), c , g(c , d), e) Permutations: 6! = 720

Match: � = {zzz 7! e}1. Constant terms

2. Matched variables3. Terms containing variables4. Repeat step 25. Regular Variables6. Sequence Variables

Algorithms - One-to-One

Steps of Commutative Matching

Manuel Krebber | June 30, 2017 17/31

Page 47: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Pattern: fc(a, g(c , d), g(xxx),xxx ,yyy⇤⇤⇤, zzz)Subject: fc(a, b, g(b), c , g(c , d), e) Permutations: 4! = 24

Match: � = {zzz 7! e}1. Constant terms2. Matched variables

3. Terms containing variables4. Repeat step 25. Regular Variables6. Sequence Variables

Algorithms - One-to-One

Steps of Commutative Matching

Manuel Krebber | June 30, 2017 17/31

Page 48: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Pattern: fc(a, g(c , d), g(xxx),xxx ,yyy⇤⇤⇤, zzz)Subject: fc(a, b, g(b), c , g(c , d), e) Permutations: 3! = 6

Match: � = {zzz 7! e,xxx 7! b}1. Constant terms2. Matched variables3. Terms containing variables

4. Repeat step 25. Regular Variables6. Sequence Variables

Algorithms - One-to-One

Steps of Commutative Matching

Manuel Krebber | June 30, 2017 17/31

Page 49: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Pattern: fc(a, g(c , d), g(xxx),xxx ,yyy⇤⇤⇤, zzz)Subject: fc(a, b, g(b), c , g(c , d), e) Permutations: 2! = 6

Match: � = {zzz 7! e,xxx 7! b}1. Constant terms2. Matched variables3. Terms containing variables4. Repeat step 2

5. Regular Variables6. Sequence Variables

Algorithms - One-to-One

Steps of Commutative Matching

Manuel Krebber | June 30, 2017 17/31

Page 50: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Pattern: fc(a, g(c , d), g(xxx),xxx ,yyy⇤⇤⇤, zzz)Subject: fc(a, b, g(b), c , g(c , d), e) Permutations: 1! = 1

Match: � = {zzz 7! e,xxx 7! b}1. Constant terms2. Matched variables3. Terms containing variables4. Repeat step 25. Regular Variables

6. Sequence Variables

Algorithms - One-to-One

Steps of Commutative Matching

Manuel Krebber | June 30, 2017 17/31

Page 51: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Pattern: fc(a, g(c , d), g(xxx),xxx ,yyy⇤⇤⇤, zzz)Subject: fc(a, b, g(b), c , g(c , d), e) Permutations: 1! = 1

Match: � = {zzz 7! e,xxx 7! b,yyy⇤⇤⇤ 7! HcI}1. Constant terms2. Matched variables3. Terms containing variables4. Repeat step 25. Regular Variables6. Sequence Variables

Algorithms - One-to-One

Steps of Commutative Matching

Manuel Krebber | June 30, 2017 17/31

Page 52: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Match fc(xxx+++,xxx+++,yyy⇤⇤⇤) and fc(a, a, a, b, b, c)

Solve linear diophantine equations (using Extended Euclidean Alg.):

3 = 2xa + ya

2 = 2xb + yb

1 = 2xc + yc

1 xa + xb + xc

Solutions: {x 7! {a, b}, y 7! {a, c}}{x 7! {a}, y 7! {a, b, b, c}}{x 7! {b}, y 7! {a, a, a, c}}

Algorithms - One-to-One

Commutativity + Sequence Variables

Manuel Krebber | June 30, 2017 18/31

Page 53: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Match fc(xxx+++,xxx+++,yyy⇤⇤⇤) and fc(a, a, a, b, b, c)

Solve linear diophantine equations (using Extended Euclidean Alg.):

3 = 2xa + ya

2 = 2xb + yb

1 = 2xc + yc

1 xa + xb + xc

Solutions: {x 7! {a, b}, y 7! {a, c}}{x 7! {a}, y 7! {a, b, b, c}}{x 7! {b}, y 7! {a, a, a, c}}

Algorithms - One-to-One

Commutativity + Sequence Variables

Manuel Krebber | June 30, 2017 18/31

Page 54: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Match fc(xxx+++,xxx+++,yyy⇤⇤⇤) and fc(a, a, a, b, b, c)

Solve linear diophantine equations (using Extended Euclidean Alg.):

3 = 2xa + ya

2 = 2xb + yb

1 = 2xc + yc

1 xa + xb + xc

Solutions: {x 7! {a, b}, y 7! {a, c}}{x 7! {a}, y 7! {a, b, b, c}}{x 7! {b}, y 7! {a, a, a, c}}

Algorithms - One-to-One

Commutativity + Sequence Variables

Manuel Krebber | June 30, 2017 18/31

Page 55: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Algorithms - Many-to-One

Table of Contents

Manuel Krebber | June 30, 2017 19/31

Page 56: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Patterns: f (1,xxx⇤⇤⇤), f (1), f (yyy , 0)

f (1))

f (1,xxx⇤⇤⇤))x

x

x

⇤⇤⇤1

f (yyy , 0))0y

y

y

f (

Subject: f (1, 0)Match: f (yyy , 0) with {yyy 7! 1}

Algorithms - Many-to-One

Discrimination Net

Manuel Krebber | June 30, 2017 20/31

Page 57: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Patterns: f (1,xxx⇤⇤⇤), f (1), f (yyy , 0)

f (1))

f (1,xxx⇤⇤⇤))x

x

x

⇤⇤⇤1

f (yyy , 0))0y

y

y

f (

Subject: f (1, 0)Match: f (yyy , 0) with {yyy 7! 1}

Algorithms - Many-to-One

Discrimination Net

Manuel Krebber | June 30, 2017 20/31

Page 58: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Patterns: f (1,xxx⇤⇤⇤), f (1), f (yyy , 0)

f (1))

f (1,xxx⇤⇤⇤))x

x

x

⇤⇤⇤1

f (yyy , 0))0y

y

y

f (

Subject: f (1, 0)Match: f (yyy , 0) with {yyy 7! 1}

Algorithms - Many-to-One

Discrimination Net

Manuel Krebber | June 30, 2017 20/31

Page 59: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Patterns: f (1,xxx⇤⇤⇤), f (1), f (yyy , 0)

f (1))

f (1,xxx⇤⇤⇤))x

x

x

⇤⇤⇤1

f (yyy , 0))0y

y

y

f (

Subject: f (1, 0)Match: f (1,xxx⇤⇤⇤) with {xxx⇤⇤⇤ 7! (0)}

Algorithms - Many-to-One

Discrimination Net

Manuel Krebber | June 30, 2017 20/31

Page 60: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Multi Layer Discrimination Net (MLDN)

5a

a

4g(a))

a

3g(b))

b

2g(xxx))

x

xx

g(

1x

x

x

x

xx

fc(a,xxx ,xxx)H1, 1, 5I

fc(a, g(xxx), g(a))

H2, 4, 5Ifc(g(b), g(xxx))

H2, 3Ifc

fc(a, g(a), g(a)) ) H1, 1, 1, 2, 2, 4, 4, 5I

Algorithms - Many-to-One

Many-to-One for Commutative

Manuel Krebber | June 30, 2017 21/31

Page 61: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Multi Layer Discrimination Net (MLDN)

5a

a

4g(a))

a

3g(b))

b

2g(xxx))

x

xx

g(

1x

x

x

x

xx

fc(a,xxx ,xxx)H1, 1, 5I

fc(a, g(xxx), g(a))

H2, 4, 5Ifc(g(b), g(xxx))

H2, 3Ifc

fc(a, g(a), g(a)) ) H1, 1, 1, 2, 2, 4, 4, 5I

Algorithms - Many-to-One

Many-to-One for Commutative

Manuel Krebber | June 30, 2017 21/31

Page 62: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

1. Match subject arguments with nested DN

2. Build bipartite graph from matches3. Enumerate all maximum matchings (Hopcroft-Karp, Uno)4. Combine substitutions from matching5. Match sequence variables

Algorithms - Many-to-One

Many-to-One for Commutative

Manuel Krebber | June 30, 2017 22/31

Page 63: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

1. Match subject arguments with nested DN2. Build bipartite graph from matches

3. Enumerate all maximum matchings (Hopcroft-Karp, Uno)4. Combine substitutions from matching5. Match sequence variables

Algorithms - Many-to-One

Many-to-One for Commutative

Manuel Krebber | June 30, 2017 22/31

Page 64: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

1. Match subject arguments with nested DN2. Build bipartite graph from matches3. Enumerate all maximum matchings (Hopcroft-Karp, Uno)

4. Combine substitutions from matching5. Match sequence variables

Algorithms - Many-to-One

Many-to-One for Commutative

Manuel Krebber | June 30, 2017 22/31

Page 65: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

1. Match subject arguments with nested DN2. Build bipartite graph from matches3. Enumerate all maximum matchings (Hopcroft-Karp, Uno)4. Combine substitutions from matching

5. Match sequence variables

Algorithms - Many-to-One

Many-to-One for Commutative

Manuel Krebber | June 30, 2017 22/31

Page 66: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

1. Match subject arguments with nested DN2. Build bipartite graph from matches3. Enumerate all maximum matchings (Hopcroft-Karp, Uno)4. Combine substitutions from matching5. Match sequence variables

Algorithms - Many-to-One

Many-to-One for Commutative

Manuel Krebber | June 30, 2017 22/31

Page 67: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

f (a, b)

f (a, a)

f (yyy , b)

f (xxx ,yyy)

Subjectfc(f (a, b), f (a, a))

Patternfc(f (yyy , b), f (xxx ,yyy))

{yyy 7! a}

{x

x

x 7!a,y

y

y 7!b}

{xxx 7! a,yyy 7! a}

Algorithms - Many-to-One

Bipartite Graph

Manuel Krebber | June 30, 2017 23/31

Page 68: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

f (a, b)

f (a, a)

f (yyy , b)

f (xxx ,yyy)

Subjectfc(f (a, b), f (a, a))

Patternfc(f (yyy , b), f (xxx ,yyy))

{yyy 7! a}

{x

x

x 7!a,y

y

y 7!b}

{xxx 7! a,yyy 7! a}

Algorithms - Many-to-One

Bipartite Graph

Manuel Krebber | June 30, 2017 23/31

Page 69: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

f (a, b)

f (a, a)

f (a,xxx)

f (xxx ,yyy)

Subjectfc(f (a, b), f (a, a))

Patternfc(f (a,xxx), f (xxx ,yyy))

{xxx 7! b}

{x

x

x 7!a,y

y

y 7!b}

{xxx 7! a,yyy 7! a}

Algorithms - Many-to-One

Bipartite Graph

Manuel Krebber | June 30, 2017 23/31

Page 70: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

f (a, b)

f (a, a)

f (a,xxx)

f (xxx ,yyy)

Subjectfc(f (a, b), f (a, a))

Patternfc(f (a,xxx), f (xxx ,yyy))

{xxx 7! b}

{x

x

x 7!a,y

y

y 7!b}

{xxx 7! a,yyy 7! a}

Algorithms - Many-to-One

Bipartite Graph

Manuel Krebber | June 30, 2017 23/31

Page 71: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Experiments

Table of Contents

Manuel Krebber | June 30, 2017 24/31

Page 72: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Python implementation

I Open source on Github:https://github.com/hpac/matchpy

Experiments

MatchPy

Manuel Krebber | June 30, 2017 25/31

Page 73: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Python implementationI Open source on Github:

https://github.com/hpac/matchpy

Experiments

MatchPy

Manuel Krebber | June 30, 2017 25/31

Page 74: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I ⇠ 200 patterns for BLAS/LAPACK kernels, e.g. ↵↵↵⇥A

A

A

T⇥B

B

B

I Supported operations:

Operation Symbol Arity Properties

Multiplication ⇥ variadic associativeAddition + variadic associative, commutativeTransposition T unaryInversion �1 unaryInverse Transposition �T unary

Experiments

Linear Algebra Example

Manuel Krebber | June 30, 2017 26/31

Page 75: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I ⇠ 200 patterns for BLAS/LAPACK kernels, e.g. ↵↵↵⇥A

A

A

T⇥B

B

B

I Supported operations:

Operation Symbol Arity Properties

Multiplication ⇥ variadic associativeAddition + variadic associative, commutativeTransposition T unaryInversion �1 unaryInverse Transposition �T unary

Experiments

Linear Algebra Example

Manuel Krebber | June 30, 2017 26/31

Page 76: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

50 100 150

0

20

40

60

Number of Patterns

SetupTim

e[m

s]

Setup Time

50 100 150

0

5

10

Number of Patterns

Match

Tim

e[m

s]

Match Time

matchMTOM

Experiments

Match Times

Manuel Krebber | June 30, 2017 27/31

Page 77: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

50 100 1500

5

10

15

20

Number of Patterns

Speedup

Speedup

50 100 1500

2

4

6

8

Number of PatternsNumber

ofSubjects

Break Even

Experiments

Speedup

Manuel Krebber | June 30, 2017 28/31

Page 78: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Conclusions

Table of Contents

Manuel Krebber | June 30, 2017 29/31

Page 79: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Generalized discrimination nets

I Many-to-one matching

Isequence variables

Iseparate associativity/commutativity

I Open source implementation

Conclusions

Contributions

Manuel Krebber | June 30, 2017 30/31

Page 80: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Generalized discrimination netsI Many-to-one matching

Isequence variables

Iseparate associativity/commutativity

I Open source implementation

Conclusions

Contributions

Manuel Krebber | June 30, 2017 30/31

Page 81: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Generalized discrimination netsI Many-to-one matching

Isequence variables

Iseparate associativity/commutativity

I Open source implementation

Conclusions

Contributions

Manuel Krebber | June 30, 2017 30/31

Page 82: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Generalized discrimination netsI Many-to-one matching

Isequence variables

Iseparate associativity/commutativity

I Open source implementation

Conclusions

Contributions

Manuel Krebber | June 30, 2017 30/31

Page 83: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

I Generalized discrimination netsI Many-to-one matching

Isequence variables

Iseparate associativity/commutativity

I Open source implementation

Conclusions

Contributions

Manuel Krebber | June 30, 2017 30/31

Page 84: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Project is on GitHub:https://github.com/hpac/matchpy

Conclusions

Repository

Manuel Krebber | June 30, 2017 31/31

Page 85: Non-linear Associative-Commutative Many-to-One Pattern ...hpac.rwth-aachen.de/~pauldj/talks/Manuel-MS-Thesis.pdfNon-linear Associative-Commutative Many-to-One Pattern Matching with

Thank you for your attention!