Proofs: Expression tree
These are the human proofs of Expression tree, at full length.
EVAL, SUBS and DIFF on expression trees are correct full
This proof shows: correctness, termination.
Let satisfy the invariant (T). For a node write and for the numbers of leaves and of operator nodes in the subtree of , counting a shared child once per occurrence. Since each operator has two children, . Write for the value of , defined by structural recursion as on the main page.
Lemma 1 (stack lemma for EVAL). Suppose that at the start of a pass of the loop of line L2 the stacks are and . Then after exactly passes the stacks are and , and at no time in between is shorter than or shorter than , so no entry of or is read or changed.
Proof. By induction on the height of . If is a constant , the pass pops at line L3 and pushes at line L4, so after pass and . A symbol is the same with line L5 and . If is an operator node, the first pass pops and line L6 leaves with . By induction for , with in the role of , after passes and . By induction for , after more passes and . The next pass pops ; the tests of lines L4 to L6 fail, since is an operator and done is true, so line L7 pops , then , and pushes . The total is passes. During the passes for and , the induction hypothesis keeps and at least as long as the stated prefixes, which contain and .
Theorem 1. EVAL terminates after passes and returns , provided defines every symbol of .
Proof. Line L1 sets with empty and empty. By Lemma 1, after passes is empty, so the loop of line L2 stops, and has one element, which line L8 returns. Since , the count is . for EVAL.
Lemma 2 (postorder). The walk used by SUBS and DIFF lists every node occurrence of exactly once, and each operator node after both of its children.
Proof. The walk is EVAL with "append to the output" in place of lines L4, L5 and L7. Lemma 1, with the output list in place of , shows that the occurrences in the subtree of are output during its passes, with itself last. By induction this lists the occurrences of the left subtree, then of the right subtree, then .
Theorem 2. SUBS returns , the tree obtained by replacing each leaf by . Hence, by the substitution lemma, .
Proof. We show for each node , by induction along the postorder of Lemma 2. A leaf that is the symbol gets at line S2, and every other leaf gets itself at line S3; this is the definition of substitution on leaves. For an operator node , both children were processed earlier, so and , and line S4 gives . A node that occurs more than once gets the same result each time, since the computation depends only on its subtree. Line S5 returns . The substitution lemma follows by induction on : for both sides are , for other leaves both are the value of the leaf, and for both sides are computed from the children by the same operation.
Theorem 3. For each environment , evaluated at , where is the polynomial function that denotes.
Proof. The smart constructors return an expression with the same value as the plain node: folding two constants computes the value, and the rules , , , and their mirror images hold in every ring. So it suffices to check lines D2 to D6 with plain nodes, by induction along the postorder. Constants have derivative 0 and a symbol has derivative 1 if and 0 otherwise (lines D2, D3). For and , lines D4 and D5 are the sum and product rules applied to the children's derivatives, which are correct by induction. For , the power rule holds for , where means 1, and ; line D6 and the code use the factor 1 for and for , both of which equal .
Termination and cost. Each walk is the loop of Lemma 1, which performs passes, and each pass does stack and dictionary operations and at most a constant number of node constructions. So SUBS and DIFF stop after steps and create nodes.