Proofs: Hoffmann-O'Donnell tree pattern matching
These are the human proofs of Hoffmann-O'Donnell tree pattern matching, at full length.
The bottom-up matching automaton computes exactly the match sets full
This proof shows: correctness.
We use the pseudocode of the page and the notation of the term rewriting pages: a signature , variables , terms , positions as sequences of positive integers, for the subterm at , and substitutions . The patterns are . For a term , is with each variable occurrence replaced by a new variable, all distinct. A pattern matches if for some ; variables of the subject are treated as constants.
The pattern forest. Write for one wildcard symbol. For a term , let be with every variable replaced by . Line L1 builds , the set of all for subterms of the patterns; it contains when some pattern has a variable, and the code always adds (id 0). Say that a member of fits a term if , or , with the same and , and each fits .
Lemma 1. For a term and a subject term , fits if and only if matches .
Proof. Induction on . If is a variable, fits every , and is a variable, which matches every . If , then up to renaming, with the variables of the arguments pairwise disjoint. It matches iff and each matches , because the matchers of the arguments have disjoint domains and combine into one substitution. By the induction hypothesis this holds iff each fits , that is, iff fits .
For a subject term , its match set is .
Lemma 2 (one step). For , . For a subject variable or a symbol that no member of has at the root, .
Proof. This is the definition of "fits", restricted to the members of . A member other than fits only if its root is the root symbol of with the same arity, and a subject variable is not the root of any member.
The function is what line L5 computes.
Lemma 3 (preprocessing). The loop of lines L3 to L6 stops. When it stops, the set of states contains and is closed under every : for every symbol at the root of a member of and all , the table entry is defined and equals .
Proof. Every state is a subset of , and has no repeated sets, so . Each round either adds an entry to a table or makes no change. The number of possible entries is at most , which is bounded, so the loop stops after finitely many rounds. In the last round no tuple of states lacks an entry, for every symbol, so every entry is defined; each entry was set to of its tuple and added to (line L6). The state is added before the loop.
Theorem. For every subject and every position of , lines L8 to L10 give the state . Lines L11 and L12 report the pair if and only if matches .
Proof. The loop of line L8 handles a node after all its children (a node is pushed again below its children, and handled when it is popped the second time, or at once if it is a leaf). By induction on the height of :
- If is a variable, or its symbol has no table, line L9 or line L10 gives , which is by Lemma 2.
- Else , and its children have the states by the induction hypothesis. By Lemma 3 the entry is defined and equals , which is by Lemma 2. A constant is the case .
Line L7 lists, for each state , the patterns with . So line L11 offers at iff fits , iff matches (Lemma 1). If is linear, is up to renaming, and the pair is reported. If not, line L12 reports it iff the full matcher finds with . In both cases the pair is reported iff matches : one direction is the check itself, and if matches then matches it too, so line L11 offers it. Each pair is considered once, because each node has one state and each state lists at most once.
Remark. Lemma 3 does not need the closure to be computed by rounds. Any order that sets on every tuple of reachable states gives the same states and tables, because depends only on its arguments. The reachable states are exactly the match sets of ground terms over the symbols, together with , by induction on the round in which a state is first added.