HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Is there a described algorithm converting _from_ Continuation Passing Style?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
_from_continuationpassingdescribedalgorithmstyleconvertingthere

Problem

Continuation Passing Style (CPS) is a form of code where the control is passed explicitly, by passing the continuation of the code at each call point, unlike direct style.

There are many descriptions in the literature for how to convert from direct style to CPS. The following pseudo-JavaScript:

function direct() {
    var a = foo(1);
    return bar(a);
}


becomes:

function cps(cont) {
    foo(1, function(a) {
        bar(a, cont);
    }
}


But are there any descriptions of the reverse transformation, where the CPS representation is taken back to a direct style?

Solution

I did some additional research to answer this question but find nothing better suited.

To do reverse of CPS to direct programming you have to assign functions to variables, this makes direct structural code.

Automated and well described technique comes from compilers - SSA (Single Static Assignment).
This has additional bonus like compacting code and constant propagating to some extend.

This is not per se technique to reverse CPS, but structure of assignments creates direct code, so it is usable in your situation.

Context

StackExchange Computer Science Q#45273, answer score: 3

Revisions (0)

No revisions yet.