The partial derivative automaton accepts L(r) and has at most k + 1 states full
This proof shows: correctness, termination.
Throughout, expressions are compared as terms (trees), ν and S⊙t are as on the main page, and ∂ax is the set computed by lines L9 to L13. For a set S of expressions write L(S)=⋃z∈SL(z), and let #x be the number of symbol occurrences in x.
Lemma 1. ν(x) is true if and only if ε∈L(x).
Proof. By induction on x: ε∈/L(∅) and ε∈/L(b); ε∈L(ε) and ε∈L(s∗); ε∈L(s)∪L(t) iff it is in one of them; and ε∈L(s)L(t) iff it is in both, since uv=ε forces u=v=ε. □
Lemma 2. L(S⊙t)=L(S)L(t).
Proof. If t=∅, both sides are empty. If t=ε, both sides are L(S). Otherwise each z∈S contributes L(zt)=L(z)L(t), or L(t)=L(ε)L(t) when z=ε, and the union over z is L(S)L(t). □
Lemma 3. For every expression x and symbol a, L(∂ax)=a−1L(x), where a−1L={v:av∈L}.
Proof. By induction on x. For ∅ and ε, no word of L(x) starts with a, and line L9 returns ∅. For a symbol b, a−1{b} is {ε} if b=a and empty otherwise, as at line L10. For s∣t, a−1 distributes over union (line L11). For st: av∈L(s)L(t) iff either av=(au)v′ with au∈L(s) and v′∈L(t), so v∈(a−1L(s))L(t), or ε∈L(s) and av∈L(t). By the induction hypothesis, Lemma 1 and Lemma 2, this is L(∂as⊙t)∪L(∂at) when ν(s) holds and L(∂as⊙t) otherwise, as at line L12. For s∗: av∈L(s∗) iff av=u1u2⋯uj with all ui∈L(s); dropping the empty factors, the first factor is au with au∈L(s) and the rest is in L(s∗). So a−1L(s∗)=(a−1L(s))L(s∗), which is L(∂as⊙s∗) by Lemma 2, as at line L13. □
For a word u, ∂εx={x} and ∂uax=⋃y∈∂ux∂ay. By induction on u and Lemma 3, L(∂ux)=u−1L(x), since (ua)−1L=a−1(u−1L) and a−1 distributes over union.
Definition. π(∅)=π(ε)=∅, π(b)={ε}, π(s∣t)=π(s)∪π(t), π(st)=(π(s)⊙t)∪π(t) and π(s∗)=π(s)⊙s∗.
Lemma 4. ∣π(x)∣≤#x.
Proof. By induction, using ∣S⊙t∣≤∣S∣, which holds because ⊙ maps each member of S to at most one term: ∣π(st)∣≤∣π(s)∣+∣π(t)∣≤#s+#t, and the other cases are similar. □
Lemma 5. For every expression x and symbol a: (i) ∂ax⊆π(x), and (ii) ∂ay⊆π(x) for every y∈π(x).
Proof. By induction on x. Note that S⊆S′ implies S⊙t⊆S′⊙t.
- x=∅ or ε: ∂ax=π(x)=∅.
- x=b: ∂ab⊆{ε}=π(b), and the only y∈π(b) is ε, with ∂aε=∅.
- x=s∣t: both parts follow from the induction hypothesis for s and t, since π(x) is the union of π(s) and π(t).
- x=st: (i) ∂ax is ∂as⊙t, possibly with ∂at, which are subsets of π(s)⊙t and π(t) by (i) for s and t. (ii) If y∈π(t), use (ii) for t. Otherwise y∈π(s)⊙t, so t=∅ and y comes from some z∈π(s). If t=ε, then y=z and ∂ay⊆π(s)=π(s)⊙t by (ii) for s. If z=ε, then y=t and ∂at⊆π(t) by (i) for t. Otherwise y is the term zt, and ∂ay is ∂az⊙t, possibly with ∂at; by (ii) for s, ∂az⊆π(s), and by (i) for t, ∂at⊆π(t).
- x=s∗: (i) ∂ax=∂as⊙s∗⊆π(s)⊙s∗. (ii) y comes from some z∈π(s), and s∗ is neither ∅ nor ε. If z=ε, then y=s∗ and (i) applies. Otherwise y is the term zs∗, and ∂ay is ∂az⊙s∗, possibly with ∂a(s∗)=∂as⊙s∗; both are subsets of π(s)⊙s∗ by (ii) and (i) for s. □
Lemma 6. Every set Q built by lines L1 to L7 satisfies Q⊆{r}∪π(r), so ∣Q∣≤k+1.
Proof. Line L1 starts with {r}. A term y is added at line L7 only when y∈∂ax for some x∈Q, and by Lemma 5, ∂ar⊆π(r) and ∂ax⊆π(r) for x∈π(r). The size bound is Lemma 4. □
Termination. Each pass of the loop at lines L2 to L7 pops one term, and a term is pushed only when it is added to Q, at most once. By Lemma 6, at most k+1 terms are ever pushed, so the loop runs at most k+1 times. Each call PARTIAL(x,a) ends: it processes the finitely many nodes below x, each once per symbol because of the memo. The loop at lines L16 to L17 runs n times.
Lemma 7. After line L8, for every x∈Q and symbol a, the moves of T from x on a lead exactly to ∂ax; for a symbol c that does not occur in r, ∂cx=∅.
Proof. Every x∈Q is pushed once and popped once, and when it is popped, lines L4 to L6 add the moves (x,a,y) for all y∈∂ax and all symbols a of r. A term x∈Q is r or a member of π(r), whose symbols all occur in r; by Lemma 3, L(∂cx)=c−1L(x)=∅, and by line L10, a term without the symbol c has ∂cx=∅. □
Theorem. PD-MATCH(r,w) returns true if and only if w∈L(r).
Proof. By Lemma 7 and induction on i, the set S after i rounds of line L17 is ∂a1⋯air. If line L17 returns false, S is empty, so L(∂a1⋯air)=(a1⋯ai)−1L(r) is empty and no word that starts with a1⋯ai is in L(r), in particular not w. Otherwise, line L18 returns whether some x∈∂wr has ν(x), which by Lemma 1 holds iff ε∈L(∂wr)=w−1L(r), that is, iff w∈L(r). ■