Bounded Second-Order Unification
is NP-Complete
Jordi Levy
Manfred Schmidt-Schauß
Mateu Villaret
IIIA
CSIC
Barcelona, Spain
D. Informatics and Mathematics
University Frankfurt
Frankfurt, Germany
IMA
Universitat de Girona
Girona, Spain
RTA’06 (FLoC’06)
Seattle, USA
Bounded Second-Order Unificationis NP-Complete – p. 1
Second-Order Unification (SOU)
Unification in λ→ with second-order typed terms
?
f(X(a), X(a)) =
X(f(a, a))
Bounded Second-Order Unificationis NP-Complete – p. 2
Second-Order Unification (SOU)
Unification in λ→ with second-order typed terms
?
f(X(a), X(a)) =
X(f(a, a))
Unifier:
σ = [X 7→ λx . x]
Common instance:
σ(f(X(a), X(a))) = f(a, a) = σ(X(f(a, a)))
Bounded Second-Order Unificationis NP-Complete – p. 2
Second-Order Unification (SOU)
Unification in λ→ with second-order typed terms
?
f(X(a), X(a)) =
X(f(a, a))
Unifier:
σ = [X 7→ λx . f(x, x)]
Common instance:
σ(f(X(a), X(a))) = f(f(a,a), f(a,a)) =
= f(f(a, a),f(a, a)) = σ(X(f(a, a)))
Bounded Second-Order Unificationis NP-Complete – p. 2
Second-Order Unification (SOU)
Unification in λ→ with second-order typed terms
?
f(X(a), X(a)) =
X(f(a, a))
unifiers:
X →
7
λx . x
X →
7
λx . f(x, x)
X 7→ λx . f(f(x, x), f(x, x))
...
Bounded Second-Order Unificationis NP-Complete – p. 2
Bounded Second-Order Unification (BSOU)
(Semantic) variant of SOU where the number of occurrences of
bound variables in instances of substitutions are bounded
?
X(f(a, a))
f(X(a), X(a)) =
Bound = 3
Bounded Second-Order Unificationis NP-Complete – p. 3
Bounded Second-Order Unification (BSOU)
(Semantic) variant of SOU where the number of occurrences of
bound variables in instances of substitutions are bounded
?
X(f(a, a))
f(X(a), X(a)) =
Bound = 3
Unifiers:
1 occurrence
X
7
→
λx . x
X
7
→
λx . f(x, x)
2 occurrences
X
7
→
λx . f(f(x, x), f(x, x)) 4 occurrences
...
Bounded Second-Order Unificationis NP-Complete – p. 3
Bounded Second-Order Unification (BSOU)
(Semantic) variant of SOU where the number of occurrences of
bound variables in instances of substitutions are bounded
?
X(f(a, a))
f(X(a), X(a)) =
Bound = 3
Unifiers:
1 occurrence
X
7
→
λx . x
X
7
→
λx . f(x, x)
2 occurrences
X
7
→
λx . f(f(x, x), f(x, x)) 4 occurrences
...
Bounded Second-Order Unificationis NP-Complete – p. 3
Simplifications
BSOU is NP-reducible to the case where:
• all free variables (unknowns) are unary
• the bound on the number of occurrences of bound variables
is 1 (bound given in unary)
Bounded Second-Order Unificationis NP-Complete – p. 4
Main ideas
The NP algorithm:
Given a BSOU equation E,
1. Guess a solution σ represented by a polynomially sized
Singleton Tree Grammar (STG)
2. Check if σ solves E using a result for STG that allows us to
compare words defined by the STG in polynomial time
Bounded Second-Order Unificationis NP-Complete – p. 5
Main ideas
The NP algorithm:
Given a BSOU equation E,
1. Guess a solution σ represented by a polynomially sized
Singleton Tree Grammar (STG)
2. Check if σ solves E using a result for STG that allows us to
compare words defined by the STG in polynomial time
?
For any solvable BSOU equation s =
t, there exists a solution σ representable by an STG of size O((|s| + |t|)k ).
Bounded Second-Order Unificationis NP-Complete – p. 5
Main ideas
The NP algorithm:
Given a BSOU equation E,
1. Guess a solution σ represented by a polynomially sized
Singleton Tree Grammar (STG)
2. Check if σ solves E using a result for STG that allows us to
compare words defined by the STG in polynomial time
?
For any solvable BSOU equation s =
t, there exists a solution σ representable by an STG of size O((|s| + |t|)k ).
Key: Some unifiers have the form X 7→ t[un [v]] where n is
exp-bounded by the exponent of periodicity lemma
We can represent un [ ] in poly-space using STG
Bounded Second-Order Unificationis NP-Complete – p. 5
More ideas
Given E, how to find a unifier σ ?
E
Bounded Second-Order Unificationis NP-Complete – p. 6
More ideas
Given E, how to find a unifier σ ?
E
ρ1
Find ρ1 s.t. σ = σ 0 ◦ ρ1
Bounded Second-Order Unificationis NP-Complete – p. 6
More ideas
Given E, how to find a unifier σ ?
E
ρ1 (E)
ρ1
Find ρ1 s.t. σ = σ 0 ◦ ρ1
Given ρ1 (E) try to find the unifier σ 0
Bounded Second-Order Unificationis NP-Complete – p. 6
More ideas
Given E, how to find a unifier σ ?
E
ρ1 (E)
ρ1
ρ2
ρ2 (ρ1 (E)). . .
Find ρ1 s.t. σ = σ 0 ◦ ρ1
Given ρ1 (E) try to find the unifier σ 0
Find ρ2 s.t. σ 0 = σ 00 ◦ ρ2 , i.e. σ = σ 00 ◦ ρ2 ◦ ρ1
...
Bounded Second-Order Unificationis NP-Complete – p. 6
More ideas
Given E, how to find a unifier σ ?
z
E
ρ1
finite
}|
ρ1 (E)
{
ρ2 (ρ1 (E)). . .
ρ2
Find ρ1 s.t. σ = σ 0 ◦ ρ1
Given ρ1 (E) try to find the unifier σ 0
Find ρ2 s.t. σ 0 = σ 00 ◦ ρ2 , i.e. σ = σ 00 ◦ ρ2 ◦ ρ1
...
σ = ρ n ◦ · · · ◦ ρ 2 ◦ ρ1
Bounded Second-Order Unificationis NP-Complete – p. 6
More ideas
Given E and σ, how to find a poly-representation of σ ?
Polynomial in
z
E
ρ1
polynomially terminating
}|
{
≥ ρ1 (E) ≥ ρ2 (ρ1 (E)). . .
ρ2
Find ρ1 s.t. σ = σ 0 ◦ ρ1
Given ρ1 (E) try to find the unifier σ 0
Find ρ2 s.t. σ 0 = σ 00 ◦ ρ2 , i.e. σ = σ 00 ◦ ρ2 ◦ ρ1
...
σ = ρ n ◦ · · · ◦ ρ 2 ◦ ρ1
σ polynomial sized
Bounded Second-Order Unificationis NP-Complete – p. 6
What do we need?
E
ρ1 (E)
ρ1
ρ2
ρ2 (ρ1 (E)). . .
Bounded Second-Order Unificationis NP-Complete – p. 7
What do we need?
z
E
ρ1
polynomially terminating
}|
{
ρ1 (E)
ρ2 (ρ1 (E)). . .
ρ2
1 Poly-terminating ordering on equations µ(E) > µ(E 0 )
Bounded Second-Order Unificationis NP-Complete – p. 7
What do we need?
Polynomial in
z
E
ρ1
polynomially terminating
}|
{
≥ ρ1 (E) ≥ ρ2 (ρ1 (E)). . .
ρ2
1 Poly-terminating ordering on equations µ(E) > µ(E 0 )
2 Getting partial instances of variables:
Given E and σ, get a ρ s.t.
• σ = σ 0 ◦ ρ, for some σ 0
• µ(E) > µ(ρ(E))
• ρ is built up from poly-many “pieces” of E
Bounded Second-Order Unificationis NP-Complete – p. 7
What do we need?
Polynomial in
z
E
ρ1
polynomially terminating
}|
{
≥ ρ1 (E) ≥ ρ2 (ρ1 (E)). . .
ρ2
1 Poly-terminating ordering on equations µ(E) > µ(E 0 )
2 Getting partial instances of variables:
Given E and σ, get a ρ s.t.
• σ = σ 0 ◦ ρ, for some σ 0
• µ(E) > µ(ρ(E))
• ρ is built up from poly-many “pieces” of E
3 A compact representation for E as [X1 7→ c1 ] · · · [Xn 7→ cn ] E 0
The “pieces” of E have a poly-size representation on the
representation of E
Bounded Second-Order Unificationis NP-Complete – p. 7
1 Poly-terminating ordering on the equations
?
f(f(X(t1 ), Y(t2 )), X(t3 )) =
f(Z(t4 ), Y(t5 ))
f
f
X
Y
t1
t2
f
X
Z
Y
t3
t4
t5
Bounded Second-Order Unificationis NP-Complete – p. 8
1 Poly-terminating ordering on the equations
?
?
Sub-equations: {f(X(t1 ), Y(t2 )) =
Z(t4 ), X(t3 ) =
Y(t5 )}
f
f
X
Y
t1
t2
f
X
Z
Y
t3
t4
t5
Bounded Second-Order Unificationis NP-Complete – p. 8
1 Poly-terminating ordering on the equations
?
?
Sub-equations: {f(X(t1 ), Y(t2 )) =
Z(t4 ), X(t3 ) =
Y(t5 )}
f
f
X
Y
t1
t2
f
X
Z
Y
t3
t4
t5
Equivalence: X ≈ Y
Dominance: Z Â X and Z Â Y
Â+ may be not-irreflexive ⇒ cycles!
Bounded Second-Order Unificationis NP-Complete – p. 8
1 Poly-terminating ordering on the equations
Definition µ(E) = hµ1 (E), µ2 (E), µ3 (E)i is a lexicographic
ordering
• µ1 (E) = |V ar(E)|
• µ2 (E) =
(
length of the shortest cycle in E
∞, if there are no cycles
• µ3 (E) =
(
0, if E contain cycles
|V ar(E)| − |V ar(E)/ ≈E | + 2|ÂE|
Lemma Any decreasing sequence of equations
µ(E0 ) > µ(E1 ) > · · · terminates in at most 2 |V ar(E0 )|3 steps.
Bounded Second-Order Unificationis NP-Complete – p. 8
2 Rules for getting partial instances
Lemma Given E and a size-minimal σ,
get ρ = [X1 7→ c1 ] ◦ . . . ◦ [Xn 7→ cn ] s.t.:
• σ = σ0 ◦ ρ
• σ 0 is a size-minimal solution of ρ(E)
• n = O(|V ar(E)|), and
ci ’s can be built up from O(|V ar(E)|)-many “pieces” of E
• µ(E) > µ(ρ(E))
Bounded Second-Order Unificationis NP-Complete – p. 9
There are Â+ cycles
(
?
f(X2 (s2 ), u2 )
X1 (t1 ) =
?
X2 (t2 ) =
g(X1 (s1 ), u1 )
f
X1
X2
g
• X1 is instantiated “inside” the cycle
ρ=
"
7 f(g(•, u1 ), u2 )e1 (f(•, u2 )),
X1 →
X2 →
7 g(f(•, u2 ), u1 )e1 (g((X02 (•), u1 )))
#
Decreases the number of variables
Bounded Second-Order Unificationis NP-Complete – p. 10
There are Â+ cycles
(
?
f(X2 (s2 ), u2 )
X1 (t1 ) =
?
X2 (t2 ) =
g(X1 (s1 ), u1 )
f
X1
X2
g
• X1 is instantiated “inside” the cycle
ρ=
"
7 f(g(•, u1 ), u2 )e1 (f(•, u2 )),
X1 →
X2 →
7 g(f(•, u2 ), u1 )e1 (g((X02 (•), u1 )))
#
Decreases the number of variables
• X1 “lefts” the cycle
ρ=
"
7 f(g(•, u1 ), u2 )e1 (f(X02 (s2 ), X01 (•)))
X1 →
X2 →
7 g(f(•, u2 ), u1 )e1 (g((X02 (•), u1 )))
with ei < eop
We get a smaller cycle
#
Bounded Second-Order Unificationis NP-Complete – p. 10
All equations are flex-flex
?
X(t) =
Y(u)
Since σ is size-minimal:
X 7→ a
Y 7→ a
ρ = or
,
or
X 7→ •
Y 7→ •
The number of free variables decreases
Bounded Second-Order Unificationis NP-Complete – p. 11
All equations are flex-flex
?
X(t) =
Y(u)
Since σ is size-minimal:
X 7→ a
Y 7→ a
ρ = or
,
or
X 7→ •
Y 7→ •
The number of free variables decreases
⇒ This is the basic difference w.r.t. Context Unification
(whose decidability is still open)
Bounded Second-Order Unificationis NP-Complete – p. 11
There are “small” instances
?
X(t) =
c[u]
And σ instantiates X by a small context c0 [ ] included in c[ ]:
ρ = [X 7→ c0 (•)]
The number of free variables decreases
Bounded Second-Order Unificationis NP-Complete – p. 12
Equivalence classes “merge” or “split”
?
• X(t) =
c[u]
Some equivalent variables may “merge”
ρ = [X 7→ c(X0 (•))]
?
The resulting new equation X0 (t1 ) =
Y(t2 ) merges
equivalence classes, but reduces dominance
Bounded Second-Order Unificationis NP-Complete – p. 13
Equivalence classes “merge” or “split”
?
• X(t) =
c[u]
Some equivalent variables may “merge”
ρ = [X 7→ c(X0 (•))]
?
The resulting new equation X0 (t1 ) =
Y(t2 ) merges
equivalence classes, but reduces dominance
?
?
?
• {X(t1 ) =
Y(u1 ), X(t2 ) =
u2 , Y(t3 ) =
u3 }
Some equivalent variables “split”,
ρ = [X 7→ c[f(X0 (•), t01 )], Y 7→ c[f(t02 , Y0 (•))]]
The number equivalence classes decreases
Bounded Second-Order Unificationis NP-Complete – p. 13
3 Singleton Tree Grammars (STG)
Non-recursive, Non-ambiguous Tree Grammars with rules like:
• A ::= f (A1 , . . . , An )
• A1 ::= C[A2 ]
• C1 ::= C2 C3
• C ::= f (A1 , . . . , Ai−1 , [•], Ai+1 , . . . , An )
Where
• A’s are non-terminals for Terms and
• C’s are non-terminals for Contexts
Bounded Second-Order Unificationis NP-Complete – p. 14
3 Singleton Tree Grammars (STG)
Non-recursive, Non-ambiguous Tree Grammars with rules like:
• A ::= f (A1 , . . . , An )
• A1 ::= C[A2 ]
• C1 ::= C2 C3
• C ::= f (A1 , . . . , Ai−1 , [•], Ai+1 , . . . , An )
Where
• A’s are non-terminals for Terms and
• C’s are non-terminals for Contexts
Theorem We can test whether A and B generates the same
tree (term) in polynomial time on |G|
[Plandowski’94, Busatti et al.’05, Schmidt-Schauß’05]
Bounded Second-Order Unificationis NP-Complete – p. 14
3 Singleton Tree Grammars (STG)
Given an STG G we can extend it G ⊆ G0 by:
• prefix, suffix: given C obtain C 0 ≺ C
• context concatenation: given C1 , · · · , Cn obtain C1 . . . Cn
• exponentiation: given C obtain C e
• sub-context: given A obtain C 0 such that A = C 0 (t)
increasing the size and depth polynomially in log e and log n
Bounded Second-Order Unificationis NP-Complete – p. 15
3 Compact representation
Keep solutions compacted as a composition of substitutions,
e.g. FOU:
σ = [X1 7→ a] ◦ . . . ◦ [Xn−1 7→ f(Xn−2 , Xn−2 )] ◦ [Xn 7→ f(Xn−1 , Xn−1 )]
Bounded Second-Order Unificationis NP-Complete – p. 16
3 Compact representation
Compute prefixes t0 of the compact representation
t = [X1 7→ v1 ] ◦ . . . ◦ [Xn 7→ vn ]u
where the grammar generates Ai →∗ vi and A →∗ u
• Replace terminal Xi by non-terminal Ai
• Compute the prefix
depth(t) = depth(A) +
Pn
i=1
depth(Ai )
Bounded Second-Order Unificationis NP-Complete – p. 16
3 Compact representation
Compute prefixes t0 of the compact representation
t = [X1 7→ v1 ] ◦ . . . ◦ [Xn 7→ vn ]u
where the grammar generates Ai →∗ vi and A →∗ u
• Replace terminal Xi by non-terminal Ai
• Compute the prefix
depth(t) = depth(A) +
Pn
i=1
depth(Ai )
Built prefixes vi0 of vi and u0 of u and enlarge G to generate
t0 = [X1 7→ v1 ] ◦ . . . ◦ [Xn 7→ vn ] u0 vn0 . . . v10
depth(t) = log n + max{depth(A), depth(A1 ), . . . , depth(An )}
Bounded Second-Order Unificationis NP-Complete – p. 16
Conclusions
?
Theorem If σ is a size-minimal solution of E = (s =
t) then we
can represent σ(t) in a compact manner with a grammar of size
O(|E|18 ) and depth O(|E|4 ).
Corollary Bounded Second-Order Unification is NP-complete
Bounded Second-Order Unificationis NP-Complete – p. 17
© Copyright 2026 Paperzz