Proofs: Substitution tree
These are the human proofs of Substitution tree, at full length.
Substitution tree operations keep the invariant and retrieval is exact full
This proof shows: correctness.
We use the pseudocode and the invariant (S1) to (S4) of the page, and the notation of the term rewriting pages: terms and substitutions applied on the right. For a node with path below the top, , and is the set of indicator variables in ; for the top, and . For a stored term , is its normal form, whose variables are .
Lemma 0 (open variables). If is a child of , then , where is the set of indicator variables in the range of , and is disjoint from and from the domains on the path to .
Proof. , and by (S2). Each variable of occurs in and is replaced; by (S3) each variable of occurs only once in all ranges, so it is not in the range of an ancestor, and it is not or in , whose indicator variables come from those ranges. It is not in an ancestor's domain either, since by (S2) such domains are open at that ancestor, hence come from earlier ranges or are .
Insertion
Loop invariant. At line L2, has domain , its values have no indicator variables, and .
Initially is the top, , and . At line L3, binds exactly the variables of : matching walks every position of against , and each variable of occurs once, so it gets one binding, to a subterm of a value of , which has no indicator variables. Line L4 sets , whose domain is by Lemma 0. For , , because contains only variables of besides stored variables; for other , . Hence .
Line L5. If is empty, is empty. Then is not the top, and by (S1) and (S2) it is a leaf, since an inner node has open variables. So , and adding the entry there keeps (S1). The other parts of the invariant do not change.
Line L8. Here is not empty and no child matches. For , GENERALIZE walks and in parallel. At equal function symbols it descends; at an equal stored variable it keeps it; at an indicator variable of it keeps and sets to the subterm of ; elsewhere it puts a fresh with and the two subterms. By induction on the walk, and , where leaves the kept variables alone. Let be the new node, the old child with substitution , and the new leaf with .
- Terms below . , and agrees with on and is the identity elsewhere on , so . All nodes below keep their terms, and (S1) holds for them.
- The new leaf. . For , , because the variables of are fresh variables, kept variables and stored variables, and binds the first two. For other , . So , which has no indicator variables, and is a leaf with one entry.
- (S2) and (S3). , which is open at . The range of has the fresh variables, each once, and the kept variables of , which leave the range of : the range of consists of subterms of at the positions of fresh variables, which do not contain the kept variables. So every indicator variable still occurs once in all ranges. is the set of fresh variables and is plus the fresh and kept variables, all open at by Lemma 0. Both are nonempty: is checked at line L7, and , which is nonempty because a with no fresh or kept variable would equal on the domain, and then would have matched at line L3.
- (S4). has two children, and the number of children of does not change.
Line L9. The new leaf has domain , nonempty, and ; its range has no indicator variables. is the top or an inner node, since a leaf has no open variables, so (S4) still holds.
Retrieval
Let be the renamed query, with variables disjoint from all stored and indicator variables. Let be the set of rigid variables of the mode: the query variables for generalizations, the stored variables for instances, none for unifiable terms. For a node with path , let be the set of equations and for and . A solution of is a substitution that binds no variable of and makes both sides of each equation equal.
Lemma 1. For a leaf , has a solution iff there is a that binds no variable of with .
Proof. Let solve . By induction on , : the step replaces each variable of by , and . So . Conversely, let . Change on the indicator variables, which occur in neither nor : for each bound on the path at , set , and . Then , and by the same definition applied to the variables of , which are bound later on the path or are not indicator variables.
Lemma 2. When line L13 processes a node , it succeeds iff has a solution, and then is a most general such solution.
Proof. Induction on the depth of . Before , is a most general solution of , by induction, or for a child of the top, the bindings of other branches having been removed at lines L12 and L16. Robinson's algorithm with rigid variables treated as constants, started from a most general solution of a set and given new equations , succeeds iff has a solution, and returns a most general one: every solution of is an instance of , so it solves , and the algorithm is complete for that problem.
Theorem. SUBST-RETRIEVE outputs each entry exactly once if it satisfies the query, and no other entry.
Proof. The stack visits each node at most once, and visits the children of a node iff its equations succeed, so by Lemma 2 it reaches a leaf and line L15 outputs its entries iff has a solution, since grows along the path. By Lemma 1 and (S1), this holds iff for some with no rigid binding. For generalizations the query variables are rigid, so and restricted to the stored variables is a matcher of onto , and so of onto after renaming. For instances the stored variables are rigid, so . For unifiable terms, and have disjoint variables, and is a unifier of the renamed terms. Each entry is at exactly one leaf by (S1).
Deletion
Line L17 follows from the top every child that matches, with the same loop invariant as insertion, and a leaf is reached with empty iff : if , then at every node on its path holds with the bindings that the rest of the path gives, so the path is followed; the converse is the loop invariant. By (S1) the entry, if present, is at such a leaf, and line L18 removes one copy. A leaf left without entries is removed at line L19, which keeps (S1) to (S3) for the other nodes.
Line L21 merges a node , other than the top, with its only child . Let be the indicator variables in the range of . The merged substitution maps to and to . For a variable of , : if this is the definition, and otherwise and . So of the merged node equals , and every node below keeps its term. The domain of is and the part of open at the parent of , so (S2) holds. Each indicator variable of the range of comes from the range of or , and a variable of bound by disappears, so (S3) holds. The merged node has the children and entries of , so (S4) holds again, and the parent of keeps its number of children.