• No results found

An optimized algorithm for epsilon-free automata

4.2 Tree automata matching optimization

4.2.2 An optimized algorithm for epsilon-free automata

Given a TRS R and a tree automaton A, for computing all regular language substitutions, it is necessary to run the previous algorithm for ever rule l → r ∈ R and every state q ∈ Q. Now, we propose a more efficient algorithm for epsilon-free tree automata dealing with all rules of R at the same time. Note that the case of automata with epsilon transition will be treated as an extension of this algorithm in Section 4.2.3. The idea behind the optimized matching algorithm is to represent the set of terms to be matched, i.e. the set of left- hand sides of R by a tree automaton ARand use the tree automata intersection algorithm

between ARand A, the automaton to complete, to compute the set of substitutions. First,

for every term t ∈ T (F , X ), we define a tree automaton whose language is exactly {t} using abstraction and normalization functions defined in Section 3.1.

Definition 121 (Term automaton) Let S be a finite set S ⊆ T (F , X ), and consider Sub the set of all the subterms ofS, QS a set of states, and α : Sub → Qt an injective

abstraction function. The term automaton forS is defined by Aα,S = hF , Qt, QSf, ∆Si

Proposition 122 If S ⊆ T (F , X ), α is an injective abstraction and Aα,Sits term automa-

ton then

L(Aα,S) = S

PROOF. The proof is done by an inductive reasoning over the depth of terms t of S. 2 Let us now define the substitution automaton A∩= Aα,S∩ A which recognizes a set

of Q-instances which corresponds to the substitutions solutions of the matching of terms of S on A.

Definition 123 (Substitution automaton) Let A = hF , Q, Qf, ∆i be an automaton, α

an injective abstraction function andAα,S = hF , QS, QSt, ∆ti the automaton of S ⊆

T (F , X ). We denote by A∩= hF ∪ (X × Q), Q∩, Qf,∩, ∆∩i the substitution automaton

ofS in A by: • Q∩= QS× Q • Qf,∩= QSf× Q • ∆∩= {f ((q1, q10), . . . , (qn, qn0)) → (qn+1, qn+10 ) | qi∈ Qt, qi0 ∈ Q, f (q1, . . . , qn) → qn+1∈ ∆S, f (q10, . . . , q0n) → q0n+1∈ ∆} ∪{(x, q) → (qx, q) | x → qx∈ ∆S, q ∈ Q}  Definition 124 (Q-instance) Let F be an alphabet, Q a set of states, X a set of variables, we inductively define the setQ-instances:

• every pair (x, q) for x ∈ X and q ∈ Q is a Q-instance

• ∀f ∈ F ar(f ) = n, if i1, . . . , inare someQ-instances then f (i1, . . . , in) is also a

Q-instance.

 Definition 125 (Recognized Q-instance) Let A∩= hF , Q∩, Q∩f, ∆∩i be a substitution

automaton. A∩ defines the set of all Q-instances i such that there exists a final state

q ∈ Q∩f verifyingi →∗∆∩ q 

Definition 126 (Regular language substitution defined by a Q-instance) A Q-instance s inductively defines a set of regular language substitutions

• if s = (x, q), then s defines the regular language substitution {x 7→ q} • if s = (a, a) then s defines the empty substitution

• if s = f (s1, . . . , sn), then if {σi,j}i∈Ijare the substitutions associated tosj,s define

the set of substitutionsτ = σ1,i1◦ . . . ◦ σn,in.

Example 127 Let AR be the automaton for termf (x, g(y)). Let A be the automaton

recognizing the language{f (a, g∗(b)), f (g(b), a). Let A

∩be the intersection automaton

betweenAR and A. The automaton A∩ recognizes, in final state(qf, q4), a unique Q-

instancef ((qx, q1), g((qy, q3)) which defines the regular language substitution σ = {x 7→

q1, y 7→ q3} and means that f (x, g(y))σ →A∗q4.

ARwith Q = {qx, qy, qf, qg} Qf = {qf } ∆ = y → qy g(qy) → qg x → qx f (qx, qg) → qf A with Q = {q1, q2, q3, q4} Qf= {q4} ∆ = a → q1 b → q2 g(q2) → q3 g(q3) → q3 f (q1, q3) → q4 f (q3, q1) → q4 A∩with Q = {qx, qy, qf, qg} × {q1, q2, q3, q4} Qf= {qf } × {q1, q2, q3, q4}

∆ = (we omit useless transitions) (y, q2) → (qy, q2) (y, q3) → (qy, q3) (x, q1) → (qx, q1) (x, q3) → (qx, q3) g((qy, q2)) → (qg, q2) g((qy, q3)) → (qg, q3) f ((qx, q1), (qg, q3)) → (qf, q4) f ((qx, q3), (qg, q1)) → (qf, q4)

Theorem 128 Let A = hF , Q, Qf, ∆i be an automaton, R a TRS, ARthe automaton of

all left-hand sides ofR and A∩the substitution automaton. Furthermore, we assume that

A respects the left-coherence condition induced by R. For all rewrite rule l → r ∈ R, for all stateq ∈ A, the set of regular language substitutions defined by the state (topα(l), q)

ofA∩is exactly the set of regular language substitutions{σi}i∈Isuch thatlσi→A∗q.

PROOF.

1. First assume that {σi}i∈Iis a set of regular language substitutions defined by a term

s such that s →∗ (α(l), q) in A∩. For all σi, we prove that lσi →∗ q by induction

on the length of the derivations s →n (α(l), q):

• If s →1

A∩ (α(l), q) then depth(s) = 1 and

– either s = (a, a) where a is a constant, we have (a, a) →A∩ (α(a), q)

then l = a and l →∆q, s defines the substitution of empty domain.

– or s = (x, a) we have also (x, a) →A∩ (α(a), q) then l = x, and s defines

σi= {x 7→ q}, we have lσ = a and a → q

• Assume now that for all set {σi}i∈Idefined by a term s of A∩verifying s →k

(α(l), q) for every k ≤ n then lσi→∗q in A.

Consider now {τi}i∈Idefined by a term s such that s →n+1(α(l), q). Clearly

s is a term of the form f (s1, . . . , sn), where sj = (lj, qj) define {τi}i∈Isuch

that τi= σ1,i1◦. . .◦σn,inwhere σj,ijrange over the set of substitutions defined

by the term sj.

f (s1, . . . , sn) →A∩ (α(l), q) then l is of the form f (l1, . . . , ln) and there ex-

ist some states qj verifying sj →kA∩ (α(lj), qj). We use the inductive hy-

there exists a combination of σi,jverifying lτi → f (l1σ1,i1, . . . , lnσn,in) →

f (q1, . . . , qn) →∗∆q

2. We use now a structural induction over l to prove that if lσ →∗q then σ correspond to a term s in L(A∩):

(a) if l is a constant a

On the one hand a →At α(a) and on the other hand there exists a state q in Q

such that a →A q then (a, a) →A∩ (α(a), q) and (α(a), q) is a final state of

A∩, A∩recognized (a, a), that defines the empty substitution.

(b) If l is a variable x

x →At α(x) and all the terms recognized by A may be an instance of x, we

have {(x, q) → (α(x), q)|q ∈ Q} ⊂ ∆∩and Qf,∩ = {α(x)} × Q we have

defined all the regular language substitution {{x 7→ q}|q ∈ Q}. (c) If l is of the form f (l1, . . . , ln)

A recognized lσ then there exist some states q, q1, . . . qnin A such that f (q1, . . . , qn) →∗A

q, liσ →Aqi. Assume that s = f ((α(l1), q1), . . . , (α(ln), qn)), there exist qi

in A and qirecognized tiσ then thanks to induction hypothesis for each lithere

exists si, Q-instance defining σi. If σ1◦ . . . σn is not defined then lσ is not

recognized by A, contradiction ; else σ1◦ . . . σn exists and A∩ recognized

s = f (s1, . . . , sn) and s define σ.

2