Proofs: Lazy DFA construction
These are the human proofs of Lazy DFA construction, at full length.
The lazy DFA accepts exactly the words that the NFA accepts full
This proof shows: correctness, termination.
Let have the finite set of states , start state and accepting states . For and a symbol , is the set of targets of the moves on that leave , and is the set of states reachable from by zero or more -moves. Define and .
Lemma 1. For every word , is the set of states such that some path from to reads , where a path may use -moves anywhere.
Proof. By induction on . For , a path that reads uses only -moves, so its end is in , and every state of the closure is the end of such a path. For , a path that reads splits at its last move on a symbol, which reads : before it, a path that reads , ending in some by induction; then a move on from to some , so ; then only -moves, so the end is in the closure. Conversely, each state of is the end of a path built from these three parts.
So accepts if and only if .
Lemma 2 (stored moves). At all times, every DFA state object satisfies: , and for every symbol with defined, .
Proof. Objects are made only at line L12, with the right accepting bit and an empty table. The fields set and accepting of an object never change afterwards. A table entry is written only at line L9, as , where was returned by STATE at line L8 with from line L6, and STATE returns an object whose set is : either the one already stored under the key , or a new one with set . Clearing the map at line L7 removes keys but changes no object. So the property holds after every line.
Lemma 3 (loop invariant). After rounds of the loop at lines L3 to L10, .
Proof. Before the loop, line L2 sets to an object with set . In round , with and : if is defined, line L4 reads , and by Lemma 2 . Otherwise lines L6 to L8 make with the same set. Either way, after line L10, .
Termination. The loop runs once for each of the symbols of . In each round, MOVE scans the finitely many moves that leave , and CLOSURE is a graph search that adds each state of at most once. Lines L7 to L9, L12 and L13 do finitely many map operations. So the algorithm stops.
Theorem. LAZY-DFA-MATCH returns true if and only if accepts , for every capacity .
Proof. By Lemma 3, after the loop . By Lemma 2, line L11 returns , which by Lemma 1 holds exactly when accepts . The capacity enters only through line L7, which by Lemma 2 never makes a stored set or move wrong.
Search. With added to the result of MOVE before the closure, the same argument with shows, by the induction of Lemma 1, that is the set of states reachable from by a path that reads some suffix of . So the state after is accepting if and only if some substring of that ends at position is accepted.