Algorithm Search⌘K

Proofs: Innermost reduction

These are the human proofs of Innermost reduction, at full length.

The stack machine performs exactly the leftmost-innermost reduction full

This proof shows: correctness.

We use the pseudocode of the page and the notation of the term rewriting pages. RR is a list of rules l1→r1,…,lm→rml_1 \to r_1, \ldots, l_m \to r_m, each with lj∉Vl_j \notin V and Var(rj)⊆Var(lj)\mathrm{Var}(r_j) \subseteq \mathrm{Var}(l_j). For a term ss whose arguments are all normal forms, the root rule of ss is the first lj→rjl_j \to r_j in the list with a match τ\tau, ljτ=sl_j\tau = s, if there is one.

Leftmost-innermost reduction. A redex of tt is a position pp such that t∣pt|_p is an instance of some left side. It is innermost if no position strictly below pp is a redex. Two different innermost redexes are not prefixes of each other, so they are ordered from left to right by the lexicographic order of positions. The leftmost-innermost step contracts the leftmost innermost redex with the first rule of the list that matches there. Write LI(t)\mathrm{LI}(t) for the sequence of leftmost-innermost steps from tt; it is finite and ends in a normal form, or it is infinite.

Lemma 1 (decomposition). Let t=f(w1,…,wn)t = f(w_1, \ldots, w_n). Then LI(t)\mathrm{LI}(t) is the concatenation of: LI(w1)\mathrm{LI}(w_1) placed at position 11; if it ends in v1v_1, then LI(w2)\mathrm{LI}(w_2) placed at position 22; and so on; and, if all of them end, with s=f(v1,…,vn)s = f(v_1, \ldots, v_n): nothing if ss is a normal form, otherwise the root step s→rjτs \to r_j\tau by the root rule of ss followed by LI(rjτ)\mathrm{LI}(r_j\tau).

Proof. Let ii be least such that wiw_i is not a normal form. Every redex of tt is either the root or below some position kk. The root is not innermost, since wiw_i has a redex. A redex kqkq with k>ik > i lies to the right of every redex iq′iq', and wiw_i has an innermost redex q′q', which is innermost in tt too, because the positions below iq′iq' in tt are those below q′q' in wiw_i. Redexes below k<ik < i do not exist. So the leftmost-innermost redex of tt is ii followed by the leftmost-innermost redex of wiw_i, with the same rule, and the step changes only the argument wiw_i. Repeating this, tt is reduced inside wiw_i exactly as LI(wi)\mathrm{LI}(w_i) reduces wiw_i, as long as that sequence goes on. If it ends, we continue with the next argument that is not a normal form. When all arguments are normal forms, the only possible redex is the root, which is then innermost; the step uses the root rule. □\square

Lemma 2 (substituted values are normal). If all arguments of ss are normal forms and lτ=sl\tau = s with l∉Vl \notin V, then xτx\tau is a normal form for each x∈Var(l)x \in \mathrm{Var}(l).

Proof. xx occurs in ll at some position q≠εq \ne \varepsilon, since ll is not a variable. Then xτ=s∣qx\tau = s|_q is a subterm of an argument of ss, and a subterm of a normal form is a normal form. □\square

The machine. A task EVAL(u,σ,p)\mathrm{EVAL}(u, \sigma, p) asks for the normal form of uσu\sigma, where every value of σ\sigma is a normal form (for the input term, σ\sigma is empty; a variable of the input is a normal form, because no left side is a variable). The position pp is where uσu\sigma sits in the whole term, and is used only for the trace.

Lemma 3 (one task). Suppose the machine pops EVAL(u,σ,p)\mathrm{EVAL}(u, \sigma, p) with task stack TT below it and value stack WW. Then, before the task stack returns to TT, the machine makes exactly the steps of LI(uσ)\mathrm{LI}(u\sigma), each at pp followed by the position of the step in uσu\sigma, and with the same rule. If LI(uσ)\mathrm{LI}(u\sigma) is finite and ends in vv, the machine reaches the state with task stack TT and value stack W vW\,v. If it is infinite, the task stack never returns to TT. (With a bound BB, the machine stops at line L9 when it is about to make its (B+1)(B+1)-st step; up to that moment the same statement holds for the steps made.)

Proof. We show, for each N≥0N \ge 0, that the first NN steps the machine makes during the task (if it makes that many) are the first NN steps of LI(uσ)\mathrm{LI}(u\sigma), and that the finite-case conclusion holds whenever LI(uσ)\mathrm{LI}(u\sigma) has fewer than NN steps. This is by induction on NN, and inside it by induction on the size of uu.

If uu is a variable, line L4 pushes uσu\sigma, which is a normal form, and makes no step: LI(uσ)\mathrm{LI}(u\sigma) is empty, and the claim holds.

If u=f(u1,…,un)u = f(u_1, \ldots, u_n), line L5 pushes BUILD(f,n,p)\mathrm{BUILD}(f, n, p) and the tasks EVAL(ui,σ,p i)\mathrm{EVAL}(u_i, \sigma, p\,i) with u1u_1 on top. The machine pops EVAL(u1,σ,p 1)\mathrm{EVAL}(u_1, \sigma, p\,1) first. Since u1u_1 is smaller than uu, the inner induction hypothesis applies: the machine makes the steps of LI(u1σ)\mathrm{LI}(u_1\sigma) at p 1p\,1, and if they end in v1v_1, it pushes v1v_1 and goes on with u2u_2, and so on. If one of these sequences is infinite, the machine never pops the BUILD\mathrm{BUILD} task, and by Lemma 1 LI(uσ)\mathrm{LI}(u\sigma) is that same infinite sequence after the finite ones before it. Otherwise the machine pops BUILD(f,n,p)\mathrm{BUILD}(f, n, p) with v1,…,vnv_1, \ldots, v_n on top of WW in this order; line L6 takes them off and forms s=f(v1,…,vn)s = f(v_1, \ldots, v_n). Line L7 finds the root rule of ss. If there is none, ss is a normal form, line L8 pushes it, and by Lemma 1 the steps made are exactly LI(uσ)\mathrm{LI}(u\sigma), which ends in ss. If there is one, lj→rjl_j \to r_j with match τ\tau, line L10 makes the root step s→rjτs \to r_j\tau at pp, which is the next step of LI(uσ)\mathrm{LI}(u\sigma) by Lemma 1, and pushes EVAL(rj,τ,p)\mathrm{EVAL}(r_j, \tau, p). By Lemma 2 the values of τ\tau are normal forms, so this task has the required form. The machine has already made at least one step during the task, so fewer than NN steps remain to be compared, and the outer induction hypothesis (for a smaller NN, with uu arbitrary) applies to the new task: its steps are those of LI(rjτ)\mathrm{LI}(r_j\tau) at pp, and if they end in vv, it pushes vv and the task stack is TT again. By Lemma 1 this is the rest of LI(uσ)\mathrm{LI}(u\sigma).

The bound only stops the machine before a step, so it does not change the steps made before it. □\square

Theorem. On input tt, RR and no bound, the algorithm returns (v,k)(v, k) exactly when LI(t)\mathrm{LI}(t) is finite with kk steps and ends in the normal form vv, and the recorded trace is LI(t)\mathrm{LI}(t). If LI(t)\mathrm{LI}(t) is infinite, the algorithm does not stop. With a bound BB, it returns (v,k)(v, k) when LI(t)\mathrm{LI}(t) has k≤Bk \le B steps, and it returns ⊥\bot after exactly BB steps otherwise.

Proof. Line L1 starts with the single task EVAL(t,∅,ε)\mathrm{EVAL}(t, \emptyset, \varepsilon), TT and WW empty. Apply Lemma 3. In the finite case the task stack becomes empty with W=vW = v, the loop of line L2 ends, and line L11 returns vv and the step count, which line L10 increased once per step. In the infinite case the task stack never empties. With a bound BB, line L9 returns ⊥\bot exactly when a (B+1)(B+1)-st step is about to be made, that is, when LI(t)\mathrm{LI}(t) has more than BB steps. The returned vv is a normal form of tt because each step is a step of →R\to_R, and the end of LI(t)\mathrm{LI}(t) has no redex. ■\blacksquare

Back to Innermost reduction