Algorithm Search⌘K

Techniqueresearch

Supercompilation

Also called Turchin supercompiler, positive supercompilation.

Drives a program symbolically on unknown input, folds repeated configurations into recursion and generalizes to stop, giving a residual program.

Supercompilation, an idea of Valentin Turchin from the 1970s, transforms a program by running it on input that is partly or wholly unknown. This is driving: a call is unfolded, a case on a known constructor takes its branch, and a case on an unknown variable splits into one branch for each pattern, where the variable is known to match that pattern. The result is a process tree of terms. When a term is a renaming of an earlier term on the same path, the path folds into a recursive call. When a term grows in a way that may never fold, a whistle blows, and the supercompiler replaces terms by more general ones so that the tree stays finite. The residual program is read off the tree. Because driving keeps the outcome of each test, a supercompiler can do what partial evaluation and deforestation do and more: from a naive string matcher and a fixed pattern it makes a matcher that reads each text character once. This page describes positive supercompilation in the form of Sørensen, Glück and Jones, with the homeomorphic embedding as the whistle, and proves that the whistle stops it. The common mistake is to think that the whistle only stops runs that would loop. It blows on finite runs too, and then the supercompiler generalizes and the result is weaker than it could be.

In plain words

Imagine running a program without knowing its input. When the program tests the input, follow both answers, and remember on each side which answer holds. Each step is written down. When you reach a situation you have already seen, up to the names of the unknowns, you write "go back there" and stop on that side. That makes a loop in the new program.

Some programs never come back to the same situation: a list that the program builds keeps getting longer. So we watch for a situation that contains an earlier one inside it, as a smaller copy. When that happens, we forget the parts that differ and treat them as unknown. Now the situations do repeat.

The written record is a new program. It does the same job, but the tests and the data that were decided while driving are gone.

When it applies

  • The program is functional and first-order, with algebraic data types, as in the template. Supercompilers also exist for Refal, Turchin's language, for higher-order functional languages, and for logic programs, where the same method is called partial deduction.
  • Part of the input is known and part is not, as in partial evaluation, or the program is a composition of stages, as in deforestation, or both.
  • The program tests its unknown input, and later work depends on what those tests found. This is where positive information pays off.
  • The time to supercompile, and the size of the residual program, are acceptable. Driving with full splitting can make a residual program much larger than the source.

The template

Language. As for deforestation: a program is a list of equations f(xˉ)=bf(\bar x) = b, and a term is a variable, c(tˉ)c(\bar t), f(tˉ)f(\bar t) or case t0 of p1→u1∣⋯∣pk→uk\mathtt{case}\ t_0\ \mathtt{of}\ p_1 \to u_1 \mid \cdots \mid p_k \to u_k with flat patterns. The meaning is call by name. The residual program may also use let zˉ=tˉ in g\mathtt{let}\ \bar z = \bar t\ \mathtt{in}\ g, which means g[tˉ/zˉ]g[\bar t / \bar z]. Every term is written as E[r]E[r] with a case context EE and a redex rr, which is a variable, a constructor term or a call.

Trivial and non-trivial terms. A term is trivial if it is a variable, a constructor term c(tˉ)c(\bar t) with an empty context, or a let. Every other term is non-trivial, and its kind is that of its redex: var, con or call.

Driving. One step of driving gives the children of a term:

  • trivial: a variable has none; c(tˉ)c(\bar t) has the children t1,…,tnt_1, \ldots, t_n; let zˉ=tˉ in g\mathtt{let}\ \bar z = \bar t\ \mathtt{in}\ g has the children t1,…,tnt_1, \ldots, t_n and gg.
  • kind var, E[case x of pi→ui]E[\mathtt{case}\ x\ \mathtt{of}\ p_i \to u_i]: one child for each branch, E[ui]E[u_i] with xx replaced by pip_i everywhere. This replacement is the positive information.
  • kind con, E[case c(tˉ) of…c(yˉ)→u…]E[\mathtt{case}\ c(\bar t)\ \mathtt{of} \ldots c(\bar y) \to u \ldots]: the child E[u[tˉ/yˉ]]E[u[\bar t / \bar y]].
  • kind call, E[f(tˉ)]E[f(\bar t)] with f(xˉ)=bf(\bar x) = b: the child E[b[tˉ/xˉ]]E[b[\bar t / \bar x]].

Homeomorphic embedding. All variables count as one symbol. The relation s⊴ts \trianglelefteq t holds if one of these holds:

  • variables: ss and tt are both variables;
  • diving: t=h(t1,…,tn)t = h(t_1, \ldots, t_n) and s⊴tis \trianglelefteq t_i for some ii;
  • coupling: s=h(s1,…,sn)s = h(s_1, \ldots, s_n), t=h(t1,…,tn)t = h(t_1, \ldots, t_n) with the same symbol, and si⊴tis_i \trianglelefteq t_i for every ii.

Here a case is a symbol whose name is the list of its patterns, and whose arguments are the selector and the branches. So s⊴ts \trianglelefteq t means that ss is tt with some parts cut out. For example, rev(u,Nil)⊴rev(r,Cons(x,Nil))\mathit{rev}(u, \mathit{Nil}) \trianglelefteq \mathit{rev}(r, \mathit{Cons}(x, \mathit{Nil})), by coupling, with u⊴ru \trianglelefteq r for the variables and Nil⊴Cons(x,Nil)\mathit{Nil} \trianglelefteq \mathit{Cons}(x, \mathit{Nil}) by diving.

Generalization. A term gg is a generalization of ss if s=gθs = g\theta for a substitution θ\theta. The most specific generalization msg(s,t)\mathrm{msg}(s, t) is a common generalization gg, with gθ1=sg\theta_1 = s and gθ2=tg\theta_2 = t, of which every other common generalization is a generalization. It is computed by anti-unification: walk ss and tt together, keep the common symbols, and put one variable for each pair of different subterms, the same variable for the same pair. This is Plotkin's least general generalization. For the two terms above it is rev(v,w)\mathit{rev}(v, w).

The algorithm. Start with a tree of one node, labelled with the start term. Repeat until every leaf is done. Take a leaf β\beta that is not done, with term tt.

  1. If tt is trivial, drive it.
  2. Fold. If a non-trivial ancestor α\alpha has a term tαt_\alpha of which tt is a renaming, β\beta is done: it calls α\alpha.
  3. Whistle. If a non-trivial ancestor α\alpha of the same kind has tα⊴tt_\alpha \trianglelefteq t, take the nearest one, and: a. if t=tαθt = t_\alpha\theta, relabel β\beta with let zˉ=θ(zˉ) in tα\mathtt{let}\ \bar z = \theta(\bar z)\ \mathtt{in}\ t_\alpha, where zˉ\bar z are the free variables of tαt_\alpha (the body then folds by step 2); b. else, if g=msg(tα,t)g = \mathrm{msg}(t_\alpha, t) is not a variable, delete the subtree below α\alpha and relabel α\alpha with let zˉ=θ1(zˉ) in g\mathtt{let}\ \bar z = \theta_1(\bar z)\ \mathtt{in}\ g (upward generalization); c. else split tt: if t=f(tˉ)t = f(\bar t), relabel β\beta with a let that names the arguments that are not variables; if t=case t0…t = \mathtt{case}\ t_0 \ldots with t0t_0 not a variable, name t0t_0 by a let; if t=case x of pi→uit = \mathtt{case}\ x\ \mathtt{of}\ p_i \to u_i, give β\beta the children uiu_i, without positive information. In each case β\beta becomes a trivial node.
  4. Else drive tt.

The residual program has one function $h_\alpha(\bar z) $ for each node α\alpha that some fold calls, with zˉ\bar z the free variables of tαt_\alpha. Its body is read off the subtree of α\alpha: a var node becomes a case on that variable, a con node and a call node become their child, a trivial node becomes the constructor or let that it is, and a fold becomes a call of hαh_\alpha.

Legality. Each step of driving, folding and generalization replaces a term by an equal one in call by name, so if the residual program gives a value, the source gives the same value; and a source run that gives a value is matched by a residual run. The first half is the least solution argument of deforestation: the meanings of the terms tαt_\alpha satisfy the residual equations. The positive information is sound because, in the branch for pip_i, the value of xx does match pip_i. A let from steps 3a, 3b and 3c is a substitution, so it is an equation too. For the second half, every fold is reached after at least one call step on its path, and the induction on the number of call steps of deforestation goes through. Sands (1996) proves total correctness for transformations of this kind in general.

Profit. Every test that the driving decides, every constructor that a case takes apart at once, and every call that is unfolded, is gone from the residual program. In a var node, the residual program tests the variable once, and positive information makes every later test of the same value in that branch a con node, which is decided. This is how the string matcher below reads each text character only once. Generalization gives some of this back: a part of a term that the whistle forgets is computed at run time again.

Termination. Theorem. For a finite program and any start term, the algorithm stops. The proof needs an order theorem.

A quasi-order ≤\le on a set is reflexive and transitive. It is a well-quasi-order (wqo) if every infinite sequence a1,a2,…a_1, a_2, \ldots has indices i<ji < j with ai≤aja_i \le a_j. A sequence with no such pair is bad.

Theorem (Higman 1952, Kruskal 1960). For a finite set of symbols, each with a fixed number of arguments, ⊴\trianglelefteq is a wqo on the finite terms over these symbols and variables.

Lemma 1. ⊴\trianglelefteq is a quasi-order. Reflexivity is coupling on every node, or the variable rule. For transitivity, let s⊴t⊴us \trianglelefteq t \trianglelefteq u; we show s⊴us \trianglelefteq u by induction on the size of uu. If t⊴ut \trianglelefteq u by diving, t⊴uit \trianglelefteq u_i, so s⊴uis \trianglelefteq u_i by induction and s⊴us \trianglelefteq u by diving. If both are variables, ss is a variable too, since nothing else embeds in a variable. If by coupling, t=h(tˉ)t = h(\bar t) and u=h(uˉ)u = h(\bar u) with ti⊴uit_i \trianglelefteq u_i: when s⊴tis \trianglelefteq t_i by diving, s⊴uis \trianglelefteq u_i by induction, and then s⊴us \trianglelefteq u; when s=h(sˉ)s = h(\bar s) by coupling, si⊴uis_i \trianglelefteq u_i for every ii by induction, and then s⊴us \trianglelefteq u. □\square

Lemma 2. In a wqo, every infinite sequence has an infinite subsequence ai1≤ai2≤⋯a_{i_1} \le a_{i_2} \le \cdots. Call ii terminal if no j>ij > i has ai≤aja_i \le a_j. The terminal indices form a bad subsequence, so there are finitely many. After the last one, every index ii has a later jj with ai≤aja_i \le a_j; start after the last terminal index and follow such steps. By transitivity, each element is below all later ones. □\square

Lemma 3. If ≤1,…,≤n\le_1, \ldots, \le_n are wqos on A1,…,AnA_1, \ldots, A_n, the order on A1×⋯×AnA_1 \times \cdots \times A_n that compares each component is a wqo. For n=2n = 2: given pairs (ai,bi)(a_i, b_i), Lemma 2 gives a subsequence on which the aa do not go down, and since ≤2\le_2 is a wqo, two of its bb are in order, bi≤2bjb_i \le_2 b_j with i<ji < j. Then (ai,bi)≤(aj,bj)(a_i, b_i) \le (a_j, b_j). Larger nn follow by induction. □\square

Proof of the theorem (Nash-Williams 1963). Suppose there is a bad sequence. Choose t1t_1 of least size such that some bad sequence starts with t1t_1; then t2t_2 of least size such that some bad sequence starts with t1,t2t_1, t_2; and so on. Every finite part t1,…,tkt_1, \ldots, t_k starts a bad sequence, so the whole sequence (ti)(t_i) is bad. Let SS be the set of all arguments of all the tit_i. SS is wqo: suppose s1,s2,…s_1, s_2, \ldots is bad in SS, with sks_k an argument of tf(k)t_{f(k)}. Each tit_i has finitely many arguments, so we may pass to a subsequence with f(1)<f(2)<⋯f(1) < f(2) < \cdots. The sequence t1,…,tf(1)−1,s1,s2,…t_1, \ldots, t_{f(1) - 1}, s_1, s_2, \ldots is bad: two tt are not in order since (ti)(t_i) is bad; ti⊴skt_i \trianglelefteq s_k would give ti⊴tf(k)t_i \trianglelefteq t_{f(k)} by diving, with i<f(1)≤f(k)i < f(1) \le f(k); and two ss are not in order by choice. But s1s_1 is smaller than tf(1)t_{f(1)}, which breaks the choice of tf(1)t_{f(1)}. So SS is wqo. Now at most one tit_i is a variable, since two variables are in order. There are finitely many symbols, so infinitely many tit_i have the same top symbol hh with nn arguments. Their lists of arguments are in SnS^n, which is wqo by Lemma 3, so two of them, tit_i and tjt_j with i<ji < j, have sm⊴sm′s_m \trianglelefteq s'_m for every argument; by coupling, ti⊴tjt_i \trianglelefteq t_j. This contradicts badness. □\square

The whistle compares only terms of the same kind. The relation "s⊴ts \trianglelefteq t and ss, tt have the same kind" is the product order of ⊴\trianglelefteq with equality on the three kinds, and equality on a finite set is a wqo, so by Lemma 3 it is a wqo too.

Proof of termination. The non-trivial terms that the algorithm meets are built from the function names, the constructors and the case pattern lists of the program, which are finite in number, so the theorem applies to them. (i) Every node has finitely many children. (ii) A non-trivial node changes its label at most once, in step 3a, 3b as the node α\alpha, or 3c, and then it is trivial. A trivial node never changes, and it is never an α\alpha in step 3b. A node is deleted only when an ancestor changes. (iii) So, by induction on the depth dd, there is a time after which no node of depth at most dd changes or is deleted: the root changes at most once, and once the nodes of depth dd are fixed, each of their finitely many children changes at most once more. (iv) Suppose that the algorithm does not stop. The fixed nodes form a limit tree. It is infinite: if it had depth DD, then after the nodes of depth D+1D + 1 are fixed, there are none of them, and all later steps act on fixed nodes, so there are only finitely many. (v) By König's lemma, a tree that is infinite and finitely branching has an infinite path. (vi) On this path, the children of a trivial node are smaller than it: the arguments of a constructor, the parts of a let, the branches of a split case, and the body of a let made by a split, which names at least one argument that is not a variable. So the path has infinitely many non-trivial nodes n1,n2,…n_1, n_2, \ldots, in order of depth. Each of them is driven, not folded and not relabelled, since it is fixed and has children. When njn_j was driven for the last time, its ancestors had their final labels, and the whistle did not blow; so no nin_i with i<ji < j and the same kind has tni⊴tnjt_{n_i} \trianglelefteq t_{n_j}. The terms tn1,tn2,…t_{n_1}, t_{n_2}, \ldots form a bad sequence for the wqo above, which is impossible. □\square

Classic examples

The examples come from a short script that implements the algorithm above and an evaluator that counts the calls, the case steps and the cases on text characters. Every residual program was checked against the source; the inputs are stated with each example.

A string matcher. The naive matcher tests whether a pattern occurs in a text over the letters A and B. It compares the pattern with the text, and after a mismatch it starts again one place to the right:

loop(pp, ss, op, os) = case pp of
    Nil -> True
  | Cons(p, pr) -> case ss of
        Nil -> False
      | Cons(s, sr) -> case eq(p, s) of True -> loop(pr, sr, op, os)
                                      | False -> next(op, os)
next(op, os) = case os of Nil -> False | Cons(s, sr) -> loop(op, sr, op, sr)

Here eq compares two letters by two case steps. Driving loop(AAB, u, AAB, u), with the pattern AAB known and the text u unknown, by steps 1, 2 and 4 only (driving and folding, as in Sørensen, Glück and Jones), gives this program, with names chosen for reading:

m0(u) = case u of Nil -> False
          | Cons(s, r) -> case s of
                B -> m0(r)
              | A -> case r of Nil -> False
                       | Cons(s2, r2) -> case s2 of A -> m2(r2) | B -> m0(r2)
m2(r) = case r of Nil -> False
          | Cons(s, r2) -> case s of A -> m2(r2) | B -> True

This is the automaton of Knuth, Morris and Pratt for AAB: m0 has matched nothing, the inner case has matched A, and m2 has matched AA, where a further A keeps the state. Each text character is tested once. The positive information is what makes it work. After a mismatch, the source restarts on os, but os is now known to start with the letters that were read, so the restart is driven over known letters, and those tests are decided. Without positive information, as in deforestation or partial evaluation, the result tests the same characters again. On the text AAAAAAAAAB, the source does 56 calls, 128 case steps and 24 tests of a text character; the residual program does 9 calls, 20 case steps and 10 tests. On AAAAAAAAAA the counts are 61, 140 and 26 against 10, 21 and 10. The two programs agree on 1000 random texts of length 0 to 12, from a generator with seed 7.

Reverse with an accumulator. rev(xs, acc) = case xs of Nil -> acc | Cons(x, r) -> rev(r, Cons(x, acc)), from rev(u, Nil). Driving gives the var node case u of ..., and in its Cons branch the call node rev(r, Cons(x, Nil)). The ancestor rev(u, Nil) embeds in it, as shown above, and the new term is not an instance of it, since Nil is not a variable. So step 3b fires: msg=rev(v,w)\mathrm{msg} = \mathit{rev}(v, w), and the root becomes let v = u, w = Nil in rev(v, w). Driving rev(v, w) gives rev(r, Cons(x, w)), which is an instance of rev(v, w) (step 3a), and then a fold. The residual program is h(v, w) = case v of Nil -> w | Cons(x, r) -> h(r, Cons(x, w)) with the start h(u, Nil), which is the source again: the whistle saved termination and lost nothing, since there was nothing to gain. Deforestation does not stop on this term.

Deforestation as a special case. From app(app(a, b), c) the algorithm gives

h(a, b, c) = case a of Nil -> app2(b, c) | Cons(x, r) -> Cons(x, h(r, b, c))

where app2 is app again. This is the one-pass program of deforestation, with no intermediate list. The whistle does not blow.

Variations

  • Turchin's supercompiler. Turchin drove Refal programs, whose data are strings with patterns, and kept a graph of configurations. He used generalization and checked for repeated configurations by his own methods; the homeomorphic embedding came later, from the work on termination of term rewriting.
  • Perfect supercompilation. Positive supercompilation keeps only what a test says in its true direction: xx matches pip_i. A perfect supercompiler also keeps negative information, such as x≠Ax \ne A in the else branch of a test on letters, and can then decide more tests.
  • Higher-order supercompilation. Supercompilers for languages with fun x -> e add rules for a call of an unknown function and for a known lambda, and need care with sharing. Supercompilers of this kind have been built for Haskell.
  • Distillation. Hamilton's distillation compares and folds whole process trees, not only terms, and can make programs asymptotically faster, which positive supercompilation cannot.
  • Other whistles. Leuschel (2002) compares the homeomorphic embedding with well-founded orders. Some systems use a bound on the depth or on the size of terms instead, which is simpler and cruder.
  • Partial deduction. The same scheme for logic programs, with unfolding of atoms, is partial deduction. The embedding was used there for termination as well.

Pitfalls

  • The whistle blows too early. In the string matcher, after the text gave AA, driving reaches the call node loop([B], r, AAB, Cons(A, Cons(A, r))). After a third A and a restart, it reaches loop(AAB, Cons(A, Cons(A, r2)), AAB, Cons(A, Cons(A, r2))). The first embeds in the second: [B] embeds in AAB by diving, and a variable embeds in any term. With the whistle on (step 3 included), the script generalizes the pattern position, and the residual program tests pattern letters at run time: on AAAAAAAAAB it does 57 case steps against 20 without the whistle and 128 for the source. It is still correct. Real supercompilers refine the whistle, for example by comparing only terms with the same redex or by ignoring some positions.
  • Comparing terms of different kinds. If the whistle also compares a call node with a var node, it blows on app(app(a, b), c). The script, with that change, generalizes the inner call to let g = app(a, b) in app(g, c) and gives two separate app functions with the intermediate list, which is no fusion at all. Comparing only terms of the same kind keeps the order a wqo and avoids this case.
  • Termination is not speed. The theorem gives no bound on the size of the tree. Driving that splits on many variables can make a residual program exponentially larger than the source.
  • Strict languages. The steps are equations of call by name. In a strict language, unfolding can drop an argument that does not stop, and a let must keep the order of effects.
  • Lost sharing. Unfolding dup(g(y)) with dup(x) = Pair(x, x) copies the call g(y). A supercompiler for a lazy language with sharing must bind such arguments with a let instead.
  • No laws. Like deforestation, supercompilation does not prove algebraic laws. flip(flip(t)) for a tree mirror flip becomes a copy function, not tt.

Exercises

  1. Drive loop(AB, u, AB, u) by hand, with driving and folding only, and give the residual matcher. How many functions does it have?
  2. Show that the sequence of terms f(Nil),f(Cons(x1,Nil)),f(Cons(x2,Cons(x1,Nil))),…f(\mathit{Nil}), f(\mathit{Cons}(x_1, \mathit{Nil})), f(\mathit{Cons}(x_2, \mathit{Cons}(x_1, \mathit{Nil}))), \ldots is not bad, and find the first pair i<ji < j with ti⊴tjt_i \trianglelefteq t_j.
  3. Give an infinite bad sequence for ⊴\trianglelefteq when there are infinitely many constructors, which shows that finiteness of the set of symbols is needed.
  4. Compute msg(f(x,x),f(A,B))\mathrm{msg}(f(x, x), f(A, B)) and msg(Cons(A,Nil),Cons(B,Nil))\mathrm{msg}(\mathit{Cons}(A, \mathit{Nil}), \mathit{Cons}(B, \mathit{Nil})), with the substitutions θ1\theta_1 and θ2\theta_2.
  5. Without positive information, drive loop(AAB, u, AAB, u) with folding. Which tests of the text are done twice in the residual program?

References

  1. Valentin F. Turchin, The concept of a supercompiler, ACM Transactions on Programming Languages and Systems 8(3), 1986, 292-325, driving of Refal programs on configurations with unknown parts, the graph of states, looping back to an earlier configuration, and generalization
  2. Morten Heine Sørensen, Robert Glück and Neil D. Jones, A positive supercompiler, Journal of Functional Programming 6(6), 1996, 811-838, driving with positive information for a first-order functional language, folding, the comparison with deforestation and partial evaluation, and the string matcher example
  3. Morten Heine Sørensen and Robert Glück, An algorithm of generalization in positive supercompilation, Logic Programming, Proceedings of the 1995 International Symposium (ILPS 1995), MIT Press, the homeomorphic embedding as the whistle, the most specific generalization, upward generalization and splitting, and the termination proof
  4. David Sands, Total correctness by local improvement in the transformation of functional programs, ACM Transactions on Programming Languages and Systems 18(2), 1996, 175-234, the improvement theorem, used to prove that folding in transformations of this kind keeps the meaning of the program
  5. Joseph B. Kruskal, Well-quasi-ordering, the tree theorem, and Vazsonyi's conjecture, Transactions of the American Mathematical Society 95(2), 1960, 210-225, the tree theorem, that finite trees are well-quasi-ordered by embedding
  6. C. St. J. A. Nash-Williams, On well-quasi-ordering finite trees, Mathematical Proceedings of the Cambridge Philosophical Society 59(4), 1963, 833-835, the short proof of the tree theorem by a minimal bad sequence
  7. Graham Higman, Ordering by divisibility in abstract algebras, Proceedings of the London Mathematical Society s3-2(1), 1952, 326-336, the well-quasi-ordering of words and of terms of an algebra with finitely many operations of bounded arity under embedding
  8. Michael Leuschel, Homeomorphic embedding for online termination of symbolic methods, The Essence of Computation, LNCS 2566, 2002, 379-403, the homeomorphic embedding as a whistle in supercompilation, partial deduction and term rewriting, its strength against well-founded orders, and its variants

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • three classic examples